-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
190 lines (167 loc) · 6.68 KB
/
main.py
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
import argparse
import json
import os
import time
import cv2
import numpy as np
class Tracking():
"""Class to handle tracking operations: Create,initialize and update.
"""
def __init__(self, tracker, first_frame=True):
self.first_frame = first_frame
self.tracker = tracker
# Create OpenCV MultiTracker object
self.multiTracker = cv2.legacy.MultiTracker_create()
def init_trackers(self, bboxes_list, frame):
"""Method to create and initialize user-defined opencv tracker over bboxes defined in first frame.
Args:
bboxes_list (List): Contains info about each object to track(object name,id and coordinates in format (x_topleft,t_topleft,width,height))
frame (Numpy ndarray): First image of videofile.
Returns:
None
"""
for bbox_dict in bboxes_list:
det = bbox_dict['coordinates']
if self.tracker == 'CSRT':
self.multiTracker.add(
cv2.legacy.TrackerCSRT_create(), frame, det)
elif self.tracker == 'KCF':
self.multiTracker.add(
cv2.legacy.TrackerKCF_create(), frame, det)
def update_trackers(self, frame):
"""Method to update tracker object based on the appeareance frame
Args:
frame (Numpy ndarray): Current image of videofile.
Returns:
current_conditions(List):Dictionary list with current coordinates of tracked objects. Same as json input file.
"""
ok, bboxes = self.multiTracker.update(frame)
current_conditions = []
if ok:
for id, bbox in enumerate(bboxes):
current_condition_dict = {
'object': 'player', 'id': id, 'coordinates': list(bbox)}
current_conditions.append(current_condition_dict)
return current_conditions
def draw_boxes(self, frame, bboxes_list):
"""Draw bounding boxes over each object in current frame
Args:
frame (_type_): Current image of videofile.
bboxes_list (_type_):Dictionary list with current coordinates of tracked objects. Same as json input file
"""
for bbox_dict in bboxes_list:
x, y, w, h = int(
bbox_dict['coordinates'][0]), int(
bbox_dict['coordinates'][1]), int(
bbox_dict['coordinates'][2]), int(
bbox_dict['coordinates'][3])
cv2.rectangle(frame, (x, y), ((x + w), (y + h)), (225, 0, 0), 3, 1)
if self.first_frame:
cv2.putText(frame, "Ground truth", (75, 75),
cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
else:
cv2.putText(
frame, "Tracking players:{}".format(
self.tracker), (75, 75), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 225, 0), 2)
cv2.putText(
frame, "Player id:{}".format(
bbox_dict['id']), (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 255), 2)
return frame
def main():
"""
Main file of tracking system.
"""
# Parsers to handle inputs
parser = argparse.ArgumentParser(description='Epicio Tracking system')
parser.add_argument(
"-i",
'--input_video',
type=str,
default='Inputs\\input.mkv',
help="path to input video")
parser.add_argument(
"-json",
'--json_file',
default='Inputs\\initial_conditions.json',
help="path to input json file")
parser.add_argument(
"-t",
'--tracker',
choices=[
"CSRT",
"KCF"],
default="KCF",
help="Choose Opencv tracker")
args = parser.parse_args()
print(
"1 of 8 step: Reading inputs:",
args.json_file,
",",
args.input_video)
print("2 of 8 step: Chose OpenCV tracker:", args.tracker)
# Reading inputs
input_video_extension = os.path.splitext(args.input_video)[1]
if input_video_extension in ['.mkv', '.avi', '.mp4', '.mov']:
cap = cv2.VideoCapture(args.input_video)
else:
raise Exception(
"{} format not supported. Is video?".format(input_video_extension))
input_jsonfile_extension = os.path.splitext(args.json_file)[1]
if input_jsonfile_extension == ".json":
with open(args.json_file, 'r') as f:
initial_conditions = json.load(f)
else:
raise Exception(
"{} format not supported. Is json file?".format(input_jsonfile_extension))
# Define the codec and create VideoWriter object
fourcc = cv2.VideoWriter_fourcc(*'DIVX')
out_videoname = 'output_' + args.tracker + '.mkv'
out_videopath = os.path.join(os.getcwd(), "Output")
if not os.path.isdir(out_videopath):
os.mkdir(out_videopath)
out = cv2.VideoWriter(os.path.join(out_videopath,out_videoname), fourcc, 24.0, (1920, 1080))
# create object tracking
print("3 of 8 step: Creating multitracker object.")
multitrack = Tracking(args.tracker)
# used to record the time when we processed last frame
prev_frame_time = 0
# used to record the time at which we processed current frame
new_frame_time = 0
# Frames per second list
fps_list = []
while cap.isOpened():
ret, frame = cap.read()
if ret:
if multitrack.first_frame:
print("Video started")
print("4 of 8 step: Initializing chosen tracker.")
multitrack.init_trackers(initial_conditions, frame)
print("5 of 8 step: Drawing bboxes as ground truth.")
out_frame = multitrack.draw_boxes(frame, initial_conditions)
multitrack.first_frame = False
print("6 of 8 step: Update tracker over frames.")
else:
current_conditions = multitrack.update_trackers(frame)
out_frame = multitrack.draw_boxes(frame, current_conditions)
else:
print("Video finished. Exiting ...")
break
new_frame_time = time.time()
fps = 1 / (new_frame_time - prev_frame_time)
fps_list.append(fps)
prev_frame_time = new_frame_time
out.write(out_frame)
if cv2.waitKey(1) == ord('q'):
break
print(
"7 of 8 step: Compute output video FPS:{}(Frames per second).".format(
round(
np.mean(fps_list))))
print("8 of 8 step: Create output video file.")
# Release everything if job is finished
cap.release()
out.release()
if __name__ == '__main__':
start_time = time.time()
main()
print("Total time(seconds): {:.4f}".format((time.time() - start_time)))