cv2.Tracker.init()

This is my code:

import cv2
cap = cv2.VideoCapture(0)
tracking = cv2.TrackerGOTURN_create()
success, img = cap.read()
bbox = cv2.selectROI("Tracking", img, False)
print(bbox)
cv2.Tracker.init(img, bbox)


def drawBox(img, bbox):
    x, y, w, h = int(bbox[0]), int(bbox[1]), int(bbox[2]), int(bbox[3])
    cv2.rectangle(img, (x, y), ((x+w), (y+h)), (225, 0, 225), 3, 1)
    cv2.putText(img, "Tracking", (75, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 225, 0), 2)
while True:


    timer = cv2.getTickCount()
    success, img = cap.read()
    success, bbox = tracking.update(img)
    if success:
        drawBox(img, bbox)
    else:
        cv2.putText(img, "Lost", (75, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 225), 2)
    fps = cv2.getTickFrequency()/(cv2.getTickCount() - timer)
    cv2.putText(img, str(int(fps)), (75, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 225), 2)
    cv2.imshow("Tracking", img)
    if cv2.waitKey(1) == ord('q'):
        break
img.release()
cv2.destroyWindow()

When i run, erro is appear

"TypeError: descriptor 'init' for 'cv2.Tracker' objects doesn't apply to a 'numpy.ndarray' object". 

Please, Help me fix it. Thanks.

this is simply wrong code. you have to call the init() method on the object instance, not on the class (like a static method), so instead it must be:

tracking.init(img, bbox)
1 Like