http.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. import os
  2. from flask import (
  3. Flask, Blueprint, jsonify
  4. )
  5. from peewee import SqliteDatabase
  6. from playhouse.shortcuts import model_to_dict
  7. from frigate.models import Event
  8. bp = Blueprint('frigate', __name__)
  9. def create_app(database: SqliteDatabase):
  10. app = Flask(__name__)
  11. @app.before_request
  12. def _db_connect():
  13. database.connect()
  14. @app.teardown_request
  15. def _db_close(exc):
  16. if not database.is_closed():
  17. database.close()
  18. app.register_blueprint(bp)
  19. return app
  20. @bp.route('/')
  21. def is_healthy():
  22. return "Frigate is running. Alive and healthy!"
  23. @bp.route('/events')
  24. def events():
  25. events = Event.select()
  26. return jsonify([model_to_dict(e) for e in events])
  27. # @app.route('/debug/stats')
  28. # def stats():
  29. # stats = {}
  30. # total_detection_fps = 0
  31. # for name, camera_stats in camera_process_info.items():
  32. # total_detection_fps += camera_stats['detection_fps'].value
  33. # stats[name] = {
  34. # 'camera_fps': round(camera_stats['camera_fps'].value, 2),
  35. # 'process_fps': round(camera_stats['process_fps'].value, 2),
  36. # 'skipped_fps': round(camera_stats['skipped_fps'].value, 2),
  37. # 'detection_fps': round(camera_stats['detection_fps'].value, 2),
  38. # 'pid': camera_stats['process'].pid,
  39. # 'capture_pid': camera_stats['capture_process'].pid,
  40. # 'frame_info': {
  41. # 'detect': camera_stats['detection_frame'].value,
  42. # 'process': object_processor.camera_data[name]['current_frame_time']
  43. # }
  44. # }
  45. # stats['detectors'] = {}
  46. # for name, detector in detectors.items():
  47. # stats['detectors'][name] = {
  48. # 'inference_speed': round(detector.avg_inference_speed.value*1000, 2),
  49. # 'detection_start': detector.detection_start.value,
  50. # 'pid': detector.detect_process.pid
  51. # }
  52. # stats['detection_fps'] = round(total_detection_fps, 2)
  53. # return jsonify(stats)
  54. # @app.route('/<camera_name>/<label>/best.jpg')
  55. # def best(camera_name, label):
  56. # if camera_name in CONFIG['cameras']:
  57. # best_object = object_processor.get_best(camera_name, label)
  58. # best_frame = best_object.get('frame')
  59. # if best_frame is None:
  60. # best_frame = np.zeros((720,1280,3), np.uint8)
  61. # else:
  62. # best_frame = cv2.cvtColor(best_frame, cv2.COLOR_YUV2BGR_I420)
  63. # crop = bool(request.args.get('crop', 0, type=int))
  64. # if crop:
  65. # region = best_object.get('region', [0,0,300,300])
  66. # best_frame = best_frame[region[1]:region[3], region[0]:region[2]]
  67. # height = int(request.args.get('h', str(best_frame.shape[0])))
  68. # width = int(height*best_frame.shape[1]/best_frame.shape[0])
  69. # best_frame = cv2.resize(best_frame, dsize=(width, height), interpolation=cv2.INTER_AREA)
  70. # ret, jpg = cv2.imencode('.jpg', best_frame)
  71. # response = make_response(jpg.tobytes())
  72. # response.headers['Content-Type'] = 'image/jpg'
  73. # return response
  74. # else:
  75. # return "Camera named {} not found".format(camera_name), 404
  76. # @app.route('/<camera_name>')
  77. # def mjpeg_feed(camera_name):
  78. # fps = int(request.args.get('fps', '3'))
  79. # height = int(request.args.get('h', '360'))
  80. # if camera_name in CONFIG['cameras']:
  81. # # return a multipart response
  82. # return Response(imagestream(camera_name, fps, height),
  83. # mimetype='multipart/x-mixed-replace; boundary=frame')
  84. # else:
  85. # return "Camera named {} not found".format(camera_name), 404
  86. # @app.route('/<camera_name>/latest.jpg')
  87. # def latest_frame(camera_name):
  88. # if camera_name in CONFIG['cameras']:
  89. # # max out at specified FPS
  90. # frame = object_processor.get_current_frame(camera_name)
  91. # if frame is None:
  92. # frame = np.zeros((720,1280,3), np.uint8)
  93. # height = int(request.args.get('h', str(frame.shape[0])))
  94. # width = int(height*frame.shape[1]/frame.shape[0])
  95. # frame = cv2.resize(frame, dsize=(width, height), interpolation=cv2.INTER_AREA)
  96. # ret, jpg = cv2.imencode('.jpg', frame)
  97. # response = make_response(jpg.tobytes())
  98. # response.headers['Content-Type'] = 'image/jpg'
  99. # return response
  100. # else:
  101. # return "Camera named {} not found".format(camera_name), 404
  102. # def imagestream(camera_name, fps, height):
  103. # while True:
  104. # # max out at specified FPS
  105. # time.sleep(1/fps)
  106. # frame = object_processor.get_current_frame(camera_name, draw=True)
  107. # if frame is None:
  108. # frame = np.zeros((height,int(height*16/9),3), np.uint8)
  109. # width = int(height*frame.shape[1]/frame.shape[0])
  110. # frame = cv2.resize(frame, dsize=(width, height), interpolation=cv2.INTER_LINEAR)
  111. # ret, jpg = cv2.imencode('.jpg', frame)
  112. # yield (b'--frame\r\n'
  113. # b'Content-Type: image/jpeg\r\n\r\n' + jpg.tobytes() + b'\r\n\r\n')
  114. # app.run(host='0.0.0.0', port=WEB_PORT, debug=False)