blakeblackshear.frigate/frigate/__main__.py

113 lines
3.6 KiB
Python
Raw Normal View History

2020-11-01 16:56:33 +01:00
import faulthandler; faulthandler.enable()
2020-11-01 15:06:15 +01:00
import os
import json
2020-11-01 15:06:15 +01:00
import yaml
2020-02-16 04:07:54 +01:00
import multiprocessing as mp
2020-11-01 15:06:15 +01:00
2020-10-30 13:38:59 +01:00
from playhouse.sqlite_ext import *
2020-11-01 16:56:33 +01:00
from typing import Dict, List
2019-01-26 15:02:59 +01:00
2020-11-01 13:17:44 +01:00
from frigate.config import FRIGATE_CONFIG_SCHEMA
2020-11-01 16:56:33 +01:00
from frigate.edgetpu import EdgeTPUProcess
2020-11-01 15:06:15 +01:00
from frigate.http import create_app
from frigate.models import Event
from frigate.mqtt import create_mqtt_client
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()
self.config: dict = None
self.detection_queue = mp.Queue()
self.detectors: Dict[str: EdgeTPUProcess] = {}
self.detection_out_events: Dict[str: mp.Event] = {}
self.detection_shms: List[mp.shared_memory.SharedMemory] = []
2020-11-01 13:17:44 +01:00
def init_config(self):
config_file = os.environ.get('CONFIG_FILE', '/config/config.yml')
if config_file.endswith(".yml"):
with open(config_file) as f:
config = yaml.safe_load(f)
elif config_file.endswith(".json"):
with open(config_file) as f:
config = json.load(f)
self.config = FRIGATE_CONFIG_SCHEMA(config)
2020-11-01 15:06:15 +01:00
# TODO: sub in FRIGATE_ENV vars
2020-11-01 13:17:44 +01:00
def init_database(self):
2020-11-01 15:06:15 +01:00
self.db = SqliteExtDatabase(f"/{os.path.join(self.config['save_clips']['clips_dir'], 'frigate.db')}")
models = [Event]
self.db.bind(models)
self.db.create_tables(models, safe=True)
def init_web_server(self):
self.flask_app = create_app(self.db)
2020-11-01 13:17:44 +01:00
def init_mqtt(self):
2020-11-01 15:06:15 +01:00
# TODO: create config class
mqtt_config = self.config['mqtt']
self.mqtt_client = create_mqtt_client(
mqtt_config['host'],
mqtt_config['port'],
mqtt_config['topic_prefix'],
mqtt_config['client_id'],
mqtt_config.get('user'),
mqtt_config.get('password')
)
2020-11-01 13:17:44 +01:00
def start_detectors(self):
2020-11-01 16:56:33 +01:00
for name in self.config['cameras'].keys():
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)
for name, detector in self.config['detectors'].items():
if detector['type'] == 'cpu':
self.detectors[name] = EdgeTPUProcess(self.detection_queue, out_events=self.detection_out_events, tf_device='cpu')
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
def start_detection_processor(self):
pass
def start_frame_processors(self):
pass
def start_camera_capture_processes(self):
pass
def start_watchdog(self):
pass
def start(self):
self.init_config()
self.init_database()
2020-11-01 15:06:15 +01:00
self.init_web_server()
2020-11-01 13:17:44 +01:00
self.init_mqtt()
self.start_detectors()
self.start_detection_processor()
self.start_frame_processors()
self.start_camera_capture_processes()
self.start_watchdog()
2020-11-01 15:06:15 +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):
self.stop_event.set()
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()