-
Notifications
You must be signed in to change notification settings - Fork 2
/
run_sort.py
187 lines (133 loc) · 5.12 KB
/
run_sort.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
import os
import time
import random
import cv2
import imutils
#import dlib
import numpy as np
#import tensorflow as tf
import argparse
from utils import image_utils, model_utils
from utils import Comparator
from sort import Sort
#Initialize tracker
# entry = 0
# exit = 0
parser = argparse.ArgumentParser(description='Run SORT')
parser.add_argument('--input_file', type=str, help='Input videos file path name')
parser.add_argument('--output_file', type=str, help='Output video file path name')
parser.add_argument('--model_path', type=str, help='path to the model')
parser.add_argument('--threshold', type=float, help='threshold for detections')
args = parser.parse_args()
model="tensorflow_hub"
tracker = Sort(use_dlib=False)
# initialize the video stream, pointer to output video file, and frame dimensions
inputFile=args.input_file
vs = cv2.VideoCapture(inputFile)
fps = int(vs.get(cv2.CAP_PROP_FPS))
total = int(vs.get(cv2.CAP_PROP_FRAME_COUNT))
(W, H) = (int(vs.get(cv2.CAP_PROP_FRAME_WIDTH)), int(vs.get(cv2.CAP_PROP_FRAME_HEIGHT)))
result = cv2.VideoWriter(args.output_file,
cv2.VideoWriter_fourcc(*'XVID'),
fps, (W,H))
Tr = args.threshold
# get line info
# line = image_utils.define_ROI(input_file, H, W)
if model=='Haar':
person_cascade = cv2.CascadeClassifier(cv2.data.haarcascades + 'haarcascade_fullbody.xml')
frame_index = 0
while True:
(grabbed, frame) = vs.read()
if not grabbed:
break
detections = model_utils.get_haar_detections(frame, person_cascade, frame_index)
trackers = tracker.update(detections, frame)
for d in trackers:
d = d.astype(np.int32)
frame = image_utils.draw_box(frame, d, (0,255,0))
#if detections != []:
#cv2.putText(frame, 'Detection active', (W-10,H-10), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
result.write(frame)
frame_index += 1
result.release()
vs.release()
elif model=='hog':
frame_index = 0
while True:
(grabbed, frame) = vs.read()
if not grabbed:
break
detections = model_utils.get_hog_svm_detections(frame, frame_index)
trackers = tracker.update(detections, frame)
for d in trackers:
d = d.astype(np.int32)
frame = image_utils.draw_box(frame, d, (0,255,0))
#if detections != []:
#cv2.putText(frame, 'Detection active', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
result.write(frame)
frame_index += 1
result.release()
vs.release()
elif model=='tensorflow':
frame_index = 0
while True:
(grabbed, frame) = vs.read()
if not grabbed:
break
detections = model_utils.get_tensorflow_detections(frame)
trackers = tracker.update(detections, frame)
for d in trackers:
d = d.astype(np.int32)
frame = image_utils.draw_box(frame, d, (0,255,0))
#if detections != []:
#cv2.putText(frame, 'Detection active', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
result.write(frame)
frame_index += 1
result.release()
vs.release()
elif model=='pedestron':
Model = model_utils.initialize_pedestron()
frame_index = 0
while True:
(grabbed, frame) = vs.read()
if not grabbed:
break
detections = model_utils.get_pedestron_detection(Model,frame,thresh=0.7)
trackers = tracker.update(detections, frame)
current={}
for d in trackers:
d = d.astype(np.int32)
frame = image_utils.draw_box(frame, d, (0,255,0))
if detections != []:
cv2.putText(frame, 'Detection active', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
current[d[4]] = (d[0], d[1], d[2], d[3])
if d[4] in tracker.previous:
previous_box = tracker.previous[d[4]]
entry, exit = Comparator.compare_with_prev_position(previous_box, d, line, entry, exit)
tracker.previous = current
frame = image_utils.annotate_frame(frame, line, entry, exit, H, W)
cv2.imshow('pedestron',frame)
cv2.waitKey(1)
result.write(frame)
frame_index += 1
result.release()
vs.release()
elif model=='tensorflow_hub':
Model = model_utils.initialize_tensorflow_hub(args.model_path)
frame_index = 0
while True:
(grabbed, frame) = vs.read()
if not grabbed:
break
detections = model_utils.get_tensorflow_detections(Model,frame,Tr,W,H)
trackers = tracker.update(detections, frame)
for d in trackers:
d = d.astype(np.int32)
frame = image_utils.draw_box(frame, d, (0,255,0))
#if detections != []:
#cv2.putText(frame, 'Detection active', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, 1, (0,255,0), 2)
result.write(frame)
frame_index += 1
if frame_index%fps==0: print(int(frame_index/fps),'seconds_completed')
result.release()
vs.release()