Files
frigate/frigate/comms/embeddings_updater.py
T

92 lines
2.6 KiB
Python
Raw Normal View History

2024-10-10 09:42:24 -06:00
"""Facilitates communication between processes."""
2025-08-08 06:08:37 -06:00
import logging
2024-10-10 09:42:24 -06:00
from enum import Enum
2025-05-13 16:27:20 +02:00
from typing import Any, Callable
2024-10-10 09:42:24 -06:00
import zmq
2025-08-08 06:08:37 -06:00
logger = logging.getLogger(__name__)
2024-10-10 09:42:24 -06:00
SOCKET_REP_REQ = "ipc:///tmp/cache/embeddings"
class EmbeddingsRequestEnum(Enum):
2025-06-05 09:13:12 -06:00
# audio
transcribe_audio = "transcribe_audio"
# custom classification
2025-06-09 08:25:33 -06:00
reload_classification_model = "reload_classification_model"
2025-06-05 09:13:12 -06:00
# face
2025-01-18 10:52:01 -07:00
clear_face_classifier = "clear_face_classifier"
2025-03-19 09:02:25 -06:00
recognize_face = "recognize_face"
2024-10-22 16:05:48 -06:00
register_face = "register_face"
2025-01-29 07:41:35 -07:00
reprocess_face = "reprocess_face"
2025-06-05 09:13:12 -06:00
# semantic search
embed_description = "embed_description"
embed_thumbnail = "embed_thumbnail"
generate_search = "generate_search"
2025-03-27 12:29:34 -05:00
reindex = "reindex"
2025-06-05 09:13:12 -06:00
# LPR
reprocess_plate = "reprocess_plate"
2025-08-12 16:27:35 -06:00
# Review Descriptions
summarize_review = "summarize_review"
2024-10-10 09:42:24 -06:00
class EmbeddingsResponder:
def __init__(self) -> None:
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REP)
self.socket.bind(SOCKET_REP_REQ)
def check_for_request(self, process: Callable) -> None:
while True: # load all messages that are queued
2024-10-22 16:05:48 -06:00
has_message, _, _ = zmq.select([self.socket], [], [], 0.01)
2024-10-10 09:42:24 -06:00
if not has_message:
break
try:
2025-08-08 06:08:37 -06:00
raw = self.socket.recv_json(flags=zmq.NOBLOCK)
2024-10-10 09:42:24 -06:00
2025-08-08 06:08:37 -06:00
if isinstance(raw, list):
(topic, value) = raw
response = process(topic, value)
else:
logging.warning(
f"Received unexpected data type in ZMQ recv_json: {type(raw)}"
)
response = None
2024-10-10 09:42:24 -06:00
if response is not None:
self.socket.send_json(response)
else:
self.socket.send_json([])
except zmq.ZMQError:
break
def stop(self) -> None:
self.socket.close()
self.context.destroy()
class EmbeddingsRequestor:
"""Simplifies sending data to EmbeddingsResponder and getting a reply."""
def __init__(self) -> None:
self.context = zmq.Context()
self.socket = self.context.socket(zmq.REQ)
self.socket.connect(SOCKET_REP_REQ)
2025-08-08 06:08:37 -06:00
def send_data(self, topic: str, data: Any) -> Any:
2024-10-10 09:42:24 -06:00
"""Sends data and then waits for reply."""
2024-10-10 15:37:43 -06:00
try:
self.socket.send_json((topic, data))
return self.socket.recv_json()
except zmq.ZMQError:
return ""
2024-10-10 09:42:24 -06:00
def stop(self) -> None:
self.socket.close()
self.context.destroy()