__main__.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. import faulthandler; faulthandler.enable()
  2. import os
  3. import json
  4. import yaml
  5. import multiprocessing as mp
  6. from playhouse.sqlite_ext import *
  7. from typing import Dict, List
  8. from frigate.config import FRIGATE_CONFIG_SCHEMA
  9. from frigate.edgetpu import EdgeTPUProcess
  10. from frigate.http import create_app
  11. from frigate.models import Event
  12. from frigate.mqtt import create_mqtt_client
  13. class FrigateApp():
  14. def __init__(self):
  15. self.stop_event = mp.Event()
  16. self.config: dict = None
  17. self.detection_queue = mp.Queue()
  18. self.detectors: Dict[str: EdgeTPUProcess] = {}
  19. self.detection_out_events: Dict[str: mp.Event] = {}
  20. self.detection_shms: List[mp.shared_memory.SharedMemory] = []
  21. def init_config(self):
  22. config_file = os.environ.get('CONFIG_FILE', '/config/config.yml')
  23. if config_file.endswith(".yml"):
  24. with open(config_file) as f:
  25. config = yaml.safe_load(f)
  26. elif config_file.endswith(".json"):
  27. with open(config_file) as f:
  28. config = json.load(f)
  29. self.config = FRIGATE_CONFIG_SCHEMA(config)
  30. # TODO: sub in FRIGATE_ENV vars
  31. def init_database(self):
  32. self.db = SqliteExtDatabase(f"/{os.path.join(self.config['save_clips']['clips_dir'], 'frigate.db')}")
  33. models = [Event]
  34. self.db.bind(models)
  35. self.db.create_tables(models, safe=True)
  36. def init_web_server(self):
  37. self.flask_app = create_app(self.db)
  38. def init_mqtt(self):
  39. # TODO: create config class
  40. mqtt_config = self.config['mqtt']
  41. self.mqtt_client = create_mqtt_client(
  42. mqtt_config['host'],
  43. mqtt_config['port'],
  44. mqtt_config['topic_prefix'],
  45. mqtt_config['client_id'],
  46. mqtt_config.get('user'),
  47. mqtt_config.get('password')
  48. )
  49. def start_detectors(self):
  50. for name in self.config['cameras'].keys():
  51. self.detection_out_events[name] = mp.Event()
  52. shm_in = mp.shared_memory.SharedMemory(name=name, create=True, size=300*300*3)
  53. shm_out = mp.shared_memory.SharedMemory(name=f"out-{name}", create=True, size=20*6*4)
  54. self.detection_shms.append(shm_in)
  55. self.detection_shms.append(shm_out)
  56. for name, detector in self.config['detectors'].items():
  57. if detector['type'] == 'cpu':
  58. self.detectors[name] = EdgeTPUProcess(self.detection_queue, out_events=self.detection_out_events, tf_device='cpu')
  59. if detector['type'] == 'edgetpu':
  60. self.detectors[name] = EdgeTPUProcess(self.detection_queue, out_events=self.detection_out_events, tf_device=detector['device'])
  61. def start_detection_processor(self):
  62. pass
  63. def start_frame_processors(self):
  64. pass
  65. def start_camera_capture_processes(self):
  66. pass
  67. def start_watchdog(self):
  68. pass
  69. def start(self):
  70. self.init_config()
  71. self.init_database()
  72. self.init_web_server()
  73. self.init_mqtt()
  74. self.start_detectors()
  75. self.start_detection_processor()
  76. self.start_frame_processors()
  77. self.start_camera_capture_processes()
  78. self.start_watchdog()
  79. self.flask_app.run(host='0.0.0.0', port=self.config['web_port'], debug=False)
  80. self.stop()
  81. def stop(self):
  82. self.stop_event.set()
  83. for detector in self.detectors.values():
  84. detector.stop()
  85. while len(self.detection_shms) > 0:
  86. shm = self.detection_shms.pop()
  87. shm.close()
  88. shm.unlink()
  89. if __name__ == '__main__':
  90. frigate_app = FrigateApp()
  91. frigate_app.start()