2019-02-26 03:27:02 +01:00
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
import threading
|
2019-02-28 03:55:07 +01:00
|
|
|
import cv2
|
2019-12-23 13:01:32 +01:00
|
|
|
import prctl
|
2019-12-14 22:18:21 +01:00
|
|
|
import numpy as np
|
2019-06-02 14:29:50 +02:00
|
|
|
from . util import draw_box_with_label
|
2019-02-26 03:27:02 +01:00
|
|
|
|
|
|
|
class ObjectCleaner(threading.Thread):
|
2019-03-27 12:17:00 +01:00
|
|
|
def __init__(self, objects_parsed, detected_objects):
|
2019-02-26 03:27:02 +01:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self._objects_parsed = objects_parsed
|
|
|
|
self._detected_objects = detected_objects
|
|
|
|
|
|
|
|
def run(self):
|
2019-12-23 13:01:32 +01:00
|
|
|
prctl.set_name("ObjectCleaner")
|
2019-02-26 03:27:02 +01:00
|
|
|
while True:
|
|
|
|
|
2019-03-27 12:17:00 +01:00
|
|
|
# wait a bit before checking for expired frames
|
|
|
|
time.sleep(0.2)
|
|
|
|
|
|
|
|
# expire the objects that are more than 1 second old
|
|
|
|
now = datetime.datetime.now().timestamp()
|
|
|
|
# look for the first object found within the last second
|
|
|
|
# (newest objects are appended to the end)
|
|
|
|
detected_objects = self._detected_objects.copy()
|
|
|
|
|
|
|
|
num_to_delete = 0
|
|
|
|
for obj in detected_objects:
|
|
|
|
if now-obj['frame_time']<2:
|
|
|
|
break
|
|
|
|
num_to_delete += 1
|
|
|
|
if num_to_delete > 0:
|
|
|
|
del self._detected_objects[:num_to_delete]
|
|
|
|
|
|
|
|
# notify that parsed objects were changed
|
|
|
|
with self._objects_parsed:
|
|
|
|
self._objects_parsed.notify_all()
|
2019-03-16 02:15:41 +01:00
|
|
|
|
2019-02-28 03:55:07 +01:00
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
# Maintains the frame and object with the highest score
|
|
|
|
class BestFrames(threading.Thread):
|
2019-03-27 12:17:00 +01:00
|
|
|
def __init__(self, objects_parsed, recent_frames, detected_objects):
|
2019-02-28 03:55:07 +01:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.objects_parsed = objects_parsed
|
|
|
|
self.recent_frames = recent_frames
|
|
|
|
self.detected_objects = detected_objects
|
2019-12-14 22:18:21 +01:00
|
|
|
self.best_objects = {}
|
|
|
|
self.best_frames = {}
|
2019-02-28 03:55:07 +01:00
|
|
|
|
|
|
|
def run(self):
|
2019-12-23 13:01:32 +01:00
|
|
|
prctl.set_name("BestFrames")
|
2019-02-28 03:55:07 +01:00
|
|
|
while True:
|
|
|
|
|
2019-03-27 12:17:00 +01:00
|
|
|
# wait until objects have been parsed
|
|
|
|
with self.objects_parsed:
|
|
|
|
self.objects_parsed.wait()
|
2019-02-28 03:55:07 +01:00
|
|
|
|
2019-03-27 12:17:00 +01:00
|
|
|
# make a copy of detected objects
|
|
|
|
detected_objects = self.detected_objects.copy()
|
2019-02-28 03:55:07 +01:00
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
for obj in detected_objects:
|
|
|
|
if obj['name'] in self.best_objects:
|
|
|
|
now = datetime.datetime.now().timestamp()
|
|
|
|
# if the object is a higher score than the current best score
|
|
|
|
# or the current object is more than 1 minute old, use the new object
|
|
|
|
if obj['score'] > self.best_objects[obj['name']]['score'] or (now - self.best_objects[obj['name']]['frame_time']) > 60:
|
|
|
|
self.best_objects[obj['name']] = obj
|
|
|
|
else:
|
|
|
|
self.best_objects[obj['name']] = obj
|
2019-03-30 13:58:31 +01:00
|
|
|
|
|
|
|
# make a copy of the recent frames
|
|
|
|
recent_frames = self.recent_frames.copy()
|
2019-03-27 12:17:00 +01:00
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
for name, obj in self.best_objects.items():
|
|
|
|
if obj['frame_time'] in recent_frames:
|
|
|
|
best_frame = recent_frames[obj['frame_time']] #, np.zeros((720,1280,3), np.uint8))
|
|
|
|
|
|
|
|
draw_box_with_label(best_frame, obj['xmin'], obj['ymin'],
|
2019-12-14 23:38:01 +01:00
|
|
|
obj['xmax'], obj['ymax'], obj['name'], obj['score'], obj['area'])
|
2019-12-14 22:18:21 +01:00
|
|
|
|
|
|
|
# print a timestamp
|
|
|
|
time_to_show = datetime.datetime.fromtimestamp(obj['frame_time']).strftime("%m/%d/%Y %H:%M:%S")
|
|
|
|
cv2.putText(best_frame, time_to_show, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, fontScale=.8, color=(255, 255, 255), thickness=2)
|
|
|
|
|
2019-12-23 13:01:32 +01:00
|
|
|
self.best_frames[name] = best_frame
|