生成式 AI
生成式 AI 可用于根据跟踪目标的缩略图自动生成描述性文本。这有助于在 Frigate 中进行 语义搜索,为您的跟踪目标提供更多上下文信息。描述可以通过 Frigate 用户界面中的 浏览
视图访问,点击跟踪目标的缩略图即可查看。
必须启用语义搜索才能使用生成式 AI。
配置
生成式 AI 可以为所有摄像头启用,也可以仅为特定摄像头启用。目前有 3 种 providers 可以与 Frigate 集成。
如果您选择的 providers 需要 API 密钥,您可以直接将其粘贴到配置中,或将其存储在以 FRIGATE_
为前缀的环境变量中。
genai:
enabled: True
provider: gemini
api_key: "{FRIGATE_GEMINI_API_KEY}"
model: gemini-1.5-flash
cameras:
front_camera: ...
indoor_camera:
genai: # <- disable GenAI for your indoor camera
enabled: False
Ollama
不建议在 CPU 上使用 Ollama,推理时间较长会使生成式 AI 的使用变得不切实际。
Ollama 允许您自托管大型语言模型并保持一切在本地运行。它提供了基于 llama.cpp 的友好 API。强烈建议在配备 Nvidia 显卡的机器或 Apple Silicon Mac 上托管此服务器,以获得最佳性能。
大多数 7b 参数的 4 位视觉模型都可以在 8GB 的显存中运行。这里有一个 Docker 容器 可用。
并行请求也有一些注意事项。您需要设置 OLLAMA_NUM_PARALLEL=1
,并选择适合您硬件和偏好的 OLLAMA_MAX_QUEUE
和 OLLAMA_MAX_LOADED_MODELS
值。请参阅 Ollama 文档。
支持的模型
您必须使用支持视觉的模型与 Frigate 配合使用。当前的模型变体可以在 模型库 中找到。截至撰写时,这包括 llava
、llava-llama3
、llava-phi3
和 moondream
。请注意,Frigate 不会自动下载您在配置中指定的模型,您必须先将模型下载到本地的 Ollama 实例中,即在 Ollama 服务器/Docker 容器上运行 ollama pull llava:7b
。请注意,Frigate 配置中指定的模型必须与下载的模型标签匹配。
运行 7B 模型至少需要 8 GB 的可用内存(或 GPU 上的显存),运行 13B 模型需要 16 GB,运行 33B 模型需要 32 GB。
配置
genai:
enabled: True
provider: ollama
base_url: http://localhost:11434
model: llava:7b
Google Gemini
Google Gemini 提供了一个免费层级,允许每分钟 15 次查询 API,这对于标准的 Frigate 使用来说已经足够了。
支持的模型
您必须使用支持视觉的模型与 Frigate 配合使用。当前的模型变体可以在 文档 中找到。截至撰写时,这包括 gemini-1.5-pro
和 gemini-1.5-flash
。
获取 API 密钥
要开始使用 Gemini,您必须先从 Google AI Studio 获取 API 密钥。
- 接受服务条款
- 点击右侧导航中的 "Get API Key"
- 点击 "Create API key in new project"
- 复制 API 密钥以在配置中使用
配置
genai:
enabled: True
provider: gemini
api_key: "{FRIGATE_GEMINI_API_KEY}"
model: gemini-1.5-flash
OpenAI
OpenAI 的 API 没有免费层级。随着 gpt-4o 的发布,价格已经降低,如果您选择此路线,每次生成的成本应该花费几美分。
支持的模型
您必须使用支持视觉的模型与 Frigate 配合使用。当前的模型变体可以在 文档 中找到。截至撰写时,这包括 gpt-4o
和 gpt-4-turbo
。
获取 API 密钥
要开始使用 OpenAI,您必须首先 创建 API 密钥 并 配置计费。
配置
genai:
enabled: True
provider: openai
api_key: "{FRIGATE_OPENAI_API_KEY}"
model: gpt-4o
Azure OpenAI
微软通过 Azure OpenAI 提供了多种视觉模型。需要订阅。
支持的模型
您必须使用支持视觉的模型与 Frigate 配合使用。当前的模型变体可以在 文档 中找到。截至撰写时,这包括 gpt-4o
和 gpt-4-turbo
。
创建资源并获取 API 密钥
要开始使用 Azure OpenAI,您必须首先 创建资源。您需要 API 密钥和资源 URL,其中必须包含 api-version
参数(参见下面的示例)。配置中不需要 model 字段,因为模型是您部署资源时选择的部署名称的一部分。
配置
genai:
enabled: True
provider: azure_openai
base_url: https://example-endpoint.openai.azure.com/openai/deployments/gpt-4o/chat/completions?api-version=2023-03-15-preview
api_key: "{FRIGATE_OPENAI_API_KEY}"
使用和最佳实践
Frigate 的缩略图搜索擅长识别跟踪目标的特定细节——例如,使用“图像标题”方法来查找“穿着黄色背心的人”、“在草坪上奔跑的白狗”或“在居民区街道上的红色汽车”。为了进一步增强这一点,Frigate 的默认提示词旨在询问您的 AI 提供商有关目标行为背后的意图,而不仅仅是描述其外观。
虽然生成检测目标的简单描述很有用,但理解意图提供了更深层次的洞察。Frigate 的默认提示词不仅识别场景中的“什么”,还推断“为什么”它可能在那里或“接下来”它可能会做什么。描述告诉您发生了什么,但意图提供了上下文。例如,一个人走向门可能看起来像访客,但如果他们在下班后快速移动,您可以推断出潜在的闯入企图。检测到一个人在夜间在门附近徘徊可以比仅仅注意到“一个人站在门旁”更快地触发警报,帮助您根据情况做出响应。
使用生成式 AI 进行通知
Frigate 提供了一个 MQTT 主题,frigate/tracked_object_update
,当您的 AI 提供商返回跟踪目标的描述时,该主题会更新为包含 event_id
和 description
的 JSON 有效负载。此描述可以直接用于通知,例如发送警报到您的手机或进行音频公告。如果需要从跟踪目标中获取更多详细信息,您可以使用 event_id
查询 HTTP API,例如:http://frigate_ip:5000/api/events/<event_id>
。
自定义提示词
Frigate 会从跟踪目标中发送多帧图像以及提示给您的生成式 AI 提供商,要求其生成描述。默认提示如下:
Analyze the sequence of images containing the {label}. Focus on the likely intent or behavior of the {label} based on its actions and movement, rather than describing its appearance or the surroundings. Consider what the {label} is doing, why, and what it might do next.
提示词可以使用变量替换,如 {label}
、{sub_label}
和 {camera}
,以将跟踪目标的信息作为提示的一部分替换。
您还可以在配置中定义自定义提示词。
genai:
enabled: True
provider: ollama
base_url: http://localhost:11434
model: llava
prompt: "Analyze the {label} in these images from the {camera} security camera. Focus on the actions, behavior, and potential intent of the {label}, rather than just describing its appearance."
object_prompts:
person: "Examine the main person in these images. What are they doing and what might their actions suggest about their intent (e.g., approaching a door, leaving an area, standing still)? Do not describe the surroundings or static details."
car: "Observe the primary vehicle in these images. Focus on its movement, direction, or purpose (e.g., parking, approaching, circling). If it's a delivery vehicle, mention the company."
提示词也可以在摄像头级别覆盖,以便为模型提供有关您特定摄像头的更详细提示(如果您希望)。默认情况下,会为所有跟踪目标和所有防区生成描述。但您也可以选择指定 objects
和 required_zones
,以仅生成某些跟踪目标或防区的描述。
可选地,您可以通过将 use_snapshot
设置为 True
来使用抓拍生成描述(如果启用)。默认情况下,此设置为 False
,它会将目标生命周期内从 detect
流中收集的未压缩图像发送给模型。一旦目标生命周期结束,只会保存一个压缩和裁剪的缩略图与跟踪目标一起。使用抓拍可能在您想要 重新生成 跟踪目标的描述时有用,因为它会为 AI 提供比裁剪/压缩的缩略图更高质量的图像(通常由 AI 本身缩小)。使用抓拍的另一个权衡是,只有一张图像会发送给您的生成式 AI 提供商,这将限制模型确定目标移动或方向的能力。
cameras:
front_door:
genai:
use_snapshot: True
prompt: "Analyze the {label} in these images from the {camera} security camera at the front door. Focus on the actions and potential intent of the {label}."
object_prompts:
person: "Examine the person in these images. What are they doing, and how might their actions suggest their purpose (e.g., delivering something, approaching, leaving)? If they are carrying or interacting with a package, include details about its source or destination."
cat: "Observe the cat in these images. Focus on its movement and intent (e.g., wandering, hunting, interacting with objects). If the cat is near the flower pots or engaging in any specific actions, mention it."
objects:
- person
- cat
required_zones:
- steps
尝试不同的提示词
许多生成式AI提供商还为其模型提供了公开的聊天界面。从 Frigate 下载几张不同的缩略图或抓拍,并在 playground 中尝试新内容,以在更新 Frigate 中的提示词之前获得您喜欢的描述。
- OpenAI - ChatGPT
- Gemini - Google AI Studio
- Ollama - Open WebUI