Reputation: 148
As the Dan's suggestion, i tried to edit this post Error occurred at setting up MOOSE tracker, I also don't know why this error happened because i installed the Opencv-contrib-python==4.5.1.48.However,aftering installing it,the error is still there. The only tracker is MIL,but i realized that the usage purpose of two trackers is different. I also tried to code tracker = cv2.legacy.TrackerMOSSE_create() like Spyke's suggestion but nothing changes. This is my code:
import cv2
cap = cv2.VideoCapture(0)
tracker = cv2.TrackerMOSSE_create()
success, img = cap.read()
# select a bounding box ( ROI )
bbox = cv2.selectROI("Tracking", img, False)
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), (255, 0, 255), 3, 1)
cv2.putText(img, "Tracking", (75, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 255, 0), 2)
while True:
timer = cv2.getTickCount()
success, img = cap.read()
success, bbox = tracker.update(img)
if success:
drawBox(img, bbox)
else:
cv2.putText(img, "Loss", (75, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
fps = cv2.getTickFrequency() / (cv2.getTickCount() - timer)
cv2.putText(img, str(int(fps)), (75, 50), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
cv2.imshow("Tracking", img)
if cv2.waitKey(1) & 0xff == ord('q'):
break
Upvotes: 8
Views: 10617
Reputation: 107
TrackerMOSSE is indeed under the legacy class as stated by @Spyke. However, Instead of:
tracker = cv2.legacy.TrackerMOSSE_create()
I was able to run MOSSE Tracker in OpenCV 4.5.1 with another kind of syntax:
tracker = cv2.legacy_TrackerMOSSE.create()
Upvotes: 4
Reputation: 134
Actually the latest version of the opencv now has the "TrackerMOSSE_create" under the legacy class So, instead of this
tracker = cv2.TrackerMOSSE_create()
use:
tracker = cv2.legacy.TrackerMOSSE_create()
Upvotes: 8