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
|
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
|
2019-12-14 23:38:01 +01:00
|
|
|
from . util import tonumpyarray, LABELS, PATH_TO_CKPT
|
2019-02-26 03:27:02 +01:00
|
|
|
|
2019-03-25 12:24:36 +01:00
|
|
|
class PreppedQueueProcessor(threading.Thread):
|
2019-12-23 13:01:32 +01:00
|
|
|
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
|
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.queue_full = queue_full
|
|
|
|
self.avg_inference_speed = 10
|
2019-03-25 12:24:36 +01:00
|
|
|
|
|
|
|
def run(self):
|
2019-12-23 13:01:32 +01:00
|
|
|
prctl.set_name("PreppedQueueProcessor")
|
2019-03-25 12:24:36 +01:00
|
|
|
# process queue...
|
|
|
|
while True:
|
2019-12-23 13:01:32 +01:00
|
|
|
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
|
|
|
|
2019-03-28 02:44:57 +01:00
|
|
|
# Actual detection.
|
2019-12-23 13:01:32 +01:00
|
|
|
frame['detected_objects'] = self.engine.DetectWithInputTensor(frame['frame'], threshold=0.5, 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)
|
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):
|
2019-12-23 13:01:32 +01:00
|
|
|
prctl.set_name("RegionRequester")
|
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
|
|
|
|
2019-12-21 14:15:39 +01:00
|
|
|
for index, region in enumerate(self.camera.config['regions']):
|
|
|
|
# queue with priority 1
|
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
|
|
|
})
|
2019-12-21 14:15:39 +01:00
|
|
|
|
|
|
|
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):
|
2019-12-23 13:01:32 +01:00
|
|
|
prctl.set_name("RegionPrepper")
|
2019-12-21 14:15:39 +01:00
|
|
|
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()
|
|
|
|
|
|
|
|
# Resize to 300x300 if needed
|
|
|
|
if cropped_frame.shape != (300, 300, 3):
|
|
|
|
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
|
|
|
|
if not self.prepped_frame_queue.full():
|
|
|
|
resize_request['frame'] = frame_expanded.flatten().copy()
|
2019-12-23 13:01:32 +01:00
|
|
|
self.prepped_frame_queue.put(resize_request)
|