-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectTracker(Moose).py
More file actions
38 lines (28 loc) · 1.15 KB
/
Copy pathObjectTracker(Moose).py
File metadata and controls
38 lines (28 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import cv2
cap = cv2.VideoCapture(0)
# add opencv contrib to use Moose object detector
# higher speed
tracker = cv2.TrackerMOSSE_create()
tracker1 = cv2.TrackerCSRT_create() # low speed but high accuracy
success , img = cap.read()
boundingBox = cv2.selectROI("Tracking", img , False)
tracker.init(img, boundingBox) # create a bounding box
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_COMPLEX, 0.7, (0, 255, 0), 2)
while True:
timer = cv2.getTickCount()
success , img = cap.read()
# updated bounding box
success ,boundingBox = tracker.update(img)
if success:
drawBox()
else:
cv2.putText(img, "Object lost", (75, 75), cv2.FONT_HERSHEY_COMPLEX, 0.7, (0, 0, 255), 2)
# calculate the FPS
fps =cv2.getTickFrequency()/(cv2.getTickCount()-timer)
cv2.putText(img,str(int(fps)),(75,50), cv2.FONT_HERSHEY_COMPLEX,0.7, (0, 0 ,255), 2)
cv2.imshow("Tracking",img)
if cv2.waitKey(1) & 0xFF == ord('q'):
break