add back all endpoints

This commit is contained in:
Blake Blackshear 2020-11-01 15:37:51 -06:00
parent 7ee6bfe855
commit 37ee746ebb
2 changed files with 97 additions and 93 deletions

View File

@ -113,7 +113,7 @@ class FrigateApp():
self.db.create_tables(models, safe=True) self.db.create_tables(models, safe=True)
def init_web_server(self): def init_web_server(self):
self.flask_app = create_app(self.db) self.flask_app = create_app(self.config, self.db, self.camera_metrics, self.detectors, self.detected_frames_processor)
def init_mqtt(self): def init_mqtt(self):
# TODO: create config class # TODO: create config class
@ -176,12 +176,12 @@ class FrigateApp():
self.init_config() self.init_config()
self.init_queues() self.init_queues()
self.init_database() self.init_database()
self.init_web_server()
self.init_mqtt() self.init_mqtt()
self.start_detectors() self.start_detectors()
self.start_detected_frames_processor() self.start_detected_frames_processor()
self.start_camera_processors() self.start_camera_processors()
self.start_camera_capture_processes() self.start_camera_capture_processes()
self.init_web_server()
self.start_event_processor() self.start_event_processor()
self.start_watchdog() self.start_watchdog()
self.flask_app.run(host='0.0.0.0', port=self.config['web_port'], debug=False) self.flask_app.run(host='0.0.0.0', port=self.config['web_port'], debug=False)

View File

