-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathObjectTracker_csrt.py
More file actions
67 lines (53 loc) · 1.74 KB
/
Copy pathObjectTracker_csrt.py
File metadata and controls
67 lines (53 loc) · 1.74 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
66
67
import cv2 as cv
import time
CAMERA_INDEX = "1900-151662242_tiny.mp4" # 0--> webcam and 1--> external camera
cap = cv.VideoCapture(CAMERA_INDEX)
if not cap.isOpened():
print("[ERROR] Cannot open camera")
exit()
tracker = cv.TrackerCSRT_create()
BB = None
total_time = 0
frame_count = 0
tracking_active = False
start_time = 0
def track(frame):
global total_time, frame_count
start = time.time()
success, box = tracker.update(frame)
end = time.time()
elapsed = end - start
total_time += elapsed
frame_count += 1
print(f"[INFO] Frame {frame_count}, Time: {elapsed:.6f}s") #each frame time count
if success:
(x, y, w, h) = [int(v) for v in box]
cv.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 0), 2)
return success, frame
while True:
ret, frame = cap.read()
if not ret:
print("[ERROR] Frame capture failed.")
break
if BB is not None:
if not tracking_active:
start_time = time.time()
tracking_active = True
success, frame = track(frame)
cv.imshow("Frame", frame)
key = cv.waitKey(1) & 0xFF
if key == ord("s"):
BB = cv.selectROI("Frame", frame, fromCenter=False, showCrosshair=True)
tracker.init(frame, BB)
elif key == ord("q"):
break
cap.release()
cv.destroyAllWindows()
# Print summary stats
if tracking_active:
total_tracking_time = time.time() - start_time
print(f"\n[SUMMARY]")
print(f"Total frames tracked: {frame_count}")
print(f"Total tracking execution time: {total_time:.4f} seconds")
print(f"Average tracker.update() time: {total_time / frame_count:.6f} seconds")
print(f"Total real time spent (including display, etc.): {total_tracking_time:.4f} seconds")