-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGenerativeDetector.py
379 lines (300 loc) · 13.2 KB
/
GenerativeDetector.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
import cv2
import numpy as np
import time
from abc import ABC, abstractmethod
from threading import Thread
from cv2 import VideoCapture
from multiprocessing import Queue, Process
from multiprocessing import Value
from OpenPersonDetector import OpenPersonDetector
from newgen.TrackJoin import TrackJoin
class PersonDetection:
'''
Class used to represent persons in generated frames
'''
def __init__(self, person_bound):
self.person_bound = person_bound
self.central_point = (
int((person_bound[0] + person_bound[2]) / 2), int((person_bound[1] + person_bound[3]) / 2))
self.track_index = None
self.short_track_index = None
self.head = None
class Frame:
'''
Processed frame provided to frame processor (may be generated or detected)
'''
def __init__(self, raw_frame, time_frame, detections=None):
self.raw_frame = raw_frame # Numpy array of raw pixels
self.time_frame = time_frame
self.detections = detections # Person detections in frame
self.is_detected = False
class _FrameBulk:
def __init__(self, head_frame, tail_frames):
self.head_frame = head_frame
self.tail_frames = tail_frames
class AbstractInputFeeder(ABC):
'''
Base class for input video frames feeder
'''
def __init__(self):
super().__init__()
@abstractmethod
def init(self):
'''
Invoked at the initiation of input process
:return: void
'''
pass
@abstractmethod
def feed_input(self):
'''
Called by input process for requesting a video frame
:return: (boolean : frame_present, 2D numpy array: frame, float: time_frame)
'''
pass
class AbstractFrameProcessor(ABC):
'''
Abstract class for processing output frames
'''
def __init__(self):
super().__init__()
@abstractmethod
def init(self):
'''
Called by main process, during the call to start_sync, following initiation of system.
:return:
'''
pass
@abstractmethod
def process_frame(self, processed_frame):
'''
Called by main process, providing detected/generated frame to user
:param processed_frame: Detected or generated Frame
:return:
'''
pass
class AbstractDetectorGenerator(ABC):
'''
Generates and provides a person detector
'''
def __init__(self):
super().__init__()
@abstractmethod
def generate_detector(self):
'''
Invoked at the initiation of person detection thread.
:return: Instance of person detector to be used
'''
pass
class GenerativeDetector:
'''
Analyses a realtime video frame stream using the provided person detector.
The detections in missed frames by detector (due to time taken for processing a single frame for detection) are
generated using trackers.
'''
def __init__(self):
self.schedule_queue = Queue()
self.detector_results_queue = Queue()
self.all_frames_queue = Queue()
mul = 3
self.schedule_queue_capacity = mul * 3
self.detector_results_queue_capacity = mul * 5
self.results_queue_capacity = mul * 3
self.max_hidden_frame_count = mul * 5
self.detection_frame_time = Value("d", 0.0)
self.tracking_frame_time = Value("d", 0.0)
self.hidden_frame_count = Value("i", 0)
self.frame_generator_process = None
self.detector_process = None
def _input_feed_thread(self, input_feeder):
input_feeder.init()
while True:
while self.schedule_queue.qsize() < self.schedule_queue_capacity and self.detector_results_queue.qsize() < self.detector_results_queue_capacity and self.hidden_frame_count.value < self.max_hidden_frame_count:
r, frame, time_frame = input_feeder.feed_input()
if r:
self.schedule(frame, time_frame)
def _frame_generator_thread(self, _detector_results_queue, _all_results_queue, tracking_frame_time,
hidden_frame_count):
track_join = TrackJoin()
while True:
frame_bulk = _detector_results_queue.get()
track_join.process_bulk(frame_bulk)
head_frame = frame_bulk.head_frame
hidden_frame_count.value -= 1
_all_results_queue.put(head_frame)
trackers = []
track_indices = []
head_links = []
for detection in head_frame.detections:
detection.head = detection
tracker = cv2.TrackerMedianFlow_create()
# tracker = cv2.TrackerKCF_create()
person_bound = tuple(map(int, detection.person_bound))
person_bound = (
person_bound[0], person_bound[1], person_bound[2] - person_bound[0],
person_bound[3] - person_bound[1])
ok = tracker.init(head_frame.raw_frame, person_bound)
if ok:
trackers.append(tracker)
track_indices.append(detection.track_index)
head_links.append(detection)
for tail_frame in frame_bulk.tail_frames:
tail_frame.detections = []
tracking_start_time = time.time()
for _i, tracker in enumerate(trackers):
ok, bbox = tracker.update(tail_frame.raw_frame)
if ok:
gen_person_bound = (int(bbox[0]), int(bbox[1]), int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
generated_detection = PersonDetection(gen_person_bound)
generated_detection.track_index = track_indices[_i]
generated_detection.head = head_links[_i]
tail_frame.detections.append(generated_detection)
# else:
# print("Track Lost", track_indices[_i])
tracking_end_time = time.time()
tracking_frame_time.value = tracking_frame_time.value * 0.5 + (
tracking_end_time - tracking_start_time) * 0.5
hidden_frame_count.value -= 1
_all_results_queue.put(tail_frame)
def print_stats(self):
'''
Print buffer status to standard output
:return:
'''
print("Input Queue Size:", self.schedule_queue.qsize())
print("Detection Results Queue Size:", self.detector_results_queue.qsize())
print("Final Results Queue Size:", self.all_frames_queue.qsize())
def _detector_thread(self, _schedule_queue, _results_queue, detector_generator, detection_frame_time,
hidden_frame_count):
from trinet_reid.trinet import TriNetReID
detector = detector_generator.generate_detector()
re_id = TriNetReID()
while True:
frame = _schedule_queue.get()
hidden_frame_count.value += 1
detection_start_time = time.time()
detections = detector.detectPersons(frame.raw_frame, None)
person_crops = []
_detections = []
for __i, detection in enumerate(detections):
minx, miny, maxx, maxy = detection.upper_body_bound
person_crop = frame.raw_frame[
int(max(0, miny - (maxy - miny) / 1.8 - 5)):int(min(frame.raw_frame.shape[0], maxy + 5)),
int(max(0, minx - 5)):int(min(frame.raw_frame.shape[1], maxx + 5))]
if person_crop.shape[0] > 0 and person_crop.shape[1] > 0:
# detection.re_id_encoding = re_id.embed(person_crop)
person_crops.append(person_crop)
_detections.append(detection)
# cv2.waitKey(1)
embeddings = re_id.embed(person_crops)
for i in range(len(_detections)):
_detections[i].re_id_encoding = embeddings[i]
detection_end_time = time.time()
detection_frame_time.value = detection_frame_time.value * 0.5 + (
detection_end_time - detection_start_time) * 0.5
frame.detections = detections
frame.is_detected = True
backlog = []
while _schedule_queue.qsize() > 0:
backlog.append(_schedule_queue.get())
hidden_frame_count.value += 1
frame_bulk = _FrameBulk(frame, backlog)
_results_queue.put(frame_bulk)
def schedule(self, frame, time_frame):
'''
Manually schedule a frame to input queue.
:param frame: numpy array
:param time_frame: time in seconds
:return:
'''
self.schedule_queue.put(Frame(frame, time_frame))
def has_results(self):
'''
Check whether any processed frames are present in the output buffer.
:return:
'''
return self.all_frames_queue.qsize() > 0
def get_result(self):
'''
Obtain a frame from output buffer
:return:
'''
return self.all_frames_queue.get()
def start_sync(self, input_feeder, frame_processor, detector_generator):
'''
Start processing input to generate detections on frames
:param input_feeder:
:param frame_processor:
:param detector_generator:
:return:
'''
self.frame_generator_process = Process(target=self._frame_generator_thread, args=(
self.detector_results_queue, self.all_frames_queue, self.tracking_frame_time, self.hidden_frame_count))
self.detector_process = Process(target=self._detector_thread, args=(
self.schedule_queue, self.detector_results_queue, detector_generator, self.detection_frame_time,
self.hidden_frame_count))
self.frame_generator_process.daemon = True
self.detector_process.daemon = True
self.frame_generator_process.start()
self.detector_process.start()
self.input_feed_thread = Thread(target=self._input_feed_thread, args=(input_feeder,))
self.input_feed_thread.daemon = True
self.input_feed_thread.start()
frame_processor.init()
while True:
processed_frame = self.get_result()
r = frame_processor.process_frame(processed_frame)
if not r:
return
if __name__ == "__main__":
class DetectorGenerator(AbstractDetectorGenerator):
def generate_detector(self):
return OpenPersonDetector(preview=False)
class FrameProcessor(AbstractFrameProcessor):
def init(self):
self.colour_set = [(255, 0, 0), (0, 255, 0), (0, 0, 255), (255, 255, 0), (255, 0, 255), (0, 255, 255),
(255, 125, 125), (125, 255, 125), (125, 125, 255), (255, 255, 125),
(255, 125, 255), (255, 255, 125)]
cv2.namedWindow("preview", cv2.WINDOW_FREERATIO)
self.start_time_map = {}
def process_frame(self, processed_frame):
frame = processed_frame.raw_frame
time_frame = processed_frame.time_frame
detections = processed_frame.detections
for i, detection in enumerate(detections):
colour = self.colour_set[detection.track_index % len(self.colour_set)]
if detection.track_index not in self.start_time_map:
self.start_time_map[detection.track_index] = time_frame
cv2.putText(frame, str(detection.track_index),
(int(detection.person_bound[0]), int(detection.person_bound[1])), cv2.FONT_HERSHEY_COMPLEX,
1, colour)
time_elapsed = time_frame - self.start_time_map[detection.track_index]
cv2.putText(frame, str(int(time_elapsed)) + " sec",
(int(detection.person_bound[0]), int(detection.person_bound[1] + 20)),
cv2.FONT_HERSHEY_COMPLEX, 1, colour)
cv2.rectangle(frame, (int(detection.person_bound[0]), int(detection.person_bound[1])),
(int(detection.person_bound[2]), int(detection.person_bound[3])), colour)
cv2.imshow("preview", frame)
k = cv2.waitKey(1)
if k & 0xFF == ord("q"):
return False
return True
class InputFeeder(AbstractInputFeeder):
def init(self):
self.cap = VideoCapture("test_videos/ntb/head_office/Cash_Counter_1-1.dav")
self.total_input_frames = 0
def feed_input(self):
# TODO: Add flow rate logic here
r, frame = self.cap.read()
if r:
frame = cv2.resize(frame, (0, 0), fx=0.5, fy=0.5)
frame = np.array(frame, copy=True)
time_frame = self.cap.get(cv2.CAP_PROP_POS_MSEC) / 1000
# time_frame = cap.get_time()
self.total_input_frames += 1
return r, frame, time_frame
else:
return False, None, None
generative_detector = GenerativeDetector()
generative_detector.start_sync(input_feeder=InputFeeder(), frame_processor=FrameProcessor(),
detector_generator=DetectorGenerator())