-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathopen_cv_object_tracker.py
More file actions
64 lines (54 loc) · 1.83 KB
/
open_cv_object_tracker.py
File metadata and controls
64 lines (54 loc) · 1.83 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
### Select ROI and have OpenCV track location and save to log file
# Updated Nov 24
#### This tutorial is really good https://www.youtube.com/watch?v=1FJWXOO1SRI
from time import time
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib as plt
import cv2 #Import openCV
import datetime
# Create a VideoCapture object - 0 is the default setting for built in camera
cap = cv2.VideoCapture(0)
tracker = cv2.legacy.TrackerMOSSE_create()
# tracker = cv2.TrackerCSRT_create()
success, img = cap.read()
bbox = cv2.selectROI("Tracking",img,False)
tracker.init(img,bbox)
# Start a recording timer
start_time = datetime.datetime.now()
x_list = []
y_list = []
w_list = []
h_list = []
time_list = []
# Do this once so there is a 0.0 mark
run_time_sec = 0
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) #15:00min
cv2.putText(img,"Tracking",(75,75),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,255,0),2)
x_list.append(x)
y_list.append(y)
w_list.append(w)
h_list.append(h)
time_list.append(run_time_sec)
df = pd.DataFrame({'x':x_list, 'y':y_list,'w':w_list,'h':h_list,'run_time':time_list})
df.to_csv('tracking_data.csv')
while True:
timer = cv2.getTickCount()
success, img = cap.read()
success, bbox = tracker.update(img)
print(bbox)
if success:
drawBox(img,bbox)
else:
cv2.putText(img,"Lost",(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)
cur_time = datetime.datetime.now()
run_time = cur_time - start_time
run_time_sec = run_time.total_seconds()
if cv2.waitKey(1) & 0xFF == ord('q'):
break