avoid extra tracking work on stationary frames

This commit is contained in:
Blake Blackshear 2022-02-05 08:29:22 -06:00
parent dd1cf4d2ce
commit 7e7d70aa5b
2 changed files with 45 additions and 35 deletions

View File

@ -103,6 +103,10 @@ class ObjectTracker:
self.tracked_objects[id]["position_changes"] += 1 self.tracked_objects[id]["position_changes"] += 1
self.tracked_objects[id].update(new_obj) self.tracked_objects[id].update(new_obj)
def update_frame_times(self, frame_time):
for id in self.tracked_objects.keys():
self.tracked_objects[id]["frame_time"] = frame_time
def match_and_update(self, frame_time, new_objects): def match_and_update(self, frame_time, new_objects):
# group by name # group by name
new_object_groups = defaultdict(lambda: []) new_object_groups = defaultdict(lambda: [])

View File

@ -584,6 +584,7 @@ def process_frames(
for obj in object_tracker.tracked_objects.values() for obj in object_tracker.tracked_objects.values()
if obj["id"] in stationary_object_ids if obj["id"] in stationary_object_ids
] ]
for region in regions: for region in regions:
detections.extend( detections.extend(
detect( detect(
@ -599,7 +600,7 @@ def process_frames(
######### #########
# merge objects, check for clipped objects and look again up to 4 times # merge objects, check for clipped objects and look again up to 4 times
######### #########
refining = True refining = len(regions) > 0
refine_count = 0 refine_count = 0
while refining and refine_count < 4: while refining and refine_count < 4:
refining = False refining = False
@ -654,44 +655,49 @@ def process_frames(
## drop detections that overlap too much ## drop detections that overlap too much
consolidated_detections = [] consolidated_detections = []
# group by name
detected_object_groups = defaultdict(lambda: [])
for detection in detections:
detected_object_groups[detection[0]].append(detection)
# loop over detections grouped by label # if detection was run on this frame, consolidate
for group in detected_object_groups.values(): if len(regions) > 0:
# if the group only has 1 item, skip # group by name
if len(group) == 1: detected_object_groups = defaultdict(lambda: [])
consolidated_detections.append(group[0]) for detection in detections:
continue detected_object_groups[detection[0]].append(detection)
# sort smallest to largest by area # loop over detections grouped by label
sorted_by_area = sorted(group, key=lambda g: g[3]) for group in detected_object_groups.values():
# if the group only has 1 item, skip
if len(group) == 1:
consolidated_detections.append(group[0])
continue
for current_detection_idx in range(0, len(sorted_by_area)): # sort smallest to largest by area
current_detection = sorted_by_area[current_detection_idx][2] sorted_by_area = sorted(group, key=lambda g: g[3])
overlap = 0
for to_check_idx in range( for current_detection_idx in range(0, len(sorted_by_area)):
min(current_detection_idx + 1, len(sorted_by_area)), current_detection = sorted_by_area[current_detection_idx][2]
len(sorted_by_area), overlap = 0
): for to_check_idx in range(
to_check = sorted_by_area[to_check_idx][2] min(current_detection_idx + 1, len(sorted_by_area)),
# if 90% of smaller detection is inside of another detection, consolidate len(sorted_by_area),
if (
area(intersection(current_detection, to_check))
/ area(current_detection)
> 0.9
): ):
overlap = 1 to_check = sorted_by_area[to_check_idx][2]
break # if 90% of smaller detection is inside of another detection, consolidate
if overlap == 0: if (
consolidated_detections.append( area(intersection(current_detection, to_check))
sorted_by_area[current_detection_idx] / area(current_detection)
) > 0.9
):
# now that we have refined our detections, we need to track objects overlap = 1
object_tracker.match_and_update(frame_time, consolidated_detections) break
if overlap == 0:
consolidated_detections.append(
sorted_by_area[current_detection_idx]
)
# now that we have refined our detections, we need to track objects
object_tracker.match_and_update(frame_time, consolidated_detections)
# else, just update the frame times for the stationary objects
else:
object_tracker.update_frame_times(frame_time)
# add to the queue if not full # add to the queue if not full
if detected_objects_queue.full(): if detected_objects_queue.full():