detect_objects.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import cv2
  2. import time
  3. import queue
  4. import yaml
  5. import numpy as np
  6. from flask import Flask, Response, make_response
  7. import paho.mqtt.client as mqtt
  8. from frigate.video import Camera
  9. from frigate.object_detection import PreppedQueueProcessor
  10. with open('/config/config.yml') as f:
  11. CONFIG = yaml.safe_load(f)
  12. MQTT_HOST = CONFIG['mqtt']['host']
  13. MQTT_PORT = CONFIG.get('mqtt', {}).get('port', 1883)
  14. MQTT_TOPIC_PREFIX = CONFIG.get('mqtt', {}).get('topic_prefix', 'frigate')
  15. MQTT_USER = CONFIG.get('mqtt', {}).get('user')
  16. MQTT_PASS = CONFIG.get('mqtt', {}).get('password')
  17. MQTT_CLIENT_ID = CONFIG.get('mqtt', {}).get('client_id', 'frigate')
  18. WEB_PORT = CONFIG.get('web_port', 5000)
  19. DEBUG = (CONFIG.get('debug', '0') == '1')
  20. def main():
  21. # connect to mqtt and setup last will
  22. def on_connect(client, userdata, flags, rc):
  23. print("On connect called")
  24. if rc != 0:
  25. if rc == 3:
  26. print ("MQTT Server unavailable")
  27. elif rc == 4:
  28. print ("MQTT Bad username or password")
  29. elif rc == 5:
  30. print ("MQTT Not authorized")
  31. else:
  32. print ("Unable to connect to MQTT: Connection refused. Error code: " + str(rc))
  33. # publish a message to signal that the service is running
  34. client.publish(MQTT_TOPIC_PREFIX+'/available', 'online', retain=True)
  35. client = mqtt.Client(client_id=MQTT_CLIENT_ID)
  36. client.on_connect = on_connect
  37. client.will_set(MQTT_TOPIC_PREFIX+'/available', payload='offline', qos=1, retain=True)
  38. if not MQTT_USER is None:
  39. client.username_pw_set(MQTT_USER, password=MQTT_PASS)
  40. client.connect(MQTT_HOST, MQTT_PORT, 60)
  41. client.loop_start()
  42. # Queue for prepped frames, max size set to (number of cameras * 5)
  43. max_queue_size = len(CONFIG['cameras'].items())*5
  44. prepped_frame_queue = queue.Queue(max_queue_size)
  45. cameras = {}
  46. for name, config in CONFIG['cameras'].items():
  47. cameras[name] = Camera(name, config, prepped_frame_queue, client, MQTT_TOPIC_PREFIX)
  48. prepped_queue_processor = PreppedQueueProcessor(
  49. cameras,
  50. prepped_frame_queue
  51. )
  52. prepped_queue_processor.start()
  53. for name, camera in cameras.items():
  54. camera.start()
  55. print("Capture process for {}: {}".format(name, camera.get_capture_pid()))
  56. # create a flask app that encodes frames a mjpeg on demand
  57. app = Flask(__name__)
  58. @app.route('/<camera_name>/best_person.jpg')
  59. def best_person(camera_name):
  60. best_person_frame = cameras[camera_name].get_best_person()
  61. if best_person_frame is None:
  62. best_person_frame = np.zeros((720,1280,3), np.uint8)
  63. ret, jpg = cv2.imencode('.jpg', best_person_frame)
  64. response = make_response(jpg.tobytes())
  65. response.headers['Content-Type'] = 'image/jpg'
  66. return response
  67. @app.route('/<camera_name>')
  68. def mjpeg_feed(camera_name):
  69. # return a multipart response
  70. return Response(imagestream(camera_name),
  71. mimetype='multipart/x-mixed-replace; boundary=frame')
  72. def imagestream(camera_name):
  73. while True:
  74. # max out at 5 FPS
  75. time.sleep(0.2)
  76. frame = cameras[camera_name].get_current_frame_with_objects()
  77. # encode the image into a jpg
  78. ret, jpg = cv2.imencode('.jpg', frame)
  79. yield (b'--frame\r\n'
  80. b'Content-Type: image/jpeg\r\n\r\n' + jpg.tobytes() + b'\r\n\r\n')
  81. app.run(host='0.0.0.0', port=WEB_PORT, debug=False)
  82. camera.join()
  83. if __name__ == '__main__':
  84. main()