2020-11-01 16:56:33 +01:00
|
|
|
import faulthandler; faulthandler.enable()
|
2020-11-01 15:06:15 +01:00
|
|
|
import os
|
2020-10-18 15:24:54 +02:00
|
|
|
import json
|
2020-11-04 04:26:39 +01:00
|
|
|
import logging
|
2020-11-01 15:06:15 +01:00
|
|
|
import yaml
|
2020-02-16 04:07:54 +01:00
|
|
|
import multiprocessing as mp
|
2020-11-04 04:26:39 +01:00
|
|
|
import sys
|
2020-11-01 15:06:15 +01:00
|
|
|
|
2020-11-04 04:26:39 +01:00
|
|
|
from logging.handlers import QueueHandler
|
2020-11-01 23:37:18 +01:00
|
|
|
from playhouse.sqlite_ext import SqliteExtDatabase
|
2020-11-01 16:56:33 +01:00
|
|
|
from typing import Dict, List
|
2019-01-26 15:02:59 +01:00
|
|
|
|
2020-11-03 15:15:58 +01:00
|
|
|
from frigate.config import FrigateConfig
|
2020-11-01 16:56:33 +01:00
|
|
|
from frigate.edgetpu import EdgeTPUProcess
|
2020-11-01 21:58:51 +01:00
|
|
|
from frigate.events import EventProcessor
|
2020-11-01 15:06:15 +01:00
|
|
|
from frigate.http import create_app
|
2020-11-04 04:26:39 +01:00
|
|
|
from frigate.log import root_configurer, log_process
|
2020-11-01 15:06:15 +01:00
|
|
|
from frigate.models import Event
|
|
|
|
from frigate.mqtt import create_mqtt_client
|
2020-11-01 17:10:43 +01:00
|
|
|
from frigate.object_processing import TrackedObjectProcessor
|
2020-11-03 15:15:58 +01:00
|
|
|
from frigate.video import track_camera, capture_camera
|
2020-11-01 22:45:04 +01:00
|
|
|
from frigate.watchdog import FrigateWatchdog
|
2020-11-01 17:10:43 +01:00
|
|
|
|
2020-11-04 04:26:39 +01:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
|
|
|
cli = sys.modules['flask.cli']
|
|
|
|
cli.show_server_banner = lambda *x: None
|
|
|
|
|
2020-11-01 13:17:44 +01:00
|
|
|
class FrigateApp():
|
2020-11-01 16:56:33 +01:00
|
|
|
def __init__(self):
|
|
|
|
self.stop_event = mp.Event()
|
2020-11-03 15:15:58 +01:00
|
|
|
self.config: FrigateConfig = None
|
2020-11-01 16:56:33 +01:00
|
|
|
self.detection_queue = mp.Queue()
|
2020-11-03 15:15:58 +01:00
|
|
|
self.detectors: Dict[str, EdgeTPUProcess] = {}
|
|
|
|
self.detection_out_events: Dict[str, mp.Event] = {}
|
2020-11-01 16:56:33 +01:00
|
|
|
self.detection_shms: List[mp.shared_memory.SharedMemory] = []
|
2020-11-04 04:26:39 +01:00
|
|
|
self.log_queue = mp.Queue()
|
2020-11-01 21:58:51 +01:00
|
|
|
self.camera_metrics = {}
|
2020-11-01 13:17:44 +01:00
|
|
|
|
2020-11-04 04:26:39 +01:00
|
|
|
def init_logger(self):
|
|
|
|
self.log_process = mp.Process(target=log_process, args=(self.log_queue,))
|
|
|
|
self.log_process.start()
|
|
|
|
root_configurer(self.log_queue)
|
|
|
|
|
2020-11-01 13:17:44 +01:00
|
|
|
def init_config(self):
|
|
|
|
config_file = os.environ.get('CONFIG_FILE', '/config/config.yml')
|
2020-11-03 15:15:58 +01:00
|
|
|
self.config = FrigateConfig(config_file=config_file)
|
2020-11-01 13:17:44 +01:00
|
|
|
|
2020-11-03 15:15:58 +01:00
|
|
|
for camera_name in self.config.cameras.keys():
|
2020-11-01 21:58:51 +01:00
|
|
|
# create camera_metrics
|
|
|
|
self.camera_metrics[camera_name] = {
|
|
|
|
'camera_fps': mp.Value('d', 0.0),
|
|
|
|
'skipped_fps': mp.Value('d', 0.0),
|
|
|
|
'process_fps': mp.Value('d', 0.0),
|
|
|
|
'detection_fps': mp.Value('d', 0.0),
|
|
|
|
'detection_frame': mp.Value('d', 0.0),
|
|
|
|
'read_start': mp.Value('d', 0.0),
|
|
|
|
'ffmpeg_pid': mp.Value('i', 0),
|
|
|
|
'frame_queue': mp.Queue(maxsize=2)
|
|
|
|
}
|
|
|
|
|
2020-11-01 17:10:43 +01:00
|
|
|
def init_queues(self):
|
|
|
|
# Queue for clip processing
|
|
|
|
self.event_queue = mp.Queue()
|
|
|
|
|
|
|
|
# Queue for cameras to push tracked objects to
|
2020-11-03 15:15:58 +01:00
|
|
|
self.detected_frames_queue = mp.Queue(maxsize=len(self.config.cameras.keys())*2)
|
2020-11-01 17:10:43 +01:00
|
|
|
|
2020-11-01 13:17:44 +01:00
|
|
|
def init_database(self):
|
2020-11-03 15:15:58 +01:00
|
|
|
self.db = SqliteExtDatabase(f"/{os.path.join(self.config.save_clips.clips_dir, 'frigate.db')}")
|
2020-11-01 15:06:15 +01:00
|
|
|
models = [Event]
|
|
|
|
self.db.bind(models)
|
|
|
|
self.db.create_tables(models, safe=True)
|
|
|
|
|
|
|
|
def init_web_server(self):
|
2020-11-01 22:37:51 +01:00
|
|
|
self.flask_app = create_app(self.config, self.db, self.camera_metrics, self.detectors, self.detected_frames_processor)
|
2020-11-01 13:17:44 +01:00
|
|
|
|
|
|
|
def init_mqtt(self):
|
2020-11-03 15:15:58 +01:00
|
|
|
self.mqtt_client = create_mqtt_client(self.config.mqtt)
|
2020-11-01 13:17:44 +01:00
|
|
|
|
|
|
|
def start_detectors(self):
|
2020-11-03 15:15:58 +01:00
|
|
|
for name in self.config.cameras.keys():
|
2020-11-01 16:56:33 +01:00
|
|
|
self.detection_out_events[name] = mp.Event()
|
|
|
|
shm_in = mp.shared_memory.SharedMemory(name=name, create=True, size=300*300*3)
|
|
|
|
shm_out = mp.shared_memory.SharedMemory(name=f"out-{name}", create=True, size=20*6*4)
|
|
|
|
self.detection_shms.append(shm_in)
|
|
|
|
self.detection_shms.append(shm_out)
|
|
|
|
|
2020-11-03 15:15:58 +01:00
|
|
|
for name, detector in self.config.detectors.items():
|
|
|
|
if detector.type == 'cpu':
|
2020-11-01 16:56:33 +01:00
|
|
|
self.detectors[name] = EdgeTPUProcess(self.detection_queue, out_events=self.detection_out_events, tf_device='cpu')
|
2020-11-03 15:15:58 +01:00
|
|
|
if detector.type == 'edgetpu':
|
|
|
|
self.detectors[name] = EdgeTPUProcess(self.detection_queue, out_events=self.detection_out_events, tf_device=detector.device)
|
2020-11-01 13:17:44 +01:00
|
|
|
|
2020-11-01 17:10:43 +01:00
|
|
|
def start_detected_frames_processor(self):
|
2020-11-03 15:15:58 +01:00
|
|
|
self.detected_frames_processor = TrackedObjectProcessor(self.config.cameras, self.mqtt_client, self.config.mqtt.topic_prefix,
|
2020-11-01 17:10:43 +01:00
|
|
|
self.detected_frames_queue, self.event_queue, self.stop_event)
|
|
|
|
self.detected_frames_processor.start()
|
2020-11-01 13:17:44 +01:00
|
|
|
|
2020-11-01 17:37:17 +01:00
|
|
|
def start_camera_processors(self):
|
2020-11-03 15:15:58 +01:00
|
|
|
for name, config in self.config.cameras.items():
|
2020-11-01 17:37:17 +01:00
|
|
|
camera_process = mp.Process(target=track_camera, args=(name, config,
|
|
|
|
self.detection_queue, self.detection_out_events[name], self.detected_frames_queue,
|
2020-11-01 21:58:51 +01:00
|
|
|
self.camera_metrics[name]))
|
2020-11-01 17:37:17 +01:00
|
|
|
camera_process.daemon = True
|
2020-11-01 21:58:51 +01:00
|
|
|
self.camera_metrics[name]['process'] = camera_process
|
2020-11-01 17:37:17 +01:00
|
|
|
camera_process.start()
|
2020-11-04 04:26:39 +01:00
|
|
|
logger.info(f"Camera processor started for {name}: {camera_process.pid}")
|
2020-11-01 13:17:44 +01:00
|
|
|
|
|
|
|
def start_camera_capture_processes(self):
|
2020-11-03 15:15:58 +01:00
|
|
|
for name, config in self.config.cameras.items():
|
2020-11-01 17:55:11 +01:00
|
|
|
capture_process = mp.Process(target=capture_camera, args=(name, config,
|
2020-11-01 21:58:51 +01:00
|
|
|
self.camera_metrics[name]))
|
2020-11-01 17:55:11 +01:00
|
|
|
capture_process.daemon = True
|
2020-11-01 21:58:51 +01:00
|
|
|
self.camera_metrics[name]['capture_process'] = capture_process
|
2020-11-01 17:55:11 +01:00
|
|
|
capture_process.start()
|
2020-11-04 04:26:39 +01:00
|
|
|
logger.info(f"Capture process started for {name}: {capture_process.pid}")
|
2020-11-01 21:58:51 +01:00
|
|
|
|
|
|
|
def start_event_processor(self):
|
|
|
|
self.event_processor = EventProcessor(self.config, self.camera_metrics, self.event_queue, self.stop_event)
|
|
|
|
self.event_processor.start()
|
2020-11-01 13:17:44 +01:00
|
|
|
|
|
|
|
def start_watchdog(self):
|
2020-11-01 22:45:04 +01:00
|
|
|
self.frigate_watchdog = FrigateWatchdog(self.detectors, self.stop_event)
|
|
|
|
self.frigate_watchdog.start()
|
2020-11-01 13:17:44 +01:00
|
|
|
|
|
|
|
def start(self):
|
2020-11-04 04:26:39 +01:00
|
|
|
self.init_logger()
|
2020-11-01 13:17:44 +01:00
|
|
|
self.init_config()
|
2020-11-01 17:10:43 +01:00
|
|
|
self.init_queues()
|
2020-11-01 13:17:44 +01:00
|
|
|
self.init_database()
|
|
|
|
self.init_mqtt()
|
|
|
|
self.start_detectors()
|
2020-11-01 17:10:43 +01:00
|
|
|
self.start_detected_frames_processor()
|
2020-11-01 17:37:17 +01:00
|
|
|
self.start_camera_processors()
|
2020-11-01 13:17:44 +01:00
|
|
|
self.start_camera_capture_processes()
|
2020-11-01 22:37:51 +01:00
|
|
|
self.init_web_server()
|
2020-11-01 21:58:51 +01:00
|
|
|
self.start_event_processor()
|
2020-11-01 13:17:44 +01:00
|
|
|
self.start_watchdog()
|
2020-11-03 15:15:58 +01:00
|
|
|
self.flask_app.run(host='0.0.0.0', port=self.config.web_port, debug=False)
|
2020-11-01 16:56:33 +01:00
|
|
|
self.stop()
|
|
|
|
|
|
|
|
def stop(self):
|
2020-11-04 04:26:39 +01:00
|
|
|
logger.info(f"Stopping...")
|
2020-11-01 16:56:33 +01:00
|
|
|
self.stop_event.set()
|
|
|
|
|
2020-11-01 17:10:43 +01:00
|
|
|
self.detected_frames_processor.join()
|
2020-11-01 21:58:51 +01:00
|
|
|
self.event_processor.join()
|
2020-11-01 22:45:04 +01:00
|
|
|
self.frigate_watchdog.join()
|
2020-11-01 17:10:43 +01:00
|
|
|
|
2020-11-01 16:56:33 +01:00
|
|
|
for detector in self.detectors.values():
|
|
|
|
detector.stop()
|
|
|
|
|
|
|
|
while len(self.detection_shms) > 0:
|
|
|
|
shm = self.detection_shms.pop()
|
|
|
|
shm.close()
|
|
|
|
shm.unlink()
|
2020-11-01 13:17:44 +01:00
|
|
|
|
2019-01-26 15:02:59 +01:00
|
|
|
if __name__ == '__main__':
|
2020-11-01 16:56:33 +01:00
|
|
|
frigate_app = FrigateApp()
|
|
|
|
|
2020-11-01 15:06:15 +01:00
|
|
|
frigate_app.start()
|