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
|
2020-01-14 14:00:53 +01:00
|
|
|
import copy
|
2019-12-23 13:01:32 +01:00
|
|
|
import prctl
|
2019-02-26 03:27:02 +01:00
|
|
|
import numpy as np
|
2019-03-17 15:03:52 +01:00
|
|
|
from edgetpu.detection.engine import DetectionEngine
|
2020-01-04 19:00:29 +01:00
|
|
|
|
2020-01-05 01:13:53 +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):
|
2020-01-14 14:00:53 +01:00
|
|
|
def __init__(self, cameras, prepped_frame_queue, fps):
|
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
|
2019-03-28 02:44:57 +01:00
|
|
|
|
|
|
|
# Load the edgetpu engine and labels
|
|
|
|
self.engine = DetectionEngine(PATH_TO_CKPT)
|
2019-12-14 23:38:01 +01:00
|
|
|
self.labels = LABELS
|
2019-12-23 13:01:32 +01:00
|
|
|
self.fps = fps
|
|
|
|
self.avg_inference_speed = 10
|
2019-03-25 12:24:36 +01:00
|
|
|
|
|
|
|
def run(self):
|
2020-01-10 03:53:04 +01:00
|
|
|
prctl.set_name(self.__class__.__name__)
|
2019-03-25 12:24:36 +01:00
|
|
|
# process queue...
|
|
|
|
while True:
|
|
|
|
frame = self.prepped_frame_queue.get()
|
2019-03-30 13:58:31 +01:00
|
|
|
|
2019-03-28 02:44:57 +01:00
|
|
|
# Actual detection.
|
2020-01-06 00:43:14 +01:00
|
|
|
frame['detected_objects'] = self.engine.detect_with_input_tensor(frame['frame'], threshold=0.2, top_k=5)
|
2019-12-23 13:01:32 +01:00
|
|
|
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)
|
2019-12-21 14:15:39 +01:00
|
|
|
|
|
|
|
class RegionRequester(threading.Thread):
|
|
|
|
def __init__(self, camera):
|
2019-12-23 13:01:32 +01:00
|
|
|
threading.Thread.__init__(self)
|
2019-12-21 14:15:39 +01:00
|
|
|
self.camera = camera
|
|
|
|
|
|
|
|
def run(self):
|
2020-01-10 03:53:04 +01:00
|
|
|
prctl.set_name(self.__class__.__name__)
|
2019-12-21 14:15:39 +01:00
|
|
|
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
|
2019-12-23 13:01:32 +01:00
|
|
|
|
2020-01-05 01:13:53 +01:00
|
|
|
# grab the current tracked objects
|
2020-01-14 14:00:53 +01:00
|
|
|
with self.camera.object_tracker.tracked_objects_lock:
|
|
|
|
tracked_objects = copy.deepcopy(self.camera.object_tracker.tracked_objects).values()
|
2020-01-05 01:13:53 +01:00
|
|
|
|
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'])
|
2020-01-05 01:13:53 +01:00
|
|
|
self.camera.regions_in_process[frame_time] += len(tracked_objects)
|
2019-12-31 21:59:22 +01:00
|
|
|
|
2019-12-21 14:15:39 +01:00
|
|
|
for index, region in enumerate(self.camera.config['regions']):
|
2019-12-23 13:01:32 +01:00
|
|
|
self.camera.resize_queue.put({
|
2019-12-21 14:15:39 +01:00
|
|
|
'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']
|
2019-12-23 13:01:32 +01:00
|
|
|
})
|
2020-01-05 01:13:53 +01:00
|
|
|
|
|
|
|
# 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
|
|
|
|
})
|
|
|
|
|
2019-12-21 14:15:39 +01:00
|
|
|
|
|
|
|
class RegionPrepper(threading.Thread):
|
2020-01-14 14:00:53 +01:00
|
|
|
def __init__(self, camera, frame_cache, resize_request_queue, prepped_frame_queue):
|
2019-12-21 14:15:39 +01:00
|
|
|
threading.Thread.__init__(self)
|
2020-01-14 14:00:53 +01:00
|
|
|
self.camera = camera
|
2019-12-21 14:15:39 +01:00
|
|
|
self.frame_cache = frame_cache
|
|
|
|
self.resize_request_queue = resize_request_queue
|
|
|
|
self.prepped_frame_queue = prepped_frame_queue
|
|
|
|
|
|
|
|
def run(self):
|
2020-01-10 03:53:04 +01:00
|
|
|
prctl.set_name(self.__class__.__name__)
|
2019-12-21 14:15:39 +01:00
|
|
|
while True:
|
|
|
|
|
|
|
|
resize_request = self.resize_request_queue.get()
|
|
|
|
|
2020-01-14 14:00:53 +01:00
|
|
|
# if the queue is over 100 items long, only prep dynamic regions
|
|
|
|
if resize_request['region_id'] != -1 and self.prepped_frame_queue.qsize() > 100:
|
|
|
|
with self.camera.regions_in_process_lock:
|
|
|
|
self.camera.regions_in_process[resize_request['frame_time']] -= 1
|
|
|
|
if self.camera.regions_in_process[resize_request['frame_time']] == 0:
|
|
|
|
del self.camera.regions_in_process[resize_request['frame_time']]
|
|
|
|
self.camera.skipped_region_tracker.update()
|
|
|
|
continue
|
|
|
|
|
2019-12-21 14:15:39 +01:00
|
|
|
frame = self.frame_cache.get(resize_request['frame_time'], None)
|
|
|
|
|
|
|
|
if frame is None:
|
|
|
|
print("RegionPrepper: frame_time not in frame_cache")
|
2020-01-15 03:39:42 +01:00
|
|
|
with self.camera.regions_in_process_lock:
|
|
|
|
self.camera.regions_in_process[resize_request['frame_time']] -= 1
|
|
|
|
if self.camera.regions_in_process[resize_request['frame_time']] == 0:
|
|
|
|
del self.camera.regions_in_process[resize_request['frame_time']]
|
|
|
|
self.camera.skipped_region_tracker.update()
|
2019-12-21 14:15:39 +01:00
|
|
|
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
|
|
|
|
2019-12-21 14:15:39 +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?
|
2019-12-21 14:15:39 +01:00
|
|
|
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)
|