blakeblackshear.frigate/frigate/video.py

358 lines
13 KiB
Python
Raw Normal View History

2019-03-30 02:49:27 +01:00
import os
2019-02-26 03:27:02 +01:00
import time
import datetime
import cv2
import queue
import threading
2020-11-04 04:26:39 +01:00
import logging
2019-03-30 02:49:27 +01:00
import ctypes
import multiprocessing as mp
import subprocess as sp
2019-05-10 13:19:39 +02:00
import numpy as np
2020-01-11 20:22:56 +01:00
import copy
2019-12-31 21:59:22 +01:00
import itertools
import json
import base64
from typing import Dict, List
from collections import defaultdict
2020-11-03 15:15:58 +01:00
from frigate.config import CameraConfig
2020-10-11 04:28:12 +02:00
from frigate.util import draw_box_with_label, yuv_region_2_rgb, area, calculate_region, clipped, intersection_over_union, intersection, EventsPerSecond, listen, FrameManager, SharedMemoryFrameManager
2020-02-16 04:07:54 +01:00
from frigate.objects import ObjectTracker
from frigate.edgetpu import RemoteObjectDetector
from frigate.motion import MotionDetector
2019-02-26 03:27:02 +01:00
2020-11-04 04:26:39 +01:00
logger = logging.getLogger(__name__)
def filtered(obj, objects_to_track, object_filters, mask=None):
2020-02-16 04:07:54 +01:00
object_name = obj[0]
if not object_name in objects_to_track:
return True
if object_name in object_filters:
obj_settings = object_filters[object_name]
# if the min area is larger than the
# detected object, don't add it to detected objects
2020-11-03 15:15:58 +01:00
if obj_settings.min_area > obj[3]:
2020-02-16 04:07:54 +01:00
return True
# if the detected object is larger than the
# max area, don't add it to detected objects
2020-11-03 15:15:58 +01:00
if obj_settings.max_area < obj[3]:
2020-02-16 04:07:54 +01:00
return True
2020-09-07 19:17:42 +02:00
# if the score is lower than the min_score, skip
2020-11-03 15:15:58 +01:00
if obj_settings.min_score > obj[1]:
2020-02-16 04:07:54 +01:00
return True
# compute the coordinates of the object and make sure
# the location isnt outside the bounds of the image (can happen from rounding)
y_location = min(int(obj[2][3]), len(mask)-1)
x_location = min(int((obj[2][2]-obj[2][0])/2.0)+obj[2][0], len(mask[0])-1)
# if the object is in a masked location, don't add it to detected objects
2020-09-11 00:37:58 +02:00
if (not mask is None) and (mask[y_location][x_location] == 0):
2020-02-16 04:07:54 +01:00
return True
2020-09-07 19:17:42 +02:00
return False
2020-02-16 04:07:54 +01:00
def create_tensor_input(frame, region):
2020-10-11 04:28:12 +02:00
cropped_frame = yuv_region_2_rgb(frame, region)
2019-03-30 02:49:27 +01:00
2020-02-16 04:07:54 +01:00
# 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]
return np.expand_dims(cropped_frame, axis=0)
def start_or_restart_ffmpeg(ffmpeg_cmd, frame_size, ffmpeg_process=None):
if not ffmpeg_process is None:
2020-11-04 04:26:39 +01:00
logger.info("Terminating the existing ffmpeg process...")
ffmpeg_process.terminate()
try:
2020-11-04 04:26:39 +01:00
logger.info("Waiting for ffmpeg to exit gracefully...")
ffmpeg_process.communicate(timeout=30)
except sp.TimeoutExpired:
2020-11-04 04:26:39 +01:00
logger.info("FFmpeg didnt exit. Force killing...")
ffmpeg_process.kill()
ffmpeg_process.communicate()
2020-03-13 21:50:27 +01:00
ffmpeg_process = None
2020-11-04 04:26:39 +01:00
logger.info("Creating ffmpeg process...")
logger.info(" ".join(ffmpeg_cmd))
process = sp.Popen(ffmpeg_cmd, stdout = sp.PIPE, stdin = sp.DEVNULL, bufsize=frame_size*10, start_new_session=True)
return process
def capture_frames(ffmpeg_process, camera_name, frame_shape, frame_manager: FrameManager,
2020-11-01 17:55:11 +01:00
frame_queue, fps:mp.Value, skipped_fps: mp.Value, current_frame: mp.Value):
2020-11-03 15:15:58 +01:00
frame_size = frame_shape[0] * frame_shape[1]
2020-10-25 16:05:21 +01:00
frame_rate = EventsPerSecond()
2020-10-26 13:59:22 +01:00
frame_rate.start()
2020-10-25 16:05:21 +01:00
skipped_eps = EventsPerSecond()
skipped_eps.start()
while True:
2020-10-25 16:05:21 +01:00
fps.value = frame_rate.eps()
skipped_fps = skipped_eps.eps()
2020-09-07 19:17:42 +02:00
current_frame.value = datetime.datetime.now().timestamp()
2020-10-24 18:36:04 +02:00
frame_name = f"{camera_name}{current_frame.value}"
frame_buffer = frame_manager.create(frame_name, frame_size)
try:
frame_buffer[:] = ffmpeg_process.stdout.read(frame_size)
except:
2020-11-04 04:26:39 +01:00
logger.info(f"{camera_name}: ffmpeg sent a broken frame. something is wrong.")
2020-10-24 18:36:04 +02:00
if ffmpeg_process.poll() != None:
2020-11-04 04:26:39 +01:00
logger.info(f"{camera_name}: ffmpeg process is not running. exiting capture thread...")
2020-10-24 18:36:04 +02:00
frame_manager.delete(frame_name)
break
continue
2020-10-25 16:05:21 +01:00
frame_rate.update()
# if the queue is full, skip this frame
if frame_queue.full():
2020-10-25 16:05:21 +01:00
skipped_eps.update()
2020-10-24 18:36:04 +02:00
frame_manager.delete(frame_name)
continue
2020-10-24 18:36:04 +02:00
# close the frame
frame_manager.close(frame_name)
# add to the queue
2020-09-07 19:17:42 +02:00
frame_queue.put(current_frame.value)
2020-10-25 16:05:21 +01:00
class CameraWatchdog(threading.Thread):
2020-11-01 17:55:11 +01:00
def __init__(self, name, config, frame_queue, camera_fps, ffmpeg_pid):
2020-10-25 16:05:21 +01:00
threading.Thread.__init__(self)
self.name = name
self.config = config
self.capture_thread = None
self.ffmpeg_process = None
self.camera_fps = camera_fps
self.ffmpeg_pid = ffmpeg_pid
2020-10-25 16:05:21 +01:00
self.frame_queue = frame_queue
2020-11-03 15:15:58 +01:00
self.frame_shape = self.config.frame_shape_yuv
self.frame_size = self.frame_shape[0] * self.frame_shape[1]
2020-10-25 16:05:21 +01:00
def run(self):
self.start_ffmpeg()
time.sleep(10)
while True:
now = datetime.datetime.now().timestamp()
if not self.capture_thread.is_alive():
self.start_ffmpeg()
elif now - self.capture_thread.current_frame.value > 5:
2020-11-04 04:26:39 +01:00
logger.info(f"No frames received from {self.name} in 5 seconds. Exiting ffmpeg...")
2020-10-25 16:05:21 +01:00
self.ffmpeg_process.terminate()
try:
2020-11-04 04:26:39 +01:00
logger.info("Waiting for ffmpeg to exit gracefully...")
2020-10-25 16:05:21 +01:00
self.ffmpeg_process.communicate(timeout=30)
except sp.TimeoutExpired:
2020-11-04 04:26:39 +01:00
logger.info("FFmpeg didnt exit. Force killing...")
2020-10-25 16:05:21 +01:00
self.ffmpeg_process.kill()
self.ffmpeg_process.communicate()
# wait a bit before checking again
time.sleep(10)
def start_ffmpeg(self):
2020-11-03 15:15:58 +01:00
self.ffmpeg_process = start_or_restart_ffmpeg(self.config.ffmpeg_cmd, self.frame_size)
2020-11-01 17:55:11 +01:00
self.ffmpeg_pid.value = self.ffmpeg_process.pid
self.capture_thread = CameraCapture(self.name, self.ffmpeg_process, self.frame_shape, self.frame_queue,
self.camera_fps)
self.capture_thread.start()
2020-10-25 16:05:21 +01:00
class CameraCapture(threading.Thread):
2020-11-01 17:55:11 +01:00
def __init__(self, name, ffmpeg_process, frame_shape, frame_queue, fps):
threading.Thread.__init__(self)
self.name = name
self.frame_shape = frame_shape
self.frame_queue = frame_queue
self.fps = fps
self.skipped_fps = EventsPerSecond()
self.frame_manager = SharedMemoryFrameManager()
self.ffmpeg_process = ffmpeg_process
2020-09-07 19:17:42 +02:00
self.current_frame = mp.Value('d', 0.0)
self.last_frame = 0
def run(self):
self.skipped_fps.start()
2020-11-01 17:55:11 +01:00
capture_frames(self.ffmpeg_process, self.name, self.frame_shape, self.frame_manager, self.frame_queue,
self.fps, self.skipped_fps, self.current_frame)
2020-11-03 15:15:58 +01:00
def capture_camera(name, config: CameraConfig, process_info):
2020-10-25 16:05:21 +01:00
frame_queue = process_info['frame_queue']
2020-11-01 17:55:11 +01:00
camera_watchdog = CameraWatchdog(name, config, frame_queue, process_info['camera_fps'], process_info['ffmpeg_pid'])
2020-10-25 16:05:21 +01:00
camera_watchdog.start()
camera_watchdog.join()
2020-11-03 15:15:58 +01:00
def track_camera(name, config: CameraConfig, detection_queue, result_connection, detected_objects_queue, process_info):
listen()
2020-02-16 04:07:54 +01:00
2020-10-25 16:05:21 +01:00
frame_queue = process_info['frame_queue']
2020-11-03 15:15:58 +01:00
frame_shape = config.frame_shape
objects_to_track = config.objects.track
object_filters = config.objects.filters
mask = config.mask
2020-02-16 04:07:54 +01:00
motion_detector = MotionDetector(frame_shape, mask, resize_factor=6)
object_detector = RemoteObjectDetector(name, '/labelmap.txt', detection_queue, result_connection)
2020-02-16 04:07:54 +01:00
object_tracker = ObjectTracker(10)
frame_manager = SharedMemoryFrameManager()
process_frames(name, frame_queue, frame_shape, frame_manager, motion_detector, object_detector,
2020-11-01 17:37:17 +01:00
object_tracker, detected_objects_queue, process_info, objects_to_track, object_filters, mask)
2020-11-04 04:26:39 +01:00
logger.info(f"{name}: exiting subprocess")
def reduce_boxes(boxes):
if len(boxes) == 0:
return []
reduced_boxes = cv2.groupRectangles([list(b) for b in itertools.chain(boxes, boxes)], 1, 0.2)[0]
return [tuple(b) for b in reduced_boxes]
def detect(object_detector, frame, region, objects_to_track, object_filters, mask):
tensor_input = create_tensor_input(frame, region)
detections = []
region_detections = object_detector.detect(tensor_input)
for d in region_detections:
box = d[2]
size = region[2]-region[0]
x_min = int((box[1] * size) + region[0])
y_min = int((box[0] * size) + region[1])
x_max = int((box[3] * size) + region[0])
y_max = int((box[2] * size) + region[1])
det = (d[0],
d[1],
(x_min, y_min, x_max, y_max),
(x_max-x_min)*(y_max-y_min),
region)
# apply object filters
if filtered(det, objects_to_track, object_filters, mask):
continue
detections.append(det)
return detections
def process_frames(camera_name: str, frame_queue: mp.Queue, frame_shape,
frame_manager: FrameManager, motion_detector: MotionDetector,
object_detector: RemoteObjectDetector, object_tracker: ObjectTracker,
2020-10-25 16:05:21 +01:00
detected_objects_queue: mp.Queue, process_info: Dict,
2020-11-03 15:15:58 +01:00
objects_to_track: List[str], object_filters, mask,
exit_on_empty: bool = False):
2020-10-25 16:05:21 +01:00
fps = process_info['process_fps']
detection_fps = process_info['detection_fps']
current_frame_time = process_info['detection_frame']
2020-02-16 04:07:54 +01:00
fps_tracker = EventsPerSecond()
fps_tracker.start()
2020-02-16 04:07:54 +01:00
while True:
2020-11-01 17:37:17 +01:00
if exit_on_empty and frame_queue.empty():
2020-11-04 04:26:39 +01:00
logger.info(f"Exiting track_objects...")
2020-11-01 17:37:17 +01:00
break
try:
frame_time = frame_queue.get(True, 10)
except queue.Empty:
continue
current_frame_time.value = frame_time
2020-10-10 17:07:14 +02:00
frame = frame_manager.get(f"{camera_name}{frame_time}", (frame_shape[0]*3//2, frame_shape[1]))
if frame is None:
2020-11-04 04:26:39 +01:00
logger.info(f"{camera_name}: frame {frame_time} is not in memory store.")
continue
2020-02-16 04:07:54 +01:00
# look for motion
motion_boxes = motion_detector.detect(frame)
tracked_object_boxes = [obj['box'] for obj in object_tracker.tracked_objects.values()]
# combine motion boxes with known locations of existing objects
combined_boxes = reduce_boxes(motion_boxes + tracked_object_boxes)
# compute regions
regions = [calculate_region(frame_shape, a[0], a[1], a[2], a[3], 1.2)
for a in combined_boxes]
# combine overlapping regions
combined_regions = reduce_boxes(regions)
2020-02-16 04:07:54 +01:00
# re-compute regions
regions = [calculate_region(frame_shape, a[0], a[1], a[2], a[3], 1.0)
for a in combined_regions]
2020-10-10 17:07:14 +02:00
2020-02-16 04:07:54 +01:00
# resize regions and detect
detections = []
for region in regions:
2020-10-11 04:28:12 +02:00
detections.extend(detect(object_detector, frame, region, objects_to_track, object_filters, mask))
2020-02-16 04:07:54 +01:00
#########
# merge objects, check for clipped objects and look again up to 4 times
2020-02-16 04:07:54 +01:00
#########
refining = True
refine_count = 0
while refining and refine_count < 4:
refining = False
# group by name
detected_object_groups = defaultdict(lambda: [])
for detection in detections:
detected_object_groups[detection[0]].append(detection)
selected_objects = []
for group in detected_object_groups.values():
# apply non-maxima suppression to suppress weak, overlapping bounding boxes
boxes = [(o[2][0], o[2][1], o[2][2]-o[2][0], o[2][3]-o[2][1])
for o in group]
confidences = [o[1] for o in group]
idxs = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
for index in idxs:
obj = group[index[0]]
if clipped(obj, frame_shape):
2020-02-16 04:07:54 +01:00
box = obj[2]
# calculate a new region that will hopefully get the entire object
region = calculate_region(frame_shape,
box[0], box[1],
box[2], box[3])
2020-10-11 04:28:12 +02:00
selected_objects.extend(detect(object_detector, frame, region, objects_to_track, object_filters, mask))
2020-02-16 04:07:54 +01:00
refining = True
else:
selected_objects.append(obj)
2020-02-16 04:07:54 +01:00
# set the detections list to only include top, complete objects
# and new detections
detections = selected_objects
if refining:
refine_count += 1
2020-02-16 04:07:54 +01:00
# now that we have refined our detections, we need to track objects
object_tracker.match_and_update(frame_time, detections)
2020-10-24 18:36:04 +02:00
# add to the queue if not full
if(detected_objects_queue.full()):
frame_manager.delete(f"{camera_name}{frame_time}")
continue
else:
fps_tracker.update()
fps.value = fps_tracker.eps()
detected_objects_queue.put((camera_name, frame_time, object_tracker.tracked_objects))
detection_fps.value = object_detector.fps.eps()
frame_manager.close(f"{camera_name}{frame_time}")