Skip to content

NVR with realtime local object detection for IP cameras

save genai debug thumbnails on early requests (#24615)

Josh Hawkins2e92251aa593
diff --git a/frigate/data_processing/post/object_descriptions.py b/frigate/data_processing/post/object_descriptions.py
index 9612ce7be9..a103b2f929 100644
--- a/frigate/data_processing/post/object_descriptions.py
+++ b/frigate/data_processing/post/object_descriptions.py
@@ -105,6 +105,10 @@ class ObjectDescriptionProcessor(PostProcessorApi):
logger.debug(f"{camera} sending early request to GenAI")
self.early_request_sent[data["id"]] = True
+
+ if camera_config.objects.genai.debug_save_thumbnails:
+ self._save_debug_thumbnails(data["id"])
+
# Copy thumbnails to avoid holding references after cleanup
thumbnails_copy = [
data["thumbnail"][:] if data.get("thumbnail") else None
@@ -297,26 +301,7 @@ class ObjectDescriptionProcessor(PostProcessorApi):
)
if camera_config.objects.genai.debug_save_thumbnails and num_thumbnails > 0:
- logger.debug(f"Saving {num_thumbnails} thumbnails for event {event_id}")
-
- Path(os.path.join(CLIPS_DIR, f"genai-requests/{event_id}")).mkdir(
- parents=True, exist_ok=True
- )
-
- for idx, data in enumerate(self.tracked_events[event_id], 1):
- jpg_bytes: bytes | None = data["thumbnail"]
-
- if jpg_bytes is None:
- logger.warning(f"Unable to save thumbnail {idx} for {event_id}.")
- else:
- with open(
- os.path.join(
- CLIPS_DIR,
- f"genai-requests/{event_id}/{idx}.jpg",
- ),
- "wb",
- ) as j:
- j.write(jpg_bytes)
+ self._save_debug_thumbnails(event_id)
# Generate the description. Call happens in a thread since it is network bound.
threading.Thread(
@@ -332,6 +317,30 @@ class ObjectDescriptionProcessor(PostProcessorApi):
# Clean up tracked events and early request state
self.cleanup_event(event_id)
+ def _save_debug_thumbnails(self, event_id: str) -> None:
+ """Write the tracked thumbnails for an event to the genai-requests dir."""
+ tracked = self.tracked_events.get(event_id, [])
+ logger.debug(f"Saving {len(tracked)} thumbnails for event {event_id}")
+
+ Path(os.path.join(CLIPS_DIR, f"genai-requests/{event_id}")).mkdir(
+ parents=True, exist_ok=True
+ )
+
+ for idx, data in enumerate(tracked, 1):
+ jpg_bytes: bytes | None = data["thumbnail"]
+
+ if jpg_bytes is None:
+ logger.warning(f"Unable to save thumbnail {idx} for {event_id}.")
+ else:
+ with open(
+ os.path.join(
+ CLIPS_DIR,
+ f"genai-requests/{event_id}/{idx}.jpg",
+ ),
+ "wb",
+ ) as j:
+ j.write(jpg_bytes)
+
def _genai_embed_description(self, event: Event, thumbnails: list[bytes]) -> None:
"""Embed the description for an event."""
start = datetime.datetime.now().timestamp()