detect_objects.py 3.1 KB

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