only cleanup old objects when motion is detected so stationary objects are still detected

This commit is contained in:
blakeblackshear 2019-03-15 20:15:41 -05:00
parent afb70f11a8
commit c12e19349e
2 changed files with 29 additions and 20 deletions

View File

@ -136,7 +136,8 @@ def main():
object_parser = ObjectParser(object_queue, objects_parsed, DETECTED_OBJECTS) object_parser = ObjectParser(object_queue, objects_parsed, DETECTED_OBJECTS)
object_parser.start() object_parser.start()
# start a thread to expire objects from the detected objects list # start a thread to expire objects from the detected objects list
object_cleaner = ObjectCleaner(objects_parsed, DETECTED_OBJECTS) object_cleaner = ObjectCleaner(objects_parsed, DETECTED_OBJECTS,
motion_changed, [region['motion_detected'] for region in regions])
object_cleaner.start() object_cleaner.start()
# connect to mqtt and setup last will # connect to mqtt and setup last will

View File

@ -20,33 +20,41 @@ class ObjectParser(threading.Thread):
self._objects_parsed.notify_all() self._objects_parsed.notify_all()
class ObjectCleaner(threading.Thread): class ObjectCleaner(threading.Thread):
def __init__(self, objects_parsed, detected_objects): def __init__(self, objects_parsed, detected_objects, motion_changed, motion_regions):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self._objects_parsed = objects_parsed self._objects_parsed = objects_parsed
self._detected_objects = detected_objects self._detected_objects = detected_objects
self.motion_changed = motion_changed
self.motion_regions = motion_regions
def run(self): def run(self):
while True: while True:
# expire the objects that are more than 1 second old # while there is motion
now = datetime.datetime.now().timestamp() while len([r for r in self.motion_regions if r.is_set()]) > 0:
# look for the first object found within the last second # wait a bit before checking for expired frames
# (newest objects are appended to the end) time.sleep(0.2)
detected_objects = self._detected_objects.copy()
num_to_delete = 0
for obj in detected_objects:
if now-obj['frame_time']<1:
break
num_to_delete += 1
if num_to_delete > 0:
del self._detected_objects[:num_to_delete]
# notify that parsed objects were changed # expire the objects that are more than 1 second old
with self._objects_parsed: now = datetime.datetime.now().timestamp()
self._objects_parsed.notify_all() # look for the first object found within the last second
# (newest objects are appended to the end)
# wait a bit before checking for more expired frames detected_objects = self._detected_objects.copy()
time.sleep(0.2) num_to_delete = 0
for obj in detected_objects:
if now-obj['frame_time']<1:
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()
# wait for the global motion flag to change
with self.motion_changed:
self.motion_changed.wait()
# Maintains the frame and person with the highest score from the most recent # Maintains the frame and person with the highest score from the most recent
# motion event # motion event