skip frames in the capture thread instead

This commit is contained in:
Blake Blackshear 2020-04-19 10:07:27 -05:00
parent 15b4024715
commit 760e1ffe1d
3 changed files with 37 additions and 45 deletions

View File

@ -110,13 +110,6 @@ cameras:
################ ################
take_frame: 1 take_frame: 1
################
# The expected framerate for the camera. Frigate will try and ensure it maintains this framerate
# by dropping frames as necessary. Setting this lower than the actual framerate will allow frigate
# to process every frame at the expense of realtime processing.
################
fps: 5
################ ################
# Configuration for the snapshots in the debug view and mqtt # Configuration for the snapshots in the debug view and mqtt
################ ################

View File

@ -105,14 +105,13 @@ class CameraWatchdog(threading.Thread):
process = camera_process['process'] process = camera_process['process']
if not process.is_alive(): if not process.is_alive():
print(f"Track process for {name} is not alive. Starting again...") print(f"Track process for {name} is not alive. Starting again...")
camera_process['fps'].value = float(self.config[name]['fps']) camera_process['process_fps'].value = 0.0
camera_process['skipped_fps'].value = 0.0
camera_process['detection_fps'].value = 0.0 camera_process['detection_fps'].value = 0.0
camera_process['read_start'].value = 0.0 camera_process['read_start'].value = 0.0
process = mp.Process(target=track_camera, args=(name, self.config[name], GLOBAL_OBJECT_CONFIG, camera_process['frame_queue'], process = mp.Process(target=track_camera, args=(name, self.config[name], GLOBAL_OBJECT_CONFIG, camera_process['frame_queue'],
camera_process['frame_shape'], self.tflite_process.detection_queue, self.tracked_objects_queue, camera_process['frame_shape'], self.tflite_process.detection_queue, self.tracked_objects_queue,
camera_process['fps'], camera_process['skipped_fps'], camera_process['detection_fps'], camera_process['process_fps'], camera_process['detection_fps'],
camera_process['read_start'])) camera_process['read_start'], camera_process['detection_frame']))
process.daemon = True process.daemon = True
camera_process['process'] = process camera_process['process'] = process
process.start() process.start()
@ -123,7 +122,7 @@ class CameraWatchdog(threading.Thread):
frame_size = frame_shape[0] * frame_shape[1] * frame_shape[2] frame_size = frame_shape[0] * frame_shape[1] * frame_shape[2]
ffmpeg_process = start_or_restart_ffmpeg(camera_process['ffmpeg_cmd'], frame_size) ffmpeg_process = start_or_restart_ffmpeg(camera_process['ffmpeg_cmd'], frame_size)
camera_capture = CameraCapture(name, ffmpeg_process, frame_shape, camera_process['frame_queue'], camera_capture = CameraCapture(name, ffmpeg_process, frame_shape, camera_process['frame_queue'],
camera_process['take_frame'], camera_process['camera_fps']) camera_process['take_frame'], camera_process['camera_fps'], camera_process['detection_frame'])
camera_capture.start() camera_capture.start()
camera_process['ffmpeg_process'] = ffmpeg_process camera_process['ffmpeg_process'] = ffmpeg_process
camera_process['capture_thread'] = camera_capture camera_process['capture_thread'] = camera_capture
@ -193,20 +192,21 @@ def main():
frame_size = frame_shape[0] * frame_shape[1] * frame_shape[2] frame_size = frame_shape[0] * frame_shape[1] * frame_shape[2]
take_frame = config.get('take_frame', 1) take_frame = config.get('take_frame', 1)
detection_frame = mp.Value('d', 0.0)
ffmpeg_process = start_or_restart_ffmpeg(ffmpeg_cmd, frame_size) ffmpeg_process = start_or_restart_ffmpeg(ffmpeg_cmd, frame_size)
frame_queue = mp.SimpleQueue() frame_queue = mp.SimpleQueue()
camera_fps = EventsPerSecond() camera_fps = EventsPerSecond()
camera_fps.start() camera_fps.start()
camera_capture = CameraCapture(name, ffmpeg_process, frame_shape, frame_queue, take_frame, camera_fps) camera_capture = CameraCapture(name, ffmpeg_process, frame_shape, frame_queue, take_frame, camera_fps, detection_frame)
camera_capture.start() camera_capture.start()
camera_processes[name] = { camera_processes[name] = {
'camera_fps': camera_fps, 'camera_fps': camera_fps,
'take_frame': take_frame, 'take_frame': take_frame,
'fps': mp.Value('d', float(config['fps'])), 'process_fps': mp.Value('d', 0.0),
'skipped_fps': mp.Value('d', 0.0),
'detection_fps': mp.Value('d', 0.0), 'detection_fps': mp.Value('d', 0.0),
'detection_frame': mp.Value('d', 0.0), 'detection_frame': detection_frame,
'read_start': mp.Value('d', 0.0), 'read_start': mp.Value('d', 0.0),
'ffmpeg_process': ffmpeg_process, 'ffmpeg_process': ffmpeg_process,
'ffmpeg_cmd': ffmpeg_cmd, 'ffmpeg_cmd': ffmpeg_cmd,
@ -216,8 +216,8 @@ def main():
} }
camera_process = mp.Process(target=track_camera, args=(name, config, GLOBAL_OBJECT_CONFIG, frame_queue, frame_shape, camera_process = mp.Process(target=track_camera, args=(name, config, GLOBAL_OBJECT_CONFIG, frame_queue, frame_shape,
tflite_process.detection_queue, tracked_objects_queue, camera_processes[name]['fps'], tflite_process.detection_queue, tracked_objects_queue, camera_processes[name]['process_fps'],
camera_processes[name]['skipped_fps'], camera_processes[name]['detection_fps'], camera_processes[name]['detection_fps'],
camera_processes[name]['read_start'], camera_processes[name]['detection_frame'])) camera_processes[name]['read_start'], camera_processes[name]['detection_frame']))
camera_process.daemon = True camera_process.daemon = True
camera_processes[name]['process'] = camera_process camera_processes[name]['process'] = camera_process
@ -267,15 +267,17 @@ def main():
for name, camera_stats in camera_processes.items(): for name, camera_stats in camera_processes.items():
total_detection_fps += camera_stats['detection_fps'].value total_detection_fps += camera_stats['detection_fps'].value
capture_thread = camera_stats['capture_thread']
stats[name] = { stats[name] = {
'fps': round(camera_stats['fps'].value, 2), 'camera_fps': round(capture_thread.fps.eps(), 2),
'skipped_fps': round(camera_stats['skipped_fps'].value, 2), 'process_fps': round(camera_stats['process_fps'].value, 2),
'skipped_fps': round(capture_thread.skipped_fps.eps(), 2),
'detection_fps': round(camera_stats['detection_fps'].value, 2), 'detection_fps': round(camera_stats['detection_fps'].value, 2),
'read_start': camera_stats['read_start'].value, 'read_start': camera_stats['read_start'].value,
'pid': camera_stats['process'].pid, 'pid': camera_stats['process'].pid,
'ffmpeg_pid': camera_stats['ffmpeg_process'].pid, 'ffmpeg_pid': camera_stats['ffmpeg_process'].pid,
'frame_info': { 'frame_info': {
'read': camera_stats['capture_thread'].current_frame, 'read': capture_thread.current_frame,
'detect': camera_stats['detection_frame'].value, 'detect': camera_stats['detection_frame'].value,
'process': object_processor.camera_data[name]['current_frame_time'] 'process': object_processor.camera_data[name]['current_frame_time']
} }

View File

@ -115,7 +115,7 @@ def start_or_restart_ffmpeg(ffmpeg_cmd, frame_size, ffmpeg_process=None):
return process return process
class CameraCapture(threading.Thread): class CameraCapture(threading.Thread):
def __init__(self, name, ffmpeg_process, frame_shape, frame_queue, take_frame, fps): def __init__(self, name, ffmpeg_process, frame_shape, frame_queue, take_frame, fps, detection_frame):
threading.Thread.__init__(self) threading.Thread.__init__(self)
self.name = name self.name = name
self.frame_shape = frame_shape self.frame_shape = frame_shape
@ -123,12 +123,16 @@ class CameraCapture(threading.Thread):
self.frame_queue = frame_queue self.frame_queue = frame_queue
self.take_frame = take_frame self.take_frame = take_frame
self.fps = fps self.fps = fps
self.skipped_fps = EventsPerSecond()
self.plasma_client = PlasmaManager() self.plasma_client = PlasmaManager()
self.ffmpeg_process = ffmpeg_process self.ffmpeg_process = ffmpeg_process
self.current_frame = 0 self.current_frame = 0
self.last_frame = 0
self.detection_frame = detection_frame
def run(self): def run(self):
frame_num = 0 frame_num = 0
self.skipped_fps.start()
while True: while True:
if self.ffmpeg_process.poll() != None: if self.ffmpeg_process.poll() != None:
print(f"{self.name}: ffmpeg process is not running. exiting capture thread...") print(f"{self.name}: ffmpeg process is not running. exiting capture thread...")
@ -141,8 +145,16 @@ class CameraCapture(threading.Thread):
print(f"{self.name}: ffmpeg didnt return a frame. something is wrong.") print(f"{self.name}: ffmpeg didnt return a frame. something is wrong.")
continue continue
self.fps.update()
frame_num += 1 frame_num += 1
if (frame_num % self.take_frame) != 0: if (frame_num % self.take_frame) != 0:
self.skipped_fps.update()
continue
# if the detection process is more than 1 second behind, skip this frame
if self.detection_frame.value > 0.0 and (self.last_frame - self.detection_frame.value) > 1:
self.skipped_fps.update()
continue continue
# put the frame in the plasma store # put the frame in the plasma store
@ -153,13 +165,14 @@ class CameraCapture(threading.Thread):
) )
# add to the queue # add to the queue
self.frame_queue.put(self.current_frame) self.frame_queue.put(self.current_frame)
self.last_frame = self.current_frame
self.fps.update() def track_camera(name, config, global_objects_config, frame_queue, frame_shape, detection_queue, detected_objects_queue, fps, detection_fps, read_start, detection_frame):
def track_camera(name, config, global_objects_config, frame_queue, frame_shape, detection_queue, detected_objects_queue, fps, skipped_fps, detection_fps, read_start, detection_frame):
print(f"Starting process for {name}: {os.getpid()}") print(f"Starting process for {name}: {os.getpid()}")
listen() listen()
detection_frame.value = 0.0
# Merge the tracked object config with the global config # Merge the tracked object config with the global config
camera_objects_config = config.get('objects', {}) camera_objects_config = config.get('objects', {})
# combine tracked objects lists # combine tracked objects lists
@ -172,8 +185,6 @@ def track_camera(name, config, global_objects_config, frame_queue, frame_shape,
for obj in objects_with_config: for obj in objects_with_config:
object_filters[obj] = {**global_object_filters.get(obj, {}), **camera_object_filters.get(obj, {})} object_filters[obj] = {**global_object_filters.get(obj, {}), **camera_object_filters.get(obj, {})}
expected_fps = config['fps']
frame = np.zeros(frame_shape, np.uint8) frame = np.zeros(frame_shape, np.uint8)
# load in the mask for object detection # load in the mask for object detection
@ -192,12 +203,9 @@ def track_camera(name, config, global_objects_config, frame_queue, frame_shape,
object_tracker = ObjectTracker(10) object_tracker = ObjectTracker(10)
plasma_client = PlasmaManager() plasma_client = PlasmaManager()
frame_num = 0
avg_wait = 0.0 avg_wait = 0.0
fps_tracker = EventsPerSecond() fps_tracker = EventsPerSecond()
skipped_fps_tracker = EventsPerSecond()
fps_tracker.start() fps_tracker.start()
skipped_fps_tracker.start()
object_detector.fps.start() object_detector.fps.start()
while True: while True:
read_start.value = datetime.datetime.now().timestamp() read_start.value = datetime.datetime.now().timestamp()
@ -206,31 +214,20 @@ def track_camera(name, config, global_objects_config, frame_queue, frame_shape,
read_start.value = 0.0 read_start.value = 0.0
avg_wait = (avg_wait*99+duration)/100 avg_wait = (avg_wait*99+duration)/100
detection_frame.value = frame_time detection_frame.value = frame_time
fps_tracker.update()
fps.value = fps_tracker.eps()
detection_fps.value = object_detector.fps.eps()
# Get frame from plasma store # Get frame from plasma store
frame = plasma_client.get(f"{name}{frame_time}") frame = plasma_client.get(f"{name}{frame_time}")
if frame is plasma.ObjectNotAvailable: if frame is plasma.ObjectNotAvailable:
skipped_fps_tracker.update()
skipped_fps.value = skipped_fps_tracker.eps()
continue continue
fps_tracker.update()
fps.value = fps_tracker.eps()
detection_fps.value = object_detector.fps.eps()
# look for motion # look for motion
motion_boxes = motion_detector.detect(frame) motion_boxes = motion_detector.detect(frame)
# skip object detection if we are below the min_fps and wait time is less than half the average
if frame_num > 100 and fps.value < expected_fps-1 and duration < 0.5*avg_wait:
skipped_fps_tracker.update()
skipped_fps.value = skipped_fps_tracker.eps()
plasma_client.delete(f"{name}{frame_time}")
continue
skipped_fps.value = skipped_fps_tracker.eps()
tracked_objects = object_tracker.tracked_objects.values() tracked_objects = object_tracker.tracked_objects.values()
# merge areas of motion that intersect with a known tracked object into a single area to look at # merge areas of motion that intersect with a known tracked object into a single area to look at