2019-03-30 02:49:27 +01:00
|
|
|
import os
|
2019-02-26 03:27:02 +01:00
|
|
|
import time
|
|
|
|
import datetime
|
|
|
|
import cv2
|
2019-12-21 14:15:39 +01:00
|
|
|
import queue
|
2019-02-27 03:29:52 +01:00
|
|
|
import threading
|
2019-03-30 02:49:27 +01:00
|
|
|
import ctypes
|
|
|
|
import multiprocessing as mp
|
2019-06-02 14:29:50 +02:00
|
|
|
import subprocess as sp
|
2019-05-10 13:19:39 +02:00
|
|
|
import numpy as np
|
2019-12-14 22:18:21 +01:00
|
|
|
from collections import defaultdict
|
2019-06-02 14:29:50 +02:00
|
|
|
from . util import tonumpyarray, draw_box_with_label
|
2019-12-21 14:15:39 +01:00
|
|
|
from . object_detection import FramePrepper, RegionPrepper, RegionRequester
|
2019-12-14 22:18:21 +01:00
|
|
|
from . objects import ObjectCleaner, BestFrames
|
2019-03-30 02:49:27 +01:00
|
|
|
from . mqtt import MqttObjectPublisher
|
2019-02-26 03:27:02 +01:00
|
|
|
|
2019-12-21 14:15:39 +01:00
|
|
|
# Stores 2 seconds worth of frames so they can be used for other threads
|
2019-02-27 03:29:52 +01:00
|
|
|
class FrameTracker(threading.Thread):
|
2019-03-27 12:17:00 +01:00
|
|
|
def __init__(self, shared_frame, frame_time, frame_ready, frame_lock, recent_frames):
|
2019-02-27 03:29:52 +01:00
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.shared_frame = shared_frame
|
|
|
|
self.frame_time = frame_time
|
|
|
|
self.frame_ready = frame_ready
|
|
|
|
self.frame_lock = frame_lock
|
|
|
|
self.recent_frames = recent_frames
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
frame_time = 0.0
|
|
|
|
while True:
|
2019-03-27 12:17:00 +01:00
|
|
|
now = datetime.datetime.now().timestamp()
|
|
|
|
# wait for a frame
|
|
|
|
with self.frame_ready:
|
|
|
|
# if there isnt a frame ready for processing or it is old, wait for a signal
|
|
|
|
if self.frame_time.value == frame_time or (now - self.frame_time.value) > 0.5:
|
|
|
|
self.frame_ready.wait()
|
|
|
|
|
|
|
|
# lock and make a copy of the frame
|
|
|
|
with self.frame_lock:
|
|
|
|
frame = self.shared_frame.copy()
|
|
|
|
frame_time = self.frame_time.value
|
|
|
|
|
|
|
|
# add the frame to recent frames
|
|
|
|
self.recent_frames[frame_time] = frame
|
2019-02-27 03:29:52 +01:00
|
|
|
|
2019-03-27 12:17:00 +01:00
|
|
|
# delete any old frames
|
|
|
|
stored_frame_times = list(self.recent_frames.keys())
|
|
|
|
for k in stored_frame_times:
|
|
|
|
if (now - k) > 2:
|
|
|
|
del self.recent_frames[k]
|
2019-03-30 02:49:27 +01:00
|
|
|
|
2019-12-08 14:03:58 +01:00
|
|
|
def get_frame_shape(source):
|
2019-03-30 02:49:27 +01:00
|
|
|
# capture a single frame and check the frame shape so the correct array
|
|
|
|
# size can be allocated in memory
|
2019-12-08 14:03:58 +01:00
|
|
|
video = cv2.VideoCapture(source)
|
2019-03-30 02:49:27 +01:00
|
|
|
ret, frame = video.read()
|
|
|
|
frame_shape = frame.shape
|
|
|
|
video.release()
|
|
|
|
return frame_shape
|
|
|
|
|
2019-12-08 14:03:58 +01:00
|
|
|
def get_ffmpeg_input(ffmpeg_input):
|
|
|
|
frigate_vars = {k: v for k, v in os.environ.items() if k.startswith('FRIGATE_')}
|
|
|
|
return ffmpeg_input.format(**frigate_vars)
|
2019-03-30 02:49:27 +01:00
|
|
|
|
2019-05-11 14:16:15 +02:00
|
|
|
class CameraWatchdog(threading.Thread):
|
|
|
|
def __init__(self, camera):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.camera = camera
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
|
|
|
|
while True:
|
|
|
|
# wait a bit before checking
|
2019-07-18 05:25:59 +02:00
|
|
|
time.sleep(10)
|
2019-05-11 14:16:15 +02:00
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
if (datetime.datetime.now().timestamp() - self.camera.frame_time.value) > 300:
|
|
|
|
print("last frame is more than 5 minutes old, restarting camera capture...")
|
2019-05-11 14:16:15 +02:00
|
|
|
self.camera.start_or_restart_capture()
|
|
|
|
time.sleep(5)
|
|
|
|
|
2019-07-18 05:25:59 +02:00
|
|
|
# Thread to read the stdout of the ffmpeg process and update the current frame
|
|
|
|
class CameraCapture(threading.Thread):
|
|
|
|
def __init__(self, camera):
|
|
|
|
threading.Thread.__init__(self)
|
|
|
|
self.camera = camera
|
|
|
|
|
|
|
|
def run(self):
|
|
|
|
frame_num = 0
|
|
|
|
while True:
|
|
|
|
if self.camera.ffmpeg_process.poll() != None:
|
|
|
|
print("ffmpeg process is not running. exiting capture thread...")
|
|
|
|
break
|
|
|
|
|
|
|
|
raw_image = self.camera.ffmpeg_process.stdout.read(self.camera.frame_size)
|
|
|
|
|
|
|
|
if len(raw_image) == 0:
|
|
|
|
print("ffmpeg didnt return a frame. something is wrong. exiting capture thread...")
|
|
|
|
break
|
|
|
|
|
|
|
|
frame_num += 1
|
|
|
|
if (frame_num % self.camera.take_frame) != 0:
|
|
|
|
continue
|
|
|
|
|
|
|
|
with self.camera.frame_lock:
|
|
|
|
self.camera.frame_time.value = datetime.datetime.now().timestamp()
|
|
|
|
|
|
|
|
self.camera.current_frame[:] = (
|
|
|
|
np
|
|
|
|
.frombuffer(raw_image, np.uint8)
|
|
|
|
.reshape(self.camera.frame_shape)
|
|
|
|
)
|
|
|
|
# Notify with the condition that a new frame is ready
|
|
|
|
with self.camera.frame_ready:
|
|
|
|
self.camera.frame_ready.notify_all()
|
|
|
|
|
2019-03-30 02:49:27 +01:00
|
|
|
class Camera:
|
2019-12-14 22:18:21 +01:00
|
|
|
def __init__(self, name, ffmpeg_config, global_objects_config, config, prepped_frame_queue, mqtt_client, mqtt_prefix):
|
2019-03-30 02:49:27 +01:00
|
|
|
self.name = name
|
|
|
|
self.config = config
|
|
|
|
self.detected_objects = []
|
2019-12-21 14:15:39 +01:00
|
|
|
self.frame_cache = {}
|
2019-12-08 22:46:43 +01:00
|
|
|
|
|
|
|
self.ffmpeg = config.get('ffmpeg', {})
|
|
|
|
self.ffmpeg_input = get_ffmpeg_input(self.ffmpeg['input'])
|
|
|
|
self.ffmpeg_global_args = self.ffmpeg.get('global_args', ffmpeg_config['global_args'])
|
|
|
|
self.ffmpeg_hwaccel_args = self.ffmpeg.get('hwaccel_args', ffmpeg_config['hwaccel_args'])
|
|
|
|
self.ffmpeg_input_args = self.ffmpeg.get('input_args', ffmpeg_config['input_args'])
|
|
|
|
self.ffmpeg_output_args = self.ffmpeg.get('output_args', ffmpeg_config['output_args'])
|
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
camera_objects_config = config.get('objects', {})
|
|
|
|
|
2019-07-02 04:17:44 +02:00
|
|
|
self.take_frame = self.config.get('take_frame', 1)
|
2019-03-30 02:49:27 +01:00
|
|
|
self.regions = self.config['regions']
|
2019-12-08 14:03:58 +01:00
|
|
|
self.frame_shape = get_frame_shape(self.ffmpeg_input)
|
2019-07-18 05:25:59 +02:00
|
|
|
self.frame_size = self.frame_shape[0] * self.frame_shape[1] * self.frame_shape[2]
|
2019-03-30 02:49:27 +01:00
|
|
|
self.mqtt_client = mqtt_client
|
|
|
|
self.mqtt_topic_prefix = '{}/{}'.format(mqtt_prefix, self.name)
|
|
|
|
|
2019-07-18 05:25:59 +02:00
|
|
|
# create a numpy array for the current frame in initialize to zeros
|
|
|
|
self.current_frame = np.zeros(self.frame_shape, np.uint8)
|
2019-03-30 02:49:27 +01:00
|
|
|
# create shared value for storing the frame_time
|
2019-07-18 05:25:59 +02:00
|
|
|
self.frame_time = mp.Value('d', 0.0)
|
2019-03-30 02:49:27 +01:00
|
|
|
# Lock to control access to the frame
|
|
|
|
self.frame_lock = mp.Lock()
|
|
|
|
# Condition for notifying that a new frame is ready
|
|
|
|
self.frame_ready = mp.Condition()
|
|
|
|
# Condition for notifying that objects were parsed
|
|
|
|
self.objects_parsed = mp.Condition()
|
2019-12-21 14:15:39 +01:00
|
|
|
|
|
|
|
# Queue for prepped frames, max size set to (number of regions * 5)
|
|
|
|
max_queue_size = len(self.config['regions'])*5
|
|
|
|
self.resize_queue = queue.PriorityQueue(max_queue_size)
|
2019-12-15 14:25:40 +01:00
|
|
|
|
|
|
|
# initialize the frame cache
|
|
|
|
self.cached_frame_with_objects = {
|
|
|
|
'frame_bytes': [],
|
|
|
|
'frame_time': 0
|
|
|
|
}
|
2019-03-30 02:49:27 +01:00
|
|
|
|
2019-07-18 05:25:59 +02:00
|
|
|
self.ffmpeg_process = None
|
|
|
|
self.capture_thread = None
|
2019-03-30 02:49:27 +01:00
|
|
|
|
2019-12-21 14:15:39 +01:00
|
|
|
# for each region, merge the object config
|
2019-03-30 02:49:27 +01:00
|
|
|
self.detection_prep_threads = []
|
2019-12-21 14:15:39 +01:00
|
|
|
for region in self.config['regions']:
|
2019-12-14 22:18:21 +01:00
|
|
|
region_objects = region.get('objects', {})
|
|
|
|
# build objects config for region
|
|
|
|
objects_with_config = set().union(global_objects_config.keys(), camera_objects_config.keys(), region_objects.keys())
|
|
|
|
merged_objects_config = defaultdict(lambda: {})
|
|
|
|
for obj in objects_with_config:
|
|
|
|
merged_objects_config[obj] = {**global_objects_config.get(obj,{}), **camera_objects_config.get(obj, {}), **region_objects.get(obj, {})}
|
|
|
|
|
|
|
|
region['objects'] = merged_objects_config
|
|
|
|
|
2019-12-21 14:15:39 +01:00
|
|
|
# start a thread to queue resize requests for regions
|
|
|
|
self.region_requester = RegionRequester(self)
|
|
|
|
|
|
|
|
# start a thread to cache recent frames for processing
|
2019-07-18 05:25:59 +02:00
|
|
|
self.frame_tracker = FrameTracker(self.current_frame, self.frame_time,
|
2019-12-21 14:15:39 +01:00
|
|
|
self.frame_ready, self.frame_lock, self.frame_cache)
|
2019-03-30 02:49:27 +01:00
|
|
|
self.frame_tracker.start()
|
|
|
|
|
2019-12-21 14:15:39 +01:00
|
|
|
# start a thread to resize regions
|
|
|
|
self.region_prepper = RegionPrepper(self.frame_cache, self.resize_queue, prepped_frame_queue)
|
|
|
|
self.region_prepper.start()
|
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
# start a thread to store the highest scoring recent frames for monitored object types
|
2019-12-21 14:15:39 +01:00
|
|
|
self.best_frames = BestFrames(self.objects_parsed, self.frame_cache, self.detected_objects)
|
2019-12-14 22:18:21 +01:00
|
|
|
self.best_frames.start()
|
2019-03-30 02:49:27 +01:00
|
|
|
|
|
|
|
# start a thread to expire objects from the detected objects list
|
|
|
|
self.object_cleaner = ObjectCleaner(self.objects_parsed, self.detected_objects)
|
|
|
|
self.object_cleaner.start()
|
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
# start a thread to publish object scores
|
|
|
|
mqtt_publisher = MqttObjectPublisher(self.mqtt_client, self.mqtt_topic_prefix, self.objects_parsed, self.detected_objects, self.best_frames)
|
2019-03-30 02:49:27 +01:00
|
|
|
mqtt_publisher.start()
|
2019-04-14 18:58:02 +02:00
|
|
|
|
2019-05-11 14:16:15 +02:00
|
|
|
# create a watchdog thread for capture process
|
|
|
|
self.watchdog = CameraWatchdog(self)
|
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
# load in the mask for object detection
|
2019-04-14 18:58:02 +02:00
|
|
|
if 'mask' in self.config:
|
|
|
|
self.mask = cv2.imread("/config/{}".format(self.config['mask']), cv2.IMREAD_GRAYSCALE)
|
2019-07-13 18:31:18 +02:00
|
|
|
else:
|
|
|
|
self.mask = None
|
2019-07-13 14:40:14 +02:00
|
|
|
|
|
|
|
if self.mask is None:
|
2019-04-14 18:58:02 +02:00
|
|
|
self.mask = np.zeros((self.frame_shape[0], self.frame_shape[1], 1), np.uint8)
|
|
|
|
self.mask[:] = 255
|
2019-05-11 14:16:15 +02:00
|
|
|
|
|
|
|
|
|
|
|
def start_or_restart_capture(self):
|
2019-07-18 05:25:59 +02:00
|
|
|
if not self.ffmpeg_process is None:
|
2019-08-11 04:05:15 +02:00
|
|
|
print("Terminating the existing ffmpeg process...")
|
|
|
|
self.ffmpeg_process.terminate()
|
|
|
|
try:
|
|
|
|
print("Waiting for ffmpeg to exit gracefully...")
|
|
|
|
self.ffmpeg_process.wait(timeout=30)
|
|
|
|
except sp.TimeoutExpired:
|
|
|
|
print("FFmpeg didnt exit. Force killing...")
|
|
|
|
self.ffmpeg_process.kill()
|
|
|
|
self.ffmpeg_process.wait()
|
|
|
|
|
2019-07-18 05:25:59 +02:00
|
|
|
print("Waiting for the capture thread to exit...")
|
|
|
|
self.capture_thread.join()
|
|
|
|
self.ffmpeg_process = None
|
|
|
|
self.capture_thread = None
|
2019-05-11 14:16:15 +02:00
|
|
|
|
2019-12-08 14:03:58 +01:00
|
|
|
# create the process to capture frames from the input stream and store in a shared array
|
2019-07-18 05:25:59 +02:00
|
|
|
print("Creating a new ffmpeg process...")
|
|
|
|
self.start_ffmpeg()
|
|
|
|
|
|
|
|
print("Creating a new capture thread...")
|
|
|
|
self.capture_thread = CameraCapture(self)
|
|
|
|
print("Starting a new capture thread...")
|
|
|
|
self.capture_thread.start()
|
|
|
|
|
|
|
|
def start_ffmpeg(self):
|
|
|
|
ffmpeg_cmd = (['ffmpeg'] +
|
2019-12-08 22:46:43 +01:00
|
|
|
self.ffmpeg_global_args +
|
2019-07-18 05:25:59 +02:00
|
|
|
self.ffmpeg_hwaccel_args +
|
2019-08-11 04:05:15 +02:00
|
|
|
self.ffmpeg_input_args +
|
2019-12-08 14:03:58 +01:00
|
|
|
['-i', self.ffmpeg_input] +
|
2019-08-22 03:15:49 +02:00
|
|
|
self.ffmpeg_output_args +
|
|
|
|
['pipe:'])
|
2019-07-18 05:25:59 +02:00
|
|
|
|
|
|
|
print(" ".join(ffmpeg_cmd))
|
|
|
|
|
|
|
|
self.ffmpeg_process = sp.Popen(ffmpeg_cmd, stdout = sp.PIPE, bufsize=self.frame_size)
|
2019-03-30 02:49:27 +01:00
|
|
|
|
|
|
|
def start(self):
|
2019-05-11 14:16:15 +02:00
|
|
|
self.start_or_restart_capture()
|
2019-03-30 02:49:27 +01:00
|
|
|
# start the object detection prep threads
|
|
|
|
for detection_prep_thread in self.detection_prep_threads:
|
|
|
|
detection_prep_thread.start()
|
2019-05-11 14:16:15 +02:00
|
|
|
self.watchdog.start()
|
2019-03-30 02:49:27 +01:00
|
|
|
|
|
|
|
def join(self):
|
2019-07-18 05:25:59 +02:00
|
|
|
self.capture_thread.join()
|
2019-03-30 02:49:27 +01:00
|
|
|
|
|
|
|
def get_capture_pid(self):
|
2019-07-18 05:25:59 +02:00
|
|
|
return self.ffmpeg_process.pid
|
2019-03-30 02:49:27 +01:00
|
|
|
|
|
|
|
def add_objects(self, objects):
|
|
|
|
if len(objects) == 0:
|
|
|
|
return
|
|
|
|
|
|
|
|
for obj in objects:
|
2019-12-14 22:18:21 +01:00
|
|
|
# find the matching region
|
|
|
|
region = self.regions[obj['region_id']]
|
|
|
|
|
|
|
|
# Compute some extra properties
|
|
|
|
obj.update({
|
|
|
|
'xmin': int((obj['box'][0] * region['size']) + region['x_offset']),
|
|
|
|
'ymin': int((obj['box'][1] * region['size']) + region['y_offset']),
|
|
|
|
'xmax': int((obj['box'][2] * region['size']) + region['x_offset']),
|
|
|
|
'ymax': int((obj['box'][3] * region['size']) + region['y_offset'])
|
|
|
|
})
|
|
|
|
|
|
|
|
# Compute the area
|
2019-07-03 13:14:39 +02:00
|
|
|
obj['area'] = (obj['xmax']-obj['xmin'])*(obj['ymax']-obj['ymin'])
|
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
object_name = obj['name']
|
|
|
|
|
|
|
|
if object_name in region['objects']:
|
|
|
|
obj_settings = region['objects'][object_name]
|
|
|
|
|
|
|
|
# if the min area is larger than the
|
|
|
|
# detected object, don't add it to detected objects
|
|
|
|
if obj_settings.get('min_area',-1) > obj['area']:
|
2019-03-30 02:49:27 +01:00
|
|
|
continue
|
2019-12-08 14:16:30 +01:00
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
# if the detected object is larger than the
|
|
|
|
# max area, don't add it to detected objects
|
|
|
|
if obj_settings.get('max_area', region['size']**2) < obj['area']:
|
|
|
|
continue
|
|
|
|
|
|
|
|
# if the score is lower than the threshold, skip
|
|
|
|
if obj_settings.get('threshold', 0) > obj['score']:
|
2019-12-08 14:16:30 +01:00
|
|
|
continue
|
2019-04-14 18:58:02 +02:00
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
# compute the coordinates of the object and make sure
|
2019-07-15 13:08:39 +02:00
|
|
|
# the location isnt outside the bounds of the image (can happen from rounding)
|
2019-04-14 18:58:02 +02:00
|
|
|
y_location = min(int(obj['ymax']), len(self.mask)-1)
|
2019-07-15 13:08:39 +02:00
|
|
|
x_location = min(int((obj['xmax']-obj['xmin'])/2.0)+obj['xmin'], len(self.mask[0])-1)
|
2019-04-14 18:58:02 +02:00
|
|
|
|
2019-12-14 22:18:21 +01:00
|
|
|
# if the object is in a masked location, don't add it to detected objects
|
2019-04-14 18:58:02 +02:00
|
|
|
if self.mask[y_location][x_location] == [0]:
|
|
|
|
continue
|
2019-03-30 02:49:27 +01:00
|
|
|
|
|
|
|
self.detected_objects.append(obj)
|
|
|
|
|
|
|
|
with self.objects_parsed:
|
|
|
|
self.objects_parsed.notify_all()
|
2019-12-14 22:18:21 +01:00
|
|
|
|
|
|
|
def get_best(self, label):
|
|
|
|
return self.best_frames.best_frames.get(label)
|
2019-03-30 03:02:40 +01:00
|
|
|
|
|
|
|
def get_current_frame_with_objects(self):
|
|
|
|
# make a copy of the current detected objects
|
|
|
|
detected_objects = self.detected_objects.copy()
|
|
|
|
# lock and make a copy of the current frame
|
|
|
|
with self.frame_lock:
|
2019-07-18 05:25:59 +02:00
|
|
|
frame = self.current_frame.copy()
|
2019-12-08 15:55:19 +01:00
|
|
|
frame_time = self.frame_time.value
|
2019-12-15 14:25:40 +01:00
|
|
|
|
|
|
|
if frame_time == self.cached_frame_with_objects['frame_time']:
|
|
|
|
return self.cached_frame_with_objects['frame_bytes']
|
2019-03-30 03:02:40 +01:00
|
|
|
|
|
|
|
# draw the bounding boxes on the screen
|
|
|
|
for obj in detected_objects:
|
2019-12-14 23:38:01 +01:00
|
|
|
draw_box_with_label(frame, obj['xmin'], obj['ymin'], obj['xmax'], obj['ymax'], obj['name'], obj['score'], obj['area'])
|
2019-03-30 03:02:40 +01:00
|
|
|
|
|
|
|
for region in self.regions:
|
|
|
|
color = (255,255,255)
|
|
|
|
cv2.rectangle(frame, (region['x_offset'], region['y_offset']),
|
|
|
|
(region['x_offset']+region['size'], region['y_offset']+region['size']),
|
|
|
|
color, 2)
|
2019-12-08 15:55:19 +01:00
|
|
|
|
|
|
|
# print a timestamp
|
|
|
|
time_to_show = datetime.datetime.fromtimestamp(frame_time).strftime("%m/%d/%Y %H:%M:%S")
|
|
|
|
cv2.putText(frame, time_to_show, (10, 30), cv2.FONT_HERSHEY_SIMPLEX, fontScale=.8, color=(255, 255, 255), thickness=2)
|
2019-03-30 03:02:40 +01:00
|
|
|
|
2019-06-02 14:29:50 +02:00
|
|
|
# convert to BGR
|
2019-03-30 03:02:40 +01:00
|
|
|
frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
|
|
|
|
|
2019-12-15 14:25:40 +01:00
|
|
|
# encode the image into a jpg
|
|
|
|
ret, jpg = cv2.imencode('.jpg', frame)
|
|
|
|
|
|
|
|
frame_bytes = jpg.tobytes()
|
|
|
|
|
|
|
|
self.cached_frame_with_objects = {
|
|
|
|
'frame_bytes': frame_bytes,
|
|
|
|
'frame_time': frame_time
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame_bytes
|
2019-03-30 02:49:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
2019-07-15 13:08:39 +02:00
|
|
|
|