detect_objects.py 3.5 KB

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