From bc528123b38ba27a786c49016749ee24ff5d1bbc Mon Sep 17 00:00:00 2001 From: Josh Hawkins <32435876+hawkeye217@users.noreply.github.com> Date: Sat, 5 Oct 2024 12:20:50 -0500 Subject: [PATCH] remove unnecessary re-embed --- frigate/embeddings/embeddings.py | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/frigate/embeddings/embeddings.py b/frigate/embeddings/embeddings.py index faf4f81d3..cfc96c2e7 100644 --- a/frigate/embeddings/embeddings.py +++ b/frigate/embeddings/embeddings.py @@ -105,6 +105,8 @@ class Embeddings: (event_id, serialize(embedding)), ) + return embedding + def upsert_description(self, event_id: str, description: str): # Generate embedding using MiniLM embedding = self.minilm_embedding([description])[0] @@ -117,6 +119,8 @@ class Embeddings: (event_id, serialize(embedding)), ) + return embedding + def delete_thumbnail(self, event_ids: List[str]) -> None: ids = ",".join(["?" for _ in event_ids]) self.db.execute_sql( @@ -147,11 +151,9 @@ class Embeddings: row[0] ) # Deserialize the thumbnail embedding else: - # If no embedding found, generate it + # If no embedding found, generate it and return it thumbnail = base64.b64decode(query.thumbnail) - self.upsert_thumbnail(query.id, thumbnail) - image = Image.open(io.BytesIO(thumbnail)).convert("RGB") - query_embedding = self.clip_embedding([image])[0] + query_embedding = self.upsert_thumbnail(query.id, thumbnail) else: query_embedding = self.clip_embedding([query])[0] @@ -166,9 +168,12 @@ class Embeddings: # Add the IN clause if event_ids is provided and not empty # this is the only filter supported by sqlite-vec as of 0.1.3 + # but it seems to be broken in this version if event_ids: sql_query += " AND id IN ({})".format(",".join("?" * len(event_ids))) + # order by distance DESC is not implemented in this version of sqlite-vec + # when it's implemented, we can use cosine similarity sql_query += " ORDER BY distance" parameters = ( @@ -198,9 +203,12 @@ class Embeddings: # Add the IN clause if event_ids is provided and not empty # this is the only filter supported by sqlite-vec as of 0.1.3 + # but it seems to be broken in this version if event_ids: sql_query += " AND id IN ({})".format(",".join("?" * len(event_ids))) + # order by distance DESC is not implemented in this version of sqlite-vec + # when it's implemented, we can use cosine similarity sql_query += " ORDER BY distance" parameters = (