diff --git a/frigate/__main__.py b/frigate/__main__.py index 989025b96..1757582a2 100644 --- a/frigate/__main__.py +++ b/frigate/__main__.py @@ -113,7 +113,7 @@ class FrigateApp(): self.db.create_tables(models, safe=True) 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): # TODO: create config class @@ -176,12 +176,12 @@ class FrigateApp(): self.init_config() self.init_queues() self.init_database() - self.init_web_server() self.init_mqtt() self.start_detectors() self.start_detected_frames_processor() self.start_camera_processors() self.start_camera_capture_processes() + self.init_web_server() self.start_event_processor() self.start_watchdog() self.flask_app.run(host='0.0.0.0', port=self.config['web_port'], debug=False) diff --git a/frigate/http.py b/frigate/http.py index f52d6d38e..a24fe7387 100644 --- a/frigate/http.py +++ b/frigate/http.py @@ -1,7 +1,11 @@ import os +import time + +import cv2 +import numpy as np from flask import ( - Flask, Blueprint, jsonify + Flask, Blueprint, jsonify, request, Response, current_app, make_response ) from peewee import SqliteDatabase from playhouse.shortcuts import model_to_dict @@ -10,7 +14,7 @@ from frigate.models import Event 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.before_request @@ -21,6 +25,11 @@ def create_app(database: SqliteDatabase): def _db_close(exc): if not database.is_closed(): 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) @@ -35,108 +44,103 @@ def events(): events = Event.select() return jsonify([model_to_dict(e) for e in events]) -# @app.route('/debug/stats') -# def stats(): -# stats = {} +@bp.route('/debug/stats') +def 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(): -# total_detection_fps += camera_stats['detection_fps'].value -# stats[name] = { -# 'camera_fps': round(camera_stats['camera_fps'].value, 2), -# 'process_fps': round(camera_stats['process_fps'].value, 2), -# 'skipped_fps': round(camera_stats['skipped_fps'].value, 2), -# 'detection_fps': round(camera_stats['detection_fps'].value, 2), -# 'pid': camera_stats['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'] -# } -# } + for name, camera_stats in camera_metrics.items(): + total_detection_fps += camera_stats['detection_fps'].value + stats[name] = { + 'camera_fps': round(camera_stats['camera_fps'].value, 2), + 'process_fps': round(camera_stats['process_fps'].value, 2), + 'skipped_fps': round(camera_stats['skipped_fps'].value, 2), + 'detection_fps': round(camera_stats['detection_fps'].value, 2), + 'pid': camera_stats['process'].pid, + 'capture_pid': camera_stats['capture_process'].pid + } -# stats['detectors'] = {} -# for name, detector in detectors.items(): -# stats['detectors'][name] = { -# 'inference_speed': round(detector.avg_inference_speed.value*1000, 2), -# 'detection_start': detector.detection_start.value, -# 'pid': detector.detect_process.pid -# } -# stats['detection_fps'] = round(total_detection_fps, 2) + stats['detectors'] = {} + for name, detector in current_app.detectors.items(): + stats['detectors'][name] = { + 'inference_speed': round(detector.avg_inference_speed.value*1000, 2), + 'detection_start': detector.detection_start.value, + 'pid': detector.detect_process.pid + } + stats['detection_fps'] = round(total_detection_fps, 2) -# return jsonify(stats) + return jsonify(stats) -# @app.route('//