Browse Source

check plasma store and consolidate frame drawing

Blake Blackshear 5 years ago
parent
commit
80b9652f7a
4 changed files with 54 additions and 51 deletions
  1. 12 2
      detect_objects.py
  2. 40 47
      frigate/object_processing.py
  3. 1 1
      frigate/objects.py
  4. 1 1
      frigate/video.py

+ 12 - 2
detect_objects.py

@@ -80,6 +80,11 @@ def main():
     # start plasma store
     # start plasma store
     plasma_cmd = ['plasma_store', '-m', '400000000', '-s', '/tmp/plasma']
     plasma_cmd = ['plasma_store', '-m', '400000000', '-s', '/tmp/plasma']
     plasma_process = sp.Popen(plasma_cmd, stdout=sp.DEVNULL, stderr=sp.DEVNULL)
     plasma_process = sp.Popen(plasma_cmd, stdout=sp.DEVNULL, stderr=sp.DEVNULL)
+    time.sleep(1)
+    rc = plasma_process.poll()
+    if rc is not None:
+        raise RuntimeError("plasma_store exited unexpectedly with "
+                            "code %d" % (rc,))
 
 
     ##
     ##
     # Setup config defaults for cameras
     # Setup config defaults for cameras
@@ -95,6 +100,7 @@ def main():
     # Start the shared tflite process
     # Start the shared tflite process
     tflite_process = EdgeTPUProcess(MODEL_PATH)
     tflite_process = EdgeTPUProcess(MODEL_PATH)
 
 
+    # start the camera processes
     camera_processes = []
     camera_processes = []
     camera_stats_values = {}
     camera_stats_values = {}
     for name, config in CONFIG['cameras'].items():
     for name, config in CONFIG['cameras'].items():
@@ -167,9 +173,13 @@ def main():
         while True:
         while True:
             # max out at 1 FPS
             # max out at 1 FPS
             time.sleep(1)
             time.sleep(1)
-            frame = object_processor.current_frame_with_objects(camera_name)
+            frame = object_processor.get_current_frame(camera_name)
+            if frame is None:
+                frame = np.zeros((720,1280,3), np.uint8)
+            frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
+            ret, jpg = cv2.imencode('.jpg', frame)
             yield (b'--frame\r\n'
             yield (b'--frame\r\n'
-                b'Content-Type: image/jpeg\r\n\r\n' + frame + b'\r\n\r\n')
+                b'Content-Type: image/jpeg\r\n\r\n' + jpg.tobytes() + b'\r\n\r\n')
 
 
     app.run(host='0.0.0.0', port=WEB_PORT, debug=False)
     app.run(host='0.0.0.0', port=WEB_PORT, debug=False)
 
 

+ 40 - 47
frigate/object_processing.py

@@ -40,51 +40,9 @@ class TrackedObjectProcessor(threading.Thread):
             return self.camera_data[camera]['best_objects'][label]['frame']
             return self.camera_data[camera]['best_objects'][label]['frame']
         else:
         else:
             return None
             return None