@ -1,7 +1,11 @@
import os import os
import time
import cv2
import numpy as np
from flask import ( from flask import (
Flask, Blueprint, jsonify Flask, Blueprint, jsonify, request, Response, current_app, make_response
) )
from peewee import SqliteDatabase from peewee import SqliteDatabase
from playhouse.shortcuts import model_to_dict from playhouse.shortcuts import model_to_dict
@ -10,7 +14,7 @@ from frigate.models import Event
bp = Blueprint('frigate', __name__) bp = Blueprint('frigate', __name__)
def create_app(database: SqliteDatabase): def create_app(frigate_config, database: SqliteDatabase, camera_metrics, detectors, detected_frames_processor):
app = Flask(__name__) app = Flask(__name__)
@app.before_request @app.before_request
@ -21,6 +25,11 @@ def create_app(database: SqliteDatabase):
def _db_close(exc): def _db_close(exc):
if not database.is_closed(): if not database.is_closed():
database.close() database.close()
app.frigate_config = frigate_config
app.camera_metrics = camera_metrics
app.detectors = detectors
app.detected_frames_processor = detected_frames_processor
app.register_blueprint(bp) app.register_blueprint(bp)
@ -35,108 +44,103 @@ def events():
events = Event.select() events = Event.select()
return jsonify([model_to_dict(e) for e in events]) return jsonify([model_to_dict(e) for e in events])
# @app.route('/debug/stats') @bp.route('/debug/stats')
# def stats(): def stats():
# stats = {} camera_metrics = current_app.camera_metrics
stats = {}
# total_detection_fps = 0 total_detection_fps = 0
# for name, camera_stats in camera_process_info.items(): for name, camera_stats in camera_metrics.items():
# total_detection_fps += camera_stats['detection_fps'].value total_detection_fps += camera_stats['detection_fps'].value
# stats[name] = { stats[name] = {
# 'camera_fps': round(camera_stats['camera_fps'].value, 2), 'camera_fps': round(camera_stats['camera_fps'].value, 2),
# 'process_fps': round(camera_stats['process_fps'].value, 2), 'process_fps': round(camera_stats['process_fps'].value, 2),
# 'skipped_fps': round(camera_stats['skipped_fps'].value, 2), 'skipped_fps': round(camera_stats['skipped_fps'].value, 2),
# 'detection_fps': round(camera_stats['detection_fps'].value, 2), 'detection_fps': round(camera_stats['detection_fps'].value, 2),
# 'pid': camera_stats['process'].pid, 'pid': camera_stats['process'].pid,
# 'capture_pid': camera_stats['capture_process'].pid, 'capture_pid': camera_stats['capture_process'].pid
# 'frame_info': { }
# 'detect': camera_stats['detection_frame'].value,
# 'process': object_processor.camera_data[name]['current_frame_time']
# }
# }
# stats['detectors'] = {} stats['detectors'] = {}
# for name, detector in detectors.items(): for name, detector in current_app.detectors.items():
# stats['detectors'][name] = { stats['detectors'][name] = {
# 'inference_speed': round(detector.avg_inference_speed.value*1000, 2), 'inference_speed': round(detector.avg_inference_speed.value*1000, 2),
# 'detection_start': detector.detection_start.value, 'detection_start': detector.detection_start.value,
# 'pid': detector.detect_process.pid 'pid': detector.detect_process.pid
# } }
# stats['detection_fps'] = round(total_detection_fps, 2) stats['detection_fps'] = round(total_detection_fps, 2)
# return jsonify(stats) return jsonify(stats)
# @app.route('/<camera_name>/<label>/best.jpg') @bp.route('/<camera_name>/<label>/best.jpg')
# def best(camera_name, label): def best(camera_name, label):
# if camera_name in CONFIG['cameras']: if camera_name in current_app.frigate_config['cameras']:
# best_object = object_processor.get_best(camera_name, label) best_object = current_app.detected_frames_processor.get_best(camera_name, label)
# best_frame = best_object.get('frame') best_frame = best_object.get('frame')
# if best_frame is None: if best_frame is None:
# best_frame = np.zeros((720,1280,3), np.uint8) best_frame = np.zeros((720,1280,3), np.uint8)
# else: else:
# best_frame = cv2.cvtColor(best_frame, cv2.COLOR_YUV2BGR_I420) best_frame = cv2.cvtColor(best_frame, cv2.COLOR_YUV2BGR_I420)
# crop = bool(request.args.get('crop', 0, type=int)) crop = bool(request.args.get('crop', 0, type=int))
# if crop: if crop:
# region = best_object.get('region', [0,0,300,300]) region = best_object.get('region', [0,0,300,300])
# best_frame = best_frame[region[1]:region[3], region[0]:region[2]] best_frame = best_frame[region[1]:region[3], region[0]:region[2]]
# height = int(request.args.get('h', str(best_frame.shape[0]))) height = int(request.args.get('h', str(best_frame.shape[0])))
# width = int(height*best_frame.shape[1]/best_frame.shape[0]) width = int(height*best_frame.shape[1]/best_frame.shape[0])
# best_frame = cv2.resize(best_frame, dsize=(width, height), interpolation=cv2.INTER_AREA) best_frame = cv2.resize(best_frame, dsize=(width, height), interpolation=cv2.INTER_AREA)
# ret, jpg = cv2.imencode('.jpg', best_frame) ret, jpg = cv2.imencode('.jpg', best_frame)
# response = make_response(jpg.tobytes()) response = make_response(jpg.tobytes())
# response.headers['Content-Type'] = 'image/jpg' response.headers['Content-Type'] = 'image/jpg'
# return response return response
# else: else:
# return "Camera named {} not found".format(camera_name), 404 return "Camera named {} not found".format(camera_name), 404
# @app.route('/<camera_name>') @bp.route('/<camera_name>')
# def mjpeg_feed(camera_name): def mjpeg_feed(camera_name):
# fps = int(request.args.get('fps', '3')) fps = int(request.args.get('fps', '3'))
# height = int(request.args.get('h', '360')) height = int(request.args.get('h', '360'))
# if camera_name in CONFIG['cameras']: if camera_name in current_app.frigate_config['cameras']:
# # return a multipart response # return a multipart response
# return Response(imagestream(camera_name, fps, height), return Response(imagestream(current_app.detected_frames_processor, camera_name, fps, height),
# mimetype='multipart/x-mixed-replace; boundary=frame') mimetype='multipart/x-mixed-replace; boundary=frame')
# else: else:
# return "Camera named {} not found".format(camera_name), 404 return "Camera named {} not found".format(camera_name), 404
# @app.route('/<camera_name>/latest.jpg') @bp.route('/<camera_name>/latest.jpg')
# def latest_frame(camera_name): def latest_frame(camera_name):
# if camera_name in CONFIG['cameras']: if camera_name in current_app.frigate_config['cameras']:
# # max out at specified FPS # max out at specified FPS
# frame = object_processor.get_current_frame(camera_name) frame = current_app.detected_frames_processor.get_current_frame(camera_name)
# if frame is None: if frame is None:
# frame = np.zeros((720,1280,3), np.uint8) frame = np.zeros((720,1280,3), np.uint8)
# height = int(request.args.get('h', str(frame.shape[0]))) height = int(request.args.get('h', str(frame.shape[0])))
# width = int(height*frame.shape[1]/frame.shape[0]) width = int(height*frame.shape[1]/frame.shape[0])
# frame = cv2.resize(frame, dsize=(width, height), interpolation=cv2.INTER_AREA) frame = cv2.resize(frame, dsize=(width, height), interpolation=cv2.INTER_AREA)
# ret, jpg = cv2.imencode('.jpg', frame) ret, jpg = cv2.imencode('.jpg', frame)
# response = make_response(jpg.tobytes()) response = make_response(jpg.tobytes())
# response.headers['Content-Type'] = 'image/jpg' response.headers['Content-Type'] = 'image/jpg'
# return response return response
# else: else:
# return "Camera named {} not found".format(camera_name), 404 return "Camera named {} not found".format(camera_name), 404
# def imagestream(camera_name, fps, height): def imagestream(detected_frames_processor, camera_name, fps, height):
# while True: while True:
# # max out at specified FPS # max out at specified FPS
# time.sleep(1/fps) time.sleep(1/fps)
# frame = object_processor.get_current_frame(camera_name, draw=True) frame = detected_frames_processor.get_current_frame(camera_name, draw=True)
# if frame is None: if frame is None:
# frame = np.zeros((height,int(height*16/9),3), np.uint8) frame = np.zeros((height,int(height*16/9),3), np.uint8)
# width = int(height*frame.shape[1]/frame.shape[0]) width = int(height*frame.shape[1]/frame.shape[0])
# frame = cv2.resize(frame, dsize=(width, height), interpolation=cv2.INTER_LINEAR) frame = cv2.resize(frame, dsize=(width, height), interpolation=cv2.INTER_LINEAR)
# ret, jpg = cv2.imencode('.jpg', frame) ret, jpg = cv2.imencode('.jpg', frame)
# yield (b'--frame\r\n' yield (b'--frame\r\n'
# b'Content-Type: image/jpeg\r\n\r\n' + jpg.tobytes() + b'\r\n\r\n') b'Content-Type: image/jpeg\r\n\r\n' + jpg.tobytes() + b'\r\n\r\n')
# app.run(host='0.0.0.0', port=WEB_PORT, debug=False)