-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsingle_tracking.py
More file actions
89 lines (53 loc) · 1.75 KB
/
Copy pathsingle_tracking.py
File metadata and controls
89 lines (53 loc) · 1.75 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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import cv2
import sys
from random import randint
tracker_types = ['BOOSTING', 'MIL', 'KCF', 'TLD', 'MEDIANFLOW', 'MOSSE', 'CSRT']
tracker_type = tracker_types[5]
#print(tracker_type)
if tracker_type == 'BOOSTING':
tracker = cv2.legacy.TrackerBoosting_create()
elif tracker_type == 'MIL':
tracker = cv2.legacy.TrackerMIL_create()
elif tracker_type == 'KCF':
tracker = cv2.legacy.TrackerKCF_create()
elif tracker_type == 'TLD':
tracker = cv2.legacy.TrackerTLD_create()
elif tracker_type == 'MEDIANFLOW':
tracker = cv2.legacy.TrackerMedianFlow_create()
elif tracker_type == 'MOSSE':
tracker = cv2.legacy.TrackerMOSSE_create()
elif tracker_type == 'CSRT':
tracker = cv2.legacy.TrackerCSRT_create()
#print(tracker)
video = cv2.VideoCapture('Videos/race.mp4')
if not video.isOpened():
print('Error while loading the video!')
sys.exit()
ok, frame = video.read()
if not ok:
print('Erro while loading the frame!')
sys.exit()
#print(ok)
bbox = cv2.selectROI(frame) # region of interest
print(bbox)
ok = tracker.init(frame, bbox)
print(ok)
colors = (randint(0, 255), randint(0,255), randint(0, 255)) # RGB -> BGR
print(colors)
while True:
ok, frame = video.read()
#print(ok)
if not ok:
break
ok, bbox = tracker.update(frame)
#print(ok, bbox)
if ok == True:
(x, y, w, h) = [int(v) for v in bbox]
#print(x, y, w, h)
cv2.rectangle(frame, (x, y), (x + w, y + h), colors, 2)
else:
cv2.putText(frame, 'Tracking failure!', (100,80), cv2.FONT_HERSHEY_SIMPLEX, .75, (0,0,255))
cv2.putText(frame, tracker_type, (100, 20), cv2.FONT_HERSHEY_SIMPLEX, .75, (0, 0, 255))
cv2.imshow('Tracking', frame)
if cv2.waitKey(1) & 0XFF == 27: # esc
break