-
-    def get_frame(self, config, camera, obj):
-        object_id_hash = hashlib.sha1(str.encode(f"{camera}{obj['frame_time']}"))
-        object_id_bytes = object_id_hash.digest()
-        object_id = plasma.ObjectID(object_id_bytes)
-        best_frame = self.plasma_client.get(object_id)
-        box = obj['box']
-        draw_box_with_label(best_frame, box[0], box[1], box[2], box[3], obj['label'], f"{int(obj['score']*100)}% {int(obj['area'])}")
-        # print a timestamp
-        if config['snapshots']['show_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)
-        return best_frame
     
     
-    def current_frame_with_objects(self, camera):
-        frame_time = self.camera_data[camera]['current_frame']
-        object_id_hash = hashlib.sha1(str.encode(f"{camera}{frame_time}"))
-        object_id_bytes = object_id_hash.digest()
-        object_id = plasma.ObjectID(object_id_bytes)
-        current_frame = self.plasma_client.get(object_id)
-            
-        tracked_objects = copy.deepcopy(self.camera_data[camera]['tracked_objects'])
-
-        # draw the bounding boxes on the screen
-        for obj in tracked_objects.values():
-            thickness = 2
-            color = COLOR_MAP[obj['label']]
-            
-            if obj['frame_time'] != frame_time:
-                thickness = 1
-                color = (255,0,0)
-
-            box = obj['box']
-            draw_box_with_label(current_frame, box[0], box[1], box[2], box[3], obj['label'], f"{int(obj['score']*100)}% {int(obj['area'])}", thickness=thickness, color=color)
-        
-        # # print fps
-        # cv2.putText(frame, str(self.fps.eps())+'FPS', (10, 60), cv2.FONT_HERSHEY_SIMPLEX, fontScale=.8, color=(255, 255, 255), thickness=2)
-
-        # convert to BGR
-        frame = cv2.cvtColor(current_frame, cv2.COLOR_RGB2BGR)
-
-        # encode the image into a jpg
-        ret, jpg = cv2.imencode('.jpg', frame)
-
-        return jpg.tobytes()
+    def get_current_frame(self, camera):
+        return self.camera_data[camera]['current_frame']
 
 
     def run(self):
     def run(self):
         while True:
         while True:
@@ -94,21 +52,56 @@ class TrackedObjectProcessor(threading.Thread):
             best_objects = self.camera_data[camera]['best_objects']
             best_objects = self.camera_data[camera]['best_objects']
             current_object_status = self.camera_data[camera]['object_status']
             current_object_status = self.camera_data[camera]['object_status']
             self.camera_data[camera]['tracked_objects'] = tracked_objects
             self.camera_data[camera]['tracked_objects'] = tracked_objects
-            self.camera_data[camera]['current_frame'] = frame_time
+
+            ###
+            # Draw tracked objects on the frame
+            ###
+            object_id_hash = hashlib.sha1(str.encode(f"{camera}{frame_time}"))
+            object_id_bytes = object_id_hash.digest()
+            object_id = plasma.ObjectID(object_id_bytes)
+            current_frame = self.plasma_client.get(object_id)
+
+            # draw the bounding boxes on the frame
+            for obj in tracked_objects.values():
+                thickness = 2
+                color = COLOR_MAP[obj['label']]
+                
+                if obj['frame_time'] != frame_time:
+                    thickness = 1
+                    color = (255,0,0)
+
+                # draw the bounding boxes on the frame
+                box = obj['box']
+                draw_box_with_label(current_frame, box[0], box[1], box[2], box[3], obj['label'], f"{int(obj['score']*100)}% {int(obj['area'])}", thickness=thickness, color=color)
+                # draw the regions on the frame
+                region = obj['region']
+                cv2.rectangle(current_frame, (region[0], region[1]), (region[2], region[3]), (0,255,0), 1)
+            
+            if config['snapshots']['show_timestamp']:
+                time_to_show = datetime.datetime.fromtimestamp(frame_time).strftime("%m/%d/%Y %H:%M:%S")
+                cv2.putText(current_frame, time_to_show, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, fontScale=.8, color=(255, 255, 255), thickness=2)
+
+            ###
+            # Set the current frame as ready
+            ###
+            self.camera_data[camera]['current_frame'] = current_frame
             
             
             ###
             ###
             # Maintain the highest scoring recent object and frame for each label
             # Maintain the highest scoring recent object and frame for each label
             ###
             ###
             for obj in tracked_objects.values():
             for obj in tracked_objects.values():
+                # if the object wasn't seen on the current frame, skip it
+                if obj['frame_time'] != frame_time:
+                    continue
                 if obj['label'] in best_objects:
                 if obj['label'] in best_objects:
                     now = datetime.datetime.now().timestamp()
                     now = datetime.datetime.now().timestamp()
                     # if the object is a higher score than the current best score 
                     # 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
                     # or the current object is more than 1 minute old, use the new object
                     if obj['score'] > best_objects[obj['label']]['score'] or (now - best_objects[obj['label']]['frame_time']) > 60:
                     if obj['score'] > best_objects[obj['label']]['score'] or (now - best_objects[obj['label']]['frame_time']) > 60:
-                        obj['frame'] = self.get_frame(config, camera, obj)
+                        obj['frame'] = np.copy(current_frame)
                         best_objects[obj['label']] = obj
                         best_objects[obj['label']] = obj
                 else:
                 else:
-                    obj['frame'] = self.get_frame(config, camera, obj)
+                    obj['frame'] = np.copy(current_frame)
                     best_objects[obj['label']] = obj
                     best_objects[obj['label']] = obj
 
 
             ###
             ###

+ 1 - 1
frigate/objects.py

@@ -9,7 +9,7 @@ import numpy as np
 import multiprocessing as mp
 import multiprocessing as mp
 from collections import defaultdict
 from collections import defaultdict
 from scipy.spatial import distance as dist
 from scipy.spatial import distance as dist
-from frigate.util import draw_box_with_label, LABELS, calculate_region
+from frigate.util import draw_box_with_label, calculate_region
 
 
 # class ObjectCleaner(threading.Thread):
 # class ObjectCleaner(threading.Thread):
 #     def __init__(self, camera):
 #     def __init__(self, camera):

+ 1 - 1
frigate/video.py

@@ -16,7 +16,7 @@ import copy
 import itertools
 import itertools
 import json
 import json
 from collections import defaultdict
 from collections import defaultdict
-from frigate.util import tonumpyarray, LABELS, draw_box_with_label, area, calculate_region, clipped, intersection_over_union, intersection, EventsPerSecond
+from frigate.util import tonumpyarray, draw_box_with_label, area, calculate_region, clipped, intersection_over_union, intersection, EventsPerSecond
 # from frigate.object_detection import RegionPrepper, RegionRequester
 # from frigate.object_detection import RegionPrepper, RegionRequester
 from frigate.objects import ObjectTracker
 from frigate.objects import ObjectTracker
 # from frigate.mqtt import MqttObjectPublisher
 # from frigate.mqtt import MqttObjectPublisher