Skip to content

Commit

Permalink
Added option to service to prepend the frigate ID to stored images to…
Browse files Browse the repository at this point in the history
… avoid them being overwrriten each run.
  • Loading branch information
rsteckler committed Dec 10, 2024
1 parent 8350a4c commit 3a43533
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 3 deletions.
5 changes: 4 additions & 1 deletion custom_components/llmvision/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
DETAIL,
INCLUDE_FILENAME,
EXPOSE_IMAGES,
EXPOSE_IMAGES_PERSIST,
SENSOR_ENTITY,
)
from .calendar import SemanticIndex
Expand Down Expand Up @@ -234,6 +235,7 @@ def __init__(self, data_call):
self.detail = str(data_call.data.get(DETAIL, "auto"))
self.include_filename = data_call.data.get(INCLUDE_FILENAME, False)
self.expose_images = data_call.data.get(EXPOSE_IMAGES, False)
self.expose_images_persist = data_call.data.get(EXPOSE_IMAGES_PERSIST, False)
self.sensor_entity = data_call.data.get(SENSOR_ENTITY)

def get_service_call_data(self):
Expand Down Expand Up @@ -285,7 +287,8 @@ async def video_analyzer(data_call):
max_frames=call.max_frames,
target_width=call.target_width,
include_filename=call.include_filename,
expose_images=call.expose_images
expose_images=call.expose_images,
expose_images_persist=call.expose_images_persist
)
response = await client.make_request(call)
await _remember(hass, call, start, response)
Expand Down
1 change: 1 addition & 0 deletions custom_components/llmvision/const.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
TEMPERATURE = 'temperature'
INCLUDE_FILENAME = 'include_filename'
EXPOSE_IMAGES = 'expose_images'
EXPOSE_IMAGES_PERSIST = 'expose_images_persist'
SENSOR_ENTITY = 'sensor_entity'

# Error messages
Expand Down
11 changes: 9 additions & 2 deletions custom_components/llmvision/media_handlers.py
Original file line number Diff line number Diff line change
Expand Up @@ -294,13 +294,14 @@ async def add_images(self, image_entities, image_paths, target_width, include_fi
raise ServiceValidationError(f"Error: {e}")
return self.client

async def add_videos(self, video_paths, event_ids, max_frames, target_width, include_filename, expose_images):
async def add_videos(self, video_paths, event_ids, max_frames, target_width, include_filename, expose_images, expose_images_persist):
"""Wrapper for client.add_frame for videos"""
tmp_clips_dir = f"/config/custom_components/{DOMAIN}/tmp_clips"
tmp_frames_dir = f"/config/custom_components/{DOMAIN}/tmp_frames"

if not video_paths:
video_paths = []
processed_event_ids = []
if event_ids:
for event_id in event_ids:
try:
Expand All @@ -323,14 +324,17 @@ async def add_videos(self, video_paths, event_ids, max_frames, target_width, inc
f"Saved frigate clip to {clip_path} (temporarily)")
# append to video_paths
video_paths.append(clip_path)
processed_event_ids.append(event_id)

except AttributeError as e:
raise ServiceValidationError(
f"Failed to fetch frigate clip {event_id}: {e}")
if video_paths:
_LOGGER.debug(f"Processing videos: {video_paths}")
video_count = 0
for video_path in video_paths:
try:
current_event_id = processed_event_ids[video_count]
video_path = video_path.strip()
if os.path.exists(video_path):
# create tmp dir to store extracted frames
Expand Down Expand Up @@ -395,7 +399,10 @@ async def add_videos(self, video_paths, event_ids, max_frames, target_width, inc
for frame_path, _ in sorted_frames:
resized_image = await self.resize_image(image_path=frame_path, target_width=target_width)
if expose_images:
await self._save_clip(image_path="/config/www/llmvision/" + frame_path.split("/")[-1], image_data=resized_image)
persist_filename = f"/config/www/llmvision/" + frame_path.split("/")[-1]
if expose_images_persist:
persist_filename = f"/config/www/llmvision/{current_event_id}-" + frame_path.split("/")[-1]
await self._save_clip(image_data=resized_image, image_path=persist_filename)
self.client.add_frame(
base64_image=resized_image,
filename=video_path.split('/')[-1].split('.')[-2] + " (frame " + str(counter) + ")" if include_filename else "Video frame " + str(counter)
Expand Down
9 changes: 9 additions & 0 deletions custom_components/llmvision/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,15 @@ video_analyzer:
default: false
selector:
boolean:
expose_images_persist:
name: Persist Exposed Images
description: Normally exposed images are re-written with each new event. Setting this to true will include the Frigate eventID, if available, as part of the filename. If there is no
Frigate eventID, a guid will be used instead.
required: false
example: false
default: false
selector:
boolean:

stream_analyzer:
name: Stream Analyzer
Expand Down

0 comments on commit 3a43533

Please sign in to comment.