Search code examples
pythonopencvobject-detection

Failed to use cv2.TrackerMOSSE_create() and other methods in openCV [Python]


I'm trying to perform a simple object detection using OpenCV in Python. My problem is that I cannot use TrackerMOSSE_create method as if it not exists.

I've only installed opencv-contrib-python package using pip install opencv-contrib-pythonand I'm aware that it cannot coexist with the opencv-python in one virtual env so I dont have that one.

I'm working on MAC and Python 3.8. There is a code below.

import cv2


print(cv2.__version__)

cap = cv2.VideoCapture(0)

tracker = cv2.TrackerMOSSE_create()


while True:

    timer = cv2.getTickCount()
    success, img = cap.read()

    fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)

    cv2.putText(img, str(int(fps)), (75, 50), cv2.FONT_ITALIC, 0.7, (0, 0, 255), 2)
    cv2.imshow("Tracking", img)

    if cv2.waitKey(1) & 0xff == ord('q'):
        break

The error I have:

Traceback (most recent call last):
  File "/Users/marcin94/PycharmProjects/tracker_objects/main.py", line 8, in <module>
    tracker = cv2.TrackerMOSSE_create()
AttributeError: module 'cv2.cv2' has no attribute 'TrackerMOSSE_create'

Solution

  • I had the same problem with Python 3.7, I think it has to do with the most recent version of opencv-contrib-python that pip automatically installs. So, I installed version 3.4.13.47 from this overview.

    pip install opencv-contrib-python==3.4.13.47
    

    Then it worked for me. Good luck!