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-31 21:59:22 +01:00
|
|
|
import itertools
|
2019-12-14 22:18:21 +01:00
|
|
|
import numpy as np
|
2020-01-08 03:44:00 +01:00
|
|
|
from collections import defaultdict
|
2019-12-31 21:59:22 +01:00
|
|
|
from scipy.spatial import distance as dist
|
2020-01-04 19:00:29 +01:00
|
|
|
from frigate.util import draw_box_with_label, LABELS, compute_intersection_rectangle, compute_intersection_over_union, calculate_region
|
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()
|
|
|
|
|
2019-12-31 21:59:22 +01:00
|
|
|
objects_removed = False
|
|
|
|
for frame_time in detected_objects.keys():
|
|
|
|
if now-frame_time>2:
|
|
|
|
del self._detected_objects[frame_time]
|
|
|
|
objects_removed = True
|
2019-03-27 12:17:00 +01:00
|
|
|
|
2019-12-31 21:59:22 +01:00
|
|
|
if objects_removed:
|
2019-03-27 12:17:00 +01:00
|
|
|
# notify that parsed objects were changed
|
|
|
|
with self._objects_parsed:
|
|
|
|
self._objects_parsed.notify_all()
|
2019-03-16 02:15:41 +01:00
|
|
|
|
2019-12-23 13:40:48 +01:00
|
|
|
class DetectedObjectsProcessor(threading.Thread):
|
|
|
|
def __init__(self, camera):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.camera = camera
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
prctl.set_name(self.__class__.__name__)
|
|
|
|
while True:
|
|
|
|
frame = self.camera.detected_objects_queue.get()
|
|
|
|
|
|
|
|
objects = frame['detected_objects']
|
|
|
|
|
2020-01-05 01:13:53 +01:00
|
|
|
for raw_obj in objects:
|
|
|
|
name = str(LABELS[raw_obj.label_id])
|
2019-12-31 21:59:22 +01:00
|
|
|
|
2020-01-05 01:13:53 +01:00
|
|
|
if not name in self.camera.objects_to_track:
|
|
|
|
continue
|
2019-12-23 13:40:48 +01:00
|
|
|
|
|
|
|
obj = {
|
2020-01-05 01:13:53 +01:00
|
|
|
'name': name,
|
2019-12-31 21:59:22 +01:00
|
|
|
'score': float(raw_obj.score),
|
|
|
|
'box': {
|
|
|
|
'xmin': int((raw_obj.bounding_box[0][0] * frame['size']) + frame['x_offset']),
|
|
|
|
'ymin': int((raw_obj.bounding_box[0][1] * frame['size']) + frame['y_offset']),
|
|
|
|
'xmax': int((raw_obj.bounding_box[1][0] * frame['size']) + frame['x_offset']),
|
|
|
|
'ymax': int((raw_obj.bounding_box[1][1] * frame['size']) + frame['y_offset'])
|
|
|
|
},
|
|
|
|
'region': {
|
|
|
|
'xmin': frame['x_offset'],
|
|
|
|
'ymin': frame['y_offset'],
|
|
|
|
'xmax': frame['x_offset']+frame['size'],
|
|
|
|
'ymax': frame['y_offset']+frame['size']
|
|
|
|
},
|
2019-12-23 13:40:48 +01:00
|
|
|
'frame_time': frame['frame_time'],
|
|
|
|
'region_id': frame['region_id']
|
|
|
|
}
|
2019-12-31 21:59:22 +01:00
|
|
|
|
|
|
|
# if the object is within 5 pixels of the region border, and the region is not on the edge
|
|
|
|
# consider the object to be clipped
|
|
|
|
obj['clipped'] = False
|
|
|
|
if ((obj['region']['xmin'] > 5 and obj['box']['xmin']-obj['region']['xmin'] <= 5) or
|
|
|
|
(obj['region']['ymin'] > 5 and obj['box']['ymin']-obj['region']['ymin'] <= 5) or
|
|
|
|
(self.camera.frame_shape[1]-obj['region']['xmax'] > 5 and obj['region']['xmax']-obj['box']['xmax'] <= 5) or
|
|
|
|
(self.camera.frame_shape[0]-obj['region']['ymax'] > 5 and obj['region']['ymax']-obj['box']['ymax'] <= 5)):
|
|
|
|
obj['clipped'] = True
|
2019-12-23 13:40:48 +01:00
|
|
|
|
|
|
|
# Compute the area
|
2019-12-31 21:59:22 +01:00
|
|
|
obj['area'] = (obj['box']['xmax']-obj['box']['xmin'])*(obj['box']['ymax']-obj['box']['ymin'])
|
2019-12-23 13:40:48 +01:00
|
|
|
|
2019-12-31 21:59:22 +01:00
|
|
|
self.camera.detected_objects[frame['frame_time']].append(obj)
|
|
|
|
|
|
|
|
with self.camera.regions_in_process_lock:
|
|
|
|
self.camera.regions_in_process[frame['frame_time']] -= 1
|
2020-01-02 13:32:02 +01:00
|
|
|
# print(f"{frame['frame_time']} remaining regions {self.camera.regions_in_process[frame['frame_time']]}")
|
2019-12-31 21:59:22 +01:00
|
|
|
|
|
|
|
if self.camera.regions_in_process[frame['frame_time']] == 0:
|
|
|
|
del self.camera.regions_in_process[frame['frame_time']]
|
2020-01-02 13:32:02 +01:00
|
|
|
# print(f"{frame['frame_time']} no remaining regions")
|
2019-12-31 21:59:22 +01:00
|
|
|
self.camera.finished_frame_queue.put(frame['frame_time'])
|
2019-12-23 13:40:48 +01:00
|
|
|
|
2019-12-31 21:59:22 +01:00
|
|
|
# Thread that checks finished frames for clipped objects and sends back
|
|
|
|
# for processing if needed
|
|
|
|
class RegionRefiner(threading.Thread):
|
|
|
|
def __init__(self, camera):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.camera = camera
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
prctl.set_name(self.__class__.__name__)
|
|
|
|
while True:
|
|
|
|
frame_time = self.camera.finished_frame_queue.get()
|
|
|
|
|
2020-01-04 19:02:06 +01:00
|
|
|
detected_objects = self.camera.detected_objects[frame_time].copy()
|
2019-12-31 21:59:22 +01:00
|
|
|
# print(f"{frame_time} finished")
|
|
|
|
|
2020-01-07 03:36:04 +01:00
|
|
|
# group by name
|
2020-01-09 13:50:53 +01:00
|
|
|
detected_object_groups = defaultdict(lambda: [])
|
2020-01-07 03:36:04 +01:00
|
|
|
for obj in detected_objects:
|
|
|
|
detected_object_groups[obj['name']].append(obj)
|
2019-12-31 21:59:22 +01:00
|
|
|
|
2020-01-02 14:38:50 +01:00
|
|
|
look_again = False
|
2020-01-04 19:02:06 +01:00
|
|
|
selected_objects = []
|
2020-01-08 03:44:00 +01:00
|
|
|
for group in detected_object_groups.values():
|
2020-01-07 03:36:04 +01:00
|
|
|
|
|
|
|
# apply non-maxima suppression to suppress weak, overlapping bounding boxes
|
|
|
|
boxes = [(o['box']['xmin'], o['box']['ymin'], o['box']['xmax']-o['box']['xmin'], o['box']['ymax']-o['box']['ymin'])
|
2020-01-08 03:44:00 +01:00
|
|
|
for o in group]
|
|
|
|
confidences = [o['score'] for o in group]
|
2020-01-07 03:36:04 +01:00
|
|
|
idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
|
|
|
|
|
|
|
|
for index in idxs:
|
|
|
|
obj = group[index[0]]
|
|
|
|
selected_objects.append(obj)
|
|
|
|
if obj['clipped']:
|
|
|
|
box = obj['box']
|
|
|
|
# calculate a new region that will hopefully get the entire object
|
|
|
|
(size, x_offset, y_offset) = calculate_region(self.camera.frame_shape,
|
|
|
|
box['xmin'], box['ymin'],
|
|
|
|
box['xmax'], box['ymax'])
|
|
|
|
# print(f"{frame_time} new region: {size} {x_offset} {y_offset}")
|
|
|
|
|
|
|
|
with self.camera.regions_in_process_lock:
|
|
|
|
if not frame_time in self.camera.regions_in_process:
|
|
|
|
self.camera.regions_in_process[frame_time] = 1
|
|
|
|
else:
|
|
|
|
self.camera.regions_in_process[frame_time] += 1
|
|
|
|
|
|
|
|
# add it to the queue
|
|
|
|
self.camera.resize_queue.put({
|
|
|
|
'camera_name': self.camera.name,
|
|
|
|
'frame_time': frame_time,
|
|
|
|
'region_id': -1,
|
|
|
|
'size': size,
|
|
|
|
'x_offset': x_offset,
|
|
|
|
'y_offset': y_offset
|
|
|
|
})
|
|
|
|
self.camera.dynamic_region_fps.update()
|
|
|
|
look_again = True
|
2019-12-31 21:59:22 +01:00
|
|
|
|
2020-01-02 14:38:50 +01:00
|
|
|
# if we are looking again, then this frame is not ready for processing
|
|
|
|
if look_again:
|
2020-01-04 19:02:06 +01:00
|
|
|
# remove the clipped objects
|
|
|
|
self.camera.detected_objects[frame_time] = [o for o in selected_objects if not o['clipped']]
|
2019-12-31 21:59:22 +01:00
|
|
|
continue
|
2020-01-02 13:32:02 +01:00
|
|
|
|
2020-01-04 19:02:06 +01:00
|
|
|
# filter objects based on camera settings
|
|
|
|
selected_objects = [o for o in selected_objects if not self.filtered(o)]
|
2020-01-02 14:38:50 +01:00
|
|
|
|
2020-01-04 19:02:06 +01:00
|
|
|
self.camera.detected_objects[frame_time] = selected_objects
|
2020-01-02 14:38:50 +01:00
|
|
|
|
|
|
|
with self.camera.objects_parsed:
|
|
|
|
self.camera.objects_parsed.notify_all()
|
2019-12-31 21:59:22 +01:00
|
|
|
|
|
|
|
# print(f"{frame_time} is actually finished")
|
|
|
|
|
|
|
|
# keep adding frames to the refined queue as long as they are finished
|
|
|
|
with self.camera.regions_in_process_lock:
|
|
|
|
while self.camera.frame_queue.qsize() > 0 and self.camera.frame_queue.queue[0] not in self.camera.regions_in_process:
|
2020-01-02 14:38:50 +01:00
|
|
|
self.camera.last_processed_frame = self.camera.frame_queue.get()
|
|
|
|
self.camera.refined_frame_queue.put(self.camera.last_processed_frame)
|
2020-01-04 19:02:06 +01:00
|
|
|
|
|
|
|
def filtered(self, obj):
|
|
|
|
object_name = obj['name']
|
|
|
|
|
|
|
|
if object_name in self.camera.object_filters:
|
|
|
|
obj_settings = self.camera.object_filters[object_name]
|
|
|
|
|
|
|
|
# if the min area is larger than the
|
|
|
|
# detected object, don't add it to detected objects
|
|
|
|
if obj_settings.get('min_area',-1) > obj['area']:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# if the detected object is larger than the
|
|
|
|
# max area, don't add it to detected objects
|
|
|
|
if obj_settings.get('max_area', self.camera.frame_shape[0]*self.camera.frame_shape[1]) < obj['area']:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# if the score is lower than the threshold, skip
|
|
|
|
if obj_settings.get('threshold', 0) > obj['score']:
|
|
|
|
return True
|
|
|
|
|
|
|
|
# compute the coordinates of the object and make sure
|
|
|
|
# the location isnt outside the bounds of the image (can happen from rounding)
|
2020-01-09 13:50:53 +01:00
|
|
|
y_location = min(int(obj['box']['ymax']), len(self.camera.mask)-1)
|
|
|
|
x_location = min(int((obj['box']['xmax']-obj['box']['xmin'])/2.0)+obj['box']['xmin'], len(self.camera.mask[0])-1)
|
2020-01-04 19:02:06 +01:00
|
|
|
|
|
|
|
# if the object is in a masked location, don't add it to detected objects
|
|
|
|
if self.camera.mask[y_location][x_location] == [0]:
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2019-12-31 21:59:22 +01:00
|
|
|
|
2020-01-02 13:32:02 +01:00
|
|
|
def has_overlap(self, new_obj, obj, overlap=.7):
|
2019-12-31 21:59:22 +01:00
|
|
|
# compute intersection rectangle with existing object and new objects region
|
|
|
|
existing_obj_current_region = compute_intersection_rectangle(obj['box'], new_obj['region'])
|
|
|
|
|
|
|
|
# compute intersection rectangle with new object and existing objects region
|
|
|
|
new_obj_existing_region = compute_intersection_rectangle(new_obj['box'], obj['region'])
|
|
|
|
|
|
|
|
# compute iou for the two intersection rectangles that were just computed
|
|
|
|
iou = compute_intersection_over_union(existing_obj_current_region, new_obj_existing_region)
|
|
|
|
|
|
|
|
# if intersection is greater than overlap
|
|
|
|
if iou > overlap:
|
|
|
|
return True
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
def find_group(self, new_obj, groups):
|
|
|
|
for index, group in enumerate(groups):
|
|
|
|
for obj in group:
|
|
|
|
if self.has_overlap(new_obj, obj):
|
|
|
|
return index
|
|
|
|
return None
|
|
|
|
|
|
|
|
class ObjectTracker(threading.Thread):
|
|
|
|
def __init__(self, camera, max_disappeared):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.camera = camera
|
|
|
|
self.tracked_objects = {}
|
|
|
|
self.disappeared = {}
|
|
|
|
self.max_disappeared = max_disappeared
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
prctl.set_name(self.__class__.__name__)
|
|
|
|
while True:
|
|
|
|
frame_time = self.camera.refined_frame_queue.get()
|
2020-01-05 01:13:53 +01:00
|
|
|
self.match_and_update(self.camera.detected_objects[frame_time])
|
2020-01-07 03:36:38 +01:00
|
|
|
self.camera.frame_tracked_queue.put(frame_time)
|
2019-12-31 21:59:22 +01:00
|
|
|
|
|
|
|
def register(self, index, obj):
|
2020-01-05 01:13:53 +01:00
|
|
|
id = f"{str(obj['frame_time'])}-{index}"
|
2020-01-09 13:52:28 +01:00
|
|
|
obj['id'] = id
|
2019-12-31 21:59:22 +01:00
|
|
|
self.tracked_objects[id] = obj
|
|
|
|
self.disappeared[id] = 0
|
|
|
|
|
|
|
|
def deregister(self, id):
|
|
|
|
del self.disappeared[id]
|
|
|
|
del self.tracked_objects[id]
|
|
|
|
|
|
|
|
def update(self, id, new_obj):
|
2020-01-08 03:43:25 +01:00
|
|
|
self.tracked_objects[id].update(new_obj)
|
2020-01-05 01:13:53 +01:00
|
|
|
# TODO: am i missing anything? history?
|
2019-12-31 21:59:22 +01:00
|
|
|
|
|
|
|
def match_and_update(self, new_objects):
|
|
|
|
# check to see if the list of input bounding box rectangles
|
|
|
|
# is empty
|
|
|
|
if len(new_objects) == 0:
|
|
|
|
# loop over any existing tracked objects and mark them
|
|
|
|
# as disappeared
|
|
|
|
for objectID in list(self.disappeared.keys()):
|
|
|
|
self.disappeared[objectID] += 1
|
|
|
|
|
|
|
|
# if we have reached a maximum number of consecutive
|
|
|
|
# frames where a given object has been marked as
|
|
|
|
# missing, deregister it
|
|
|
|
if self.disappeared[objectID] > self.max_disappeared:
|
|
|
|
self.deregister(objectID)
|
|
|
|
|
|
|
|
# return early as there are no centroids or tracking info
|
|
|
|
# to update
|
|
|
|
return
|
2020-01-09 13:52:28 +01:00
|
|
|
|
|
|
|
# group by name
|
|
|
|
new_object_groups = defaultdict(lambda: [])
|
2019-12-31 21:59:22 +01:00
|
|
|
for obj in new_objects:
|
2020-01-09 13:52:28 +01:00
|
|
|
new_object_groups[obj['name']].append(obj)
|
2019-12-31 21:59:22 +01:00
|
|
|
|
2020-01-09 13:52:28 +01:00
|
|
|
# track objects for each label type
|
|
|
|
# TODO: this is going to miss deregistering objects that are not in the new groups
|
|
|
|
for label, group in new_object_groups.items():
|
|
|
|
current_objects = [o for o in self.tracked_objects.values() if o['name'] == label]
|
|
|
|
current_ids = [o['id'] for o in current_objects]
|
|
|
|
current_centroids = np.array([o['centroid'] for o in current_objects])
|
|
|
|
|
|
|
|
# compute centroids
|
|
|
|
for obj in group:
|
|
|
|
centroid_x = int((obj['box']['xmin']+obj['box']['xmax']) / 2.0)
|
|
|
|
centroid_y = int((obj['box']['ymin']+obj['box']['ymax']) / 2.0)
|
|
|
|
obj['centroid'] = (centroid_x, centroid_y)
|
|
|
|
|
|
|
|
if len(current_objects) == 0:
|
|
|
|
for index, obj in enumerate(group):
|
|
|
|
self.register(index, obj)
|
|
|
|
return
|
|
|
|
|
|
|
|
new_centroids = np.array([o['centroid'] for o in group])
|
|
|
|
|
|
|
|
# compute the distance between each pair of tracked
|
|
|
|
# centroids and new centroids, respectively -- our
|
|
|
|
# goal will be to match each new centroid to an existing
|
|
|
|
# object centroid
|
|
|
|
D = dist.cdist(current_centroids, new_centroids)
|
|
|
|
|
|
|
|
# in order to perform this matching we must (1) find the
|
|
|
|
# smallest value in each row and then (2) sort the row
|
|
|
|
# indexes based on their minimum values so that the row
|
|
|
|
# with the smallest value is at the *front* of the index
|
|
|
|
# list
|
|
|
|
rows = D.min(axis=1).argsort()
|
|
|
|
|
|
|
|
# next, we perform a similar process on the columns by
|
|
|
|
# finding the smallest value in each column and then
|
|
|
|
# sorting using the previously computed row index list
|
|
|
|
cols = D.argmin(axis=1)[rows]
|
|
|
|
|
|
|
|
# in order to determine if we need to update, register,
|
|
|
|
# or deregister an object we need to keep track of which
|
|
|
|
# of the rows and column indexes we have already examined
|
|
|
|
usedRows = set()
|
|
|
|
usedCols = set()
|
|
|
|
|
|
|
|
# loop over the combination of the (row, column) index
|
|
|
|
# tuples
|
|
|
|
for (row, col) in zip(rows, cols):
|
|
|
|
# if we have already examined either the row or
|
|
|
|
# column value before, ignore it
|
|
|
|
# val
|
|
|
|
if row in usedRows or col in usedCols:
|
|
|
|
continue
|
2019-12-31 21:59:22 +01:00
|
|
|
|
2020-01-09 13:52:28 +01:00
|
|
|
# otherwise, grab the object ID for the current row,
|
|
|
|
# set its new centroid, and reset the disappeared
|
|
|
|
# counter
|
2019-12-31 21:59:22 +01:00
|
|
|
objectID = current_ids[row]
|
2020-01-09 13:52:28 +01:00
|
|
|
self.update(objectID, new_objects[col])
|
|
|
|
self.disappeared[objectID] = 0
|
|
|
|
|
|
|
|
# indicate that we have examined each of the row and
|
|
|
|
# column indexes, respectively
|
|
|
|
usedRows.add(row)
|
|
|
|
usedCols.add(col)
|
|
|
|
|
|
|
|
# compute both the row and column index we have NOT yet
|
|
|
|
# examined
|
|
|
|
unusedRows = set(range(0, D.shape[0])).difference(usedRows)
|
|
|
|
unusedCols = set(range(0, D.shape[1])).difference(usedCols)
|
|
|
|
|
|
|
|
# in the event that the number of object centroids is
|
|
|
|
# equal or greater than the number of input centroids
|
|
|
|
# we need to check and see if some of these objects have
|
|
|
|
# potentially disappeared
|
|
|
|
if D.shape[0] >= D.shape[1]:
|
|
|
|
# loop over the unused row indexes
|
|
|
|
for row in unusedRows:
|
|
|
|
# grab the object ID for the corresponding row
|
|
|
|
# index and increment the disappeared counter
|
|
|
|
objectID = current_ids[row]
|
|
|
|
self.disappeared[objectID] += 1
|
|
|
|
|
|
|
|
# check to see if the number of consecutive
|
|
|
|
# frames the object has been marked "disappeared"
|
|
|
|
# for warrants deregistering the object
|
|
|
|
if self.disappeared[objectID] > self.max_disappeared:
|
|
|
|
self.deregister(objectID)
|
|
|
|
|
|
|
|
# otherwise, if the number of input centroids is greater
|
|
|
|
# than the number of existing object centroids we need to
|
|
|
|
# register each new input centroid as a trackable object
|
|
|
|
else:
|
|
|
|
for col in unusedCols:
|
|
|
|
self.register(col, new_objects[col])
|
2019-12-31 21:59:22 +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-31 21:59:22 +01:00
|
|
|
for obj in itertools.chain.from_iterable(detected_objects.values()):
|
2019-12-14 22:18:21 +01:00
|
|
|
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))
|
|
|
|
|
2019-12-31 21:59:22 +01:00
|
|
|
draw_box_with_label(best_frame, obj['box']['xmin'], obj['box']['ymin'],
|
|
|
|
obj['box']['xmax'], obj['box']['ymax'], obj['name'], f"{int(obj['score']*100)}% {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
|