From e72b9de945e7c263c12caa13472d7264baf3128a Mon Sep 17 00:00:00 2001 From: alexyao2015 <33379584+alexyao2015@users.noreply.github.com> Date: Sun, 17 Mar 2024 16:31:47 -0500 Subject: [PATCH] enable on inprogress event --- docs/docs/integrations/api.md | 2 +- frigate/api/media.py | 73 ++++++++++++++++++++++++----------- 2 files changed, 51 insertions(+), 24 deletions(-) diff --git a/docs/docs/integrations/api.md b/docs/docs/integrations/api.md index 6078ccdf8..c6a832560 100644 --- a/docs/docs/integrations/api.md +++ b/docs/docs/integrations/api.md @@ -292,7 +292,7 @@ Returns the clip for the event id. Works after the event has ended. ### `GET /api/events//snapshot-clean.png` -Returns the clean snapshot image for the event id. Only works after an event has completed and if `snapshots` and `clean_copy` are enabled in the config. +Returns the clean snapshot image for the event id. Only works if `snapshots` and `clean_copy` are enabled in the config. | param | Type | Description | | ---------- | ---- | ------------------ | diff --git a/frigate/api/media.py b/frigate/api/media.py index ec5272014..78e8c711e 100644 --- a/frigate/api/media.py +++ b/frigate/api/media.py @@ -938,13 +938,37 @@ def grid_snapshot(camera_name): @MediaBp.route("/events//snapshot-clean.png") def event_snapshot_clean(id): download = request.args.get("download", type=bool) + png_bytes = None try: event = Event.get(Event.id == id) - if event.end_time is None: + snapshot_config = current_app.frigate_config.cameras[event.camera].snapshots + if not (snapshot_config.enabled and event.has_snapshot): return make_response( - jsonify({"success": False, "message": "Event not complete"}), 404 + jsonify( + { + "success": False, + "message": "Snapshots and clean_copy must be enabled in the config", + } + ), + 404, ) - if not event.has_snapshot: + if event.end_time is None: + # see if the object is currently being tracked + try: + camera_states = ( + current_app.detected_frames_processor.camera_states.values() + ) + for camera_state in camera_states: + if id in camera_state.tracked_objects: + tracked_obj = camera_state.tracked_objects.get(id) + if tracked_obj is not None: + png_bytes = tracked_obj.get_clean_png() + break + except Exception: + return make_response( + jsonify({"success": False, "message": "Event not found"}), 404 + ) + elif not event.has_snapshot: return make_response( jsonify({"success": False, "message": "Snapshot not available"}), 404 ) @@ -952,27 +976,30 @@ def event_snapshot_clean(id): return make_response( jsonify({"success": False, "message": "Event not found"}), 404 ) - try: - clean_snapshot_path = os.path.join( - CLIPS_DIR, f"{event.camera}-{event.id}-clean.png" - ) - if not os.path.exists(clean_snapshot_path): - return make_response( - jsonify({"success": False, "message": "Clean snapshot not available"}), - 404, + if png_bytes is None: + try: + clean_snapshot_path = os.path.join( + CLIPS_DIR, f"{event.camera}-{event.id}-clean.png" + ) + if not os.path.exists(clean_snapshot_path): + return make_response( + jsonify( + {"success": False, "message": "Clean snapshot not available"} + ), + 404, + ) + with open( + os.path.join(CLIPS_DIR, f"{event.camera}-{event.id}-clean.png"), "rb" + ) as image_file: + png_bytes = image_file.read() + except Exception: + logger.error(f"Unable to load clean png for event: {event.id}") + return make_response( + jsonify( + {"success": False, "message": "Unable to load clean png for event"} + ), + 400, ) - with open( - os.path.join(CLIPS_DIR, f"{event.camera}-{event.id}-clean.png"), "rb" - ) as image_file: - png_bytes = image_file.read() - except Exception: - logger.error(f"Unable to load clean png for event: {event.id}") - return make_response( - jsonify( - {"success": False, "message": "Unable to load clean png for event"} - ), - 400, - ) response = make_response(png_bytes) response.headers["Content-Type"] = "image/png" response.headers["Cache-Control"] = "private, max-age=31536000"