detect_objects.py 9.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246
  1. import os
  2. import cv2
  3. import time
  4. import datetime
  5. import queue
  6. import yaml
  7. import threading
  8. import multiprocessing as mp
  9. import subprocess as sp
  10. import numpy as np
  11. import logging
  12. from flask import Flask, Response, make_response, jsonify
  13. import paho.mqtt.client as mqtt
  14. from frigate.video import track_camera
  15. from frigate.object_processing import TrackedObjectProcessor
  16. from frigate.util import EventsPerSecond
  17. from frigate.edgetpu import EdgeTPUProcess
  18. FRIGATE_VARS = {k: v for k, v in os.environ.items() if k.startswith('FRIGATE_')}
  19. with open('/config/config.yml') as f:
  20. CONFIG = yaml.safe_load(f)
  21. MQTT_HOST = CONFIG['mqtt']['host']
  22. MQTT_PORT = CONFIG.get('mqtt', {}).get('port', 1883)
  23. MQTT_TOPIC_PREFIX = CONFIG.get('mqtt', {}).get('topic_prefix', 'frigate')
  24. MQTT_USER = CONFIG.get('mqtt', {}).get('user')
  25. MQTT_PASS = CONFIG.get('mqtt', {}).get('password')
  26. if not MQTT_PASS is None:
  27. MQTT_PASS = MQTT_PASS.format(**FRIGATE_VARS)
  28. MQTT_CLIENT_ID = CONFIG.get('mqtt', {}).get('client_id', 'frigate')
  29. # Set the default FFmpeg config
  30. FFMPEG_CONFIG = CONFIG.get('ffmpeg', {})
  31. FFMPEG_DEFAULT_CONFIG = {
  32. 'global_args': FFMPEG_CONFIG.get('global_args',
  33. ['-hide_banner','-loglevel','panic']),
  34. 'hwaccel_args': FFMPEG_CONFIG.get('hwaccel_args',
  35. []),
  36. 'input_args': FFMPEG_CONFIG.get('input_args',
  37. ['-avoid_negative_ts', 'make_zero',
  38. '-fflags', 'nobuffer',
  39. '-flags', 'low_delay',
  40. '-strict', 'experimental',
  41. '-fflags', '+genpts+discardcorrupt',
  42. '-vsync', 'drop',
  43. '-rtsp_transport', 'tcp',
  44. '-stimeout', '5000000',
  45. '-use_wallclock_as_timestamps', '1']),
  46. 'output_args': FFMPEG_CONFIG.get('output_args',
  47. ['-f', 'rawvideo',
  48. '-pix_fmt', 'rgb24'])
  49. }
  50. GLOBAL_OBJECT_CONFIG = CONFIG.get('objects', {})
  51. WEB_PORT = CONFIG.get('web_port', 5000)
  52. DEBUG = (CONFIG.get('debug', '0') == '1')
  53. class CameraWatchdog(threading.Thread):
  54. def __init__(self, camera_processes, config, tflite_process, tracked_objects_queue, object_processor):
  55. threading.Thread.__init__(self)
  56. self.camera_processes = camera_processes
  57. self.config = config
  58. self.tflite_process = tflite_process
  59. self.tracked_objects_queue = tracked_objects_queue
  60. self.object_processor = object_processor
  61. def run(self):
  62. time.sleep(10)
  63. while True:
  64. # wait a bit before checking
  65. time.sleep(30)
  66. if (self.tflite_process.detection_start.value > 0.0 and
  67. datetime.datetime.now().timestamp() - self.tflite_process.detection_start.value > 10):
  68. print("Detection appears to be stuck. Restarting detection process")
  69. time.sleep(30)
  70. for name, camera_process in self.camera_processes.items():
  71. process = camera_process['process']
  72. if not process.is_alive():
  73. print(f"Process for {name} is not alive. Starting again...")
  74. camera_process['fps'].value = float(self.config[name]['fps'])
  75. camera_process['skipped_fps'].value = 0.0
  76. camera_process['detection_fps'].value = 0.0
  77. process = mp.Process(target=track_camera, args=(name, self.config[name], FFMPEG_DEFAULT_CONFIG, GLOBAL_OBJECT_CONFIG,
  78. self.tflite_process.detection_queue, self.tracked_objects_queue,
  79. camera_process['fps'], camera_process['skipped_fps'], camera_process['detection_fps']))
  80. process.daemon = True
  81. camera_process['process'] = process
  82. process.start()
  83. print(f"Camera_process started for {name}: {process.pid}")
  84. def main():
  85. # connect to mqtt and setup last will
  86. def on_connect(client, userdata, flags, rc):
  87. print("On connect called")
  88. if rc != 0:
  89. if rc == 3:
  90. print ("MQTT Server unavailable")
  91. elif rc == 4:
  92. print ("MQTT Bad username or password")
  93. elif rc == 5:
  94. print ("MQTT Not authorized")
  95. else:
  96. print ("Unable to connect to MQTT: Connection refused. Error code: " + str(rc))
  97. # publish a message to signal that the service is running
  98. client.publish(MQTT_TOPIC_PREFIX+'/available', 'online', retain=True)
  99. client = mqtt.Client(client_id=MQTT_CLIENT_ID)
  100. client.on_connect = on_connect
  101. client.will_set(MQTT_TOPIC_PREFIX+'/available', payload='offline', qos=1, retain=True)
  102. if not MQTT_USER is None:
  103. client.username_pw_set(MQTT_USER, password=MQTT_PASS)
  104. client.connect(MQTT_HOST, MQTT_PORT, 60)
  105. client.loop_start()
  106. # start plasma store
  107. plasma_cmd = ['plasma_store', '-m', '400000000', '-s', '/tmp/plasma']
  108. plasma_process = sp.Popen(plasma_cmd, stdout=sp.DEVNULL)
  109. time.sleep(1)
  110. rc = plasma_process.poll()
  111. if rc is not None:
  112. raise RuntimeError("plasma_store exited unexpectedly with "
  113. "code %d" % (rc,))
  114. ##
  115. # Setup config defaults for cameras
  116. ##
  117. for name, config in CONFIG['cameras'].items():
  118. config['snapshots'] = {
  119. 'show_timestamp': config.get('snapshots', {}).get('show_timestamp', True)
  120. }
  121. # Queue for cameras to push tracked objects to
  122. tracked_objects_queue = mp.Queue()
  123. # Start the shared tflite process
  124. tflite_process = EdgeTPUProcess()
  125. # start the camera processes
  126. camera_processes = {}
  127. for name, config in CONFIG['cameras'].items():
  128. camera_processes[name] = {
  129. 'fps': mp.Value('d', float(config['fps'])),
  130. 'skipped_fps': mp.Value('d', 0.0),
  131. 'detection_fps': mp.Value('d', 0.0)
  132. }
  133. camera_process = mp.Process(target=track_camera, args=(name, config, FFMPEG_DEFAULT_CONFIG, GLOBAL_OBJECT_CONFIG,
  134. tflite_process.detection_queue, tracked_objects_queue,
  135. camera_processes[name]['fps'], camera_processes[name]['skipped_fps'], camera_processes[name]['detection_fps']))
  136. camera_process.daemon = True
  137. camera_processes[name]['process'] = camera_process
  138. for name, camera_process in camera_processes.items():
  139. camera_process['process'].start()
  140. print(f"Camera_process started for {name}: {camera_process['process'].pid}")
  141. object_processor = TrackedObjectProcessor(CONFIG['cameras'], client, MQTT_TOPIC_PREFIX, tracked_objects_queue)
  142. object_processor.start()
  143. camera_watchdog = CameraWatchdog(camera_processes, CONFIG['cameras'], tflite_process, tracked_objects_queue, object_processor)
  144. camera_watchdog.start()
  145. # create a flask app that encodes frames a mjpeg on demand
  146. app = Flask(__name__)
  147. log = logging.getLogger('werkzeug')
  148. log.setLevel(logging.ERROR)
  149. @app.route('/')
  150. def ishealthy():
  151. # return a healh
  152. return "Frigate is running. Alive and healthy!"
  153. @app.route('/debug/stats')
  154. def stats():
  155. stats = {}
  156. total_detection_fps = 0
  157. for name, camera_stats in camera_processes.items():
  158. total_detection_fps += camera_stats['detection_fps'].value
  159. stats[name] = {
  160. 'fps': round(camera_stats['fps'].value, 2),
  161. 'skipped_fps': round(camera_stats['skipped_fps'].value, 2),
  162. 'detection_fps': round(camera_stats['detection_fps'].value, 2)
  163. }
  164. stats['coral'] = {
  165. 'fps': round(total_detection_fps, 2),
  166. 'inference_speed': round(tflite_process.avg_inference_speed.value*1000, 2),
  167. 'detection_queue': tflite_process.detection_queue.qsize(),
  168. 'detection_start': tflite_process.detection_start.value
  169. }
  170. rc = plasma_process.poll()
  171. stats['plasma_store_rc'] = rc
  172. stats['tracked_objects_queue'] = tracked_objects_queue.qsize()
  173. return jsonify(stats)
  174. @app.route('/<camera_name>/<label>/best.jpg')
  175. def best(camera_name, label):
  176. if camera_name in CONFIG['cameras']:
  177. best_frame = object_processor.get_best(camera_name, label)
  178. if best_frame is None:
  179. best_frame = np.zeros((720,1280,3), np.uint8)
  180. best_frame = cv2.cvtColor(best_frame, cv2.COLOR_RGB2BGR)
  181. ret, jpg = cv2.imencode('.jpg', best_frame)
  182. response = make_response(jpg.tobytes())
  183. response.headers['Content-Type'] = 'image/jpg'
  184. return response
  185. else:
  186. return "Camera named {} not found".format(camera_name), 404
  187. @app.route('/<camera_name>')
  188. def mjpeg_feed(camera_name):
  189. if camera_name in CONFIG['cameras']:
  190. # return a multipart response
  191. return Response(imagestream(camera_name),
  192. mimetype='multipart/x-mixed-replace; boundary=frame')
  193. else:
  194. return "Camera named {} not found".format(camera_name), 404
  195. def imagestream(camera_name):
  196. while True:
  197. # max out at 1 FPS
  198. time.sleep(1)
  199. frame = object_processor.get_current_frame(camera_name)
  200. if frame is None:
  201. frame = np.zeros((720,1280,3), np.uint8)
  202. frame = cv2.cvtColor(frame, cv2.COLOR_RGB2BGR)
  203. ret, jpg = cv2.imencode('.jpg', frame)
  204. yield (b'--frame\r\n'
  205. b'Content-Type: image/jpeg\r\n\r\n' + jpg.tobytes() + b'\r\n\r\n')
  206. app.run(host='0.0.0.0', port=WEB_PORT, debug=False)
  207. camera_watchdog.join()
  208. plasma_process.terminate()
  209. if __name__ == '__main__':
  210. main()