blakeblackshear.frigate/frigate/object_detection.py

126 lines
4.9 KiB
Python
Raw Normal View History

2019-02-26 03:27:02 +01:00
import datetime
2019-03-26 02:35:44 +01:00
import time
2019-02-26 03:27:02 +01:00
import cv2
2019-03-25 12:24:36 +01:00
import threading
import prctl
2019-02-26 03:27:02 +01:00
import numpy as np
from edgetpu.detection.engine import DetectionEngine
2020-01-04 19:00:29 +01:00
from frigate.util import tonumpyarray, LABELS, PATH_TO_CKPT, calculate_region
2019-02-26 03:27:02 +01:00
2019-03-25 12:24:36 +01:00
class PreppedQueueProcessor(threading.Thread):
def __init__(self, cameras, prepped_frame_queue, fps, queue_full):
2019-03-25 12:24:36 +01:00
threading.Thread.__init__(self)
2019-03-30 02:49:27 +01:00
self.cameras = cameras
2019-03-25 12:24:36 +01:00
self.prepped_frame_queue = prepped_frame_queue
# Load the edgetpu engine and labels
self.engine = DetectionEngine(PATH_TO_CKPT)
self.labels = LABELS
self.fps = fps
self.queue_full = queue_full
self.avg_inference_speed = 10
2019-03-25 12:24:36 +01:00
def run(self):
prctl.set_name("PreppedQueueProcessor")
2019-03-25 12:24:36 +01:00
# process queue...
while True:
if self.prepped_frame_queue.full():
self.queue_full.update()
2019-03-25 12:24:36 +01:00
frame = self.prepped_frame_queue.get()
2019-03-30 13:58:31 +01:00
# Actual detection.
2020-01-02 13:32:02 +01:00
frame['detected_objects'] = self.engine.DetectWithInputTensor(frame['frame'], threshold=0.2, top_k=5)
self.fps.update()
self.avg_inference_speed = (self.avg_inference_speed*9 + self.engine.get_inference_time())/10
2019-03-25 12:24:36 +01:00
2019-12-23 13:40:48 +01:00
self.cameras[frame['camera_name']].detected_objects_queue.put(frame)
class RegionRequester(threading.Thread):
def __init__(self, camera):
threading.Thread.__init__(self)
self.camera = camera
def run(self):
prctl.set_name("RegionRequester")
frame_time = 0.0
while True:
now = datetime.datetime.now().timestamp()
with self.camera.frame_ready:
# if there isnt a frame ready for processing or it is old, wait for a new frame
if self.camera.frame_time.value == frame_time or (now - self.camera.frame_time.value) > 0.5:
self.camera.frame_ready.wait()
# make a copy of the frame_time
frame_time = self.camera.frame_time.value
# grab the current tracked objects
tracked_objects = self.camera.object_tracker.tracked_objects.values()
2019-12-31 21:59:22 +01:00
with self.camera.regions_in_process_lock:
self.camera.regions_in_process[frame_time] = len(self.camera.config['regions'])
self.camera.regions_in_process[frame_time] += len(tracked_objects)
2019-12-31 21:59:22 +01:00
for index, region in enumerate(self.camera.config['regions']):
self.camera.resize_queue.put({
'camera_name': self.camera.name,
'frame_time': frame_time,
'region_id': index,
'size': region['size'],
'x_offset': region['x_offset'],
'y_offset': region['y_offset']
})
# request a region for tracked objects
for tracked_object in tracked_objects:
box = tracked_object['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'])
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
})
class RegionPrepper(threading.Thread):
def __init__(self, frame_cache, resize_request_queue, prepped_frame_queue):
threading.Thread.__init__(self)
self.frame_cache = frame_cache
self.resize_request_queue = resize_request_queue
self.prepped_frame_queue = prepped_frame_queue
def run(self):
prctl.set_name("RegionPrepper")
while True:
resize_request = self.resize_request_queue.get()
frame = self.frame_cache.get(resize_request['frame_time'], None)
if frame is None:
print("RegionPrepper: frame_time not in frame_cache")
continue
# make a copy of the region
cropped_frame = frame[resize_request['y_offset']:resize_request['y_offset']+resize_request['size'], resize_request['x_offset']:resize_request['x_offset']+resize_request['size']].copy()
2019-12-31 21:59:22 +01:00
# Resize to 300x300 if needed
if cropped_frame.shape != (300, 300, 3):
2019-12-31 21:59:22 +01:00
# TODO: use Pillow-SIMD?
cropped_frame = cv2.resize(cropped_frame, dsize=(300, 300), interpolation=cv2.INTER_LINEAR)
# Expand dimensions since the model expects images to have shape: [1, 300, 300, 3]
frame_expanded = np.expand_dims(cropped_frame, axis=0)
# add the frame to the queue
2019-12-31 21:59:22 +01:00
resize_request['frame'] = frame_expanded.flatten().copy()
self.prepped_frame_queue.put(resize_request)