VLM / LLM — Phân tích ngữ nghĩa bằng AI
SDK có các node kết nối tới Vision-Language Model (VLM) và LLM để phân tích ngữ nghĩa ảnh/video: mô tả đối tượng, trích thuộc tính (màu áo, loại xe...), enrich event.
Backend hỗ trợ
| Backend | Giao thức | Ví dụ endpoint |
|---|---|---|
| Ollama | Ollama API | http://127.0.0.1:11434 |
| llama.cpp server | OpenAI-compatible | http://127.0.0.1:8080 |
| OpenAI / vLLM | OpenAI API | https://api.openai.com |
Chuẩn bị (Ollama)
bash
# Cài Ollama rồi pull model VLM
ollama pull qwen3-vl:latest # bản đầy đủ (~6GB)
ollama pull qwen3-vl:2b # bản nhẹ (~2GB)Node VLM Feature
Gắn sau detector/tracker — trích đặc trưng ngữ nghĩa cho từng object:
cpp
#include <cvedix/nodes/infers/cvedix_vlm_feature_node.h>
auto vlm = std::make_shared<cvedix_nodes::cvedix_vlm_feature_node>(
"vlm_feature_0",
"qwen3-vl:latest", // model
"http://127.0.0.1:11434", // API base URL
"", // API key (nếu cần)
llmlib::LLMBackendType::Ollama, // backend
8, // run_every_n_frames
4, // max_targets_per_frame
0.03f, 0.03f, 10);
vlm->attach_to({tracker});Kết quả gắn vào target:
secondary_labels: nhãn dạngvlm:object:red text, rectangular sign...embeddings: vector đặc trưng (128 chiều)
Nếu VLM timeout, node tự fallback sang đặc trưng histogram + gradient (nhãn có hậu tố :fallback) — pipeline không bao giờ bị treo.
Node RapidMedia VLM (production)
Bản nâng cao có pre-filter (chỉ xử lý object có track_id, lọc theo diện tích/label) + metrics giám sát:
cpp
#include <cvedix/nodes/infers/cvedix_rapidmedia_vlm_feature_node.h>
auto vlm = std::make_shared<cvedix_nodes::cvedix_rapidmedia_vlm_feature_node>(
"rapidmedia_vlm_0",
"qwen3-vl:2b",
"http://127.0.0.1:8080",
"",
llmlib::LLMBackendType::OpenAI, // llama.cpp server
4, 4, 0.02f, 0.02f, 12);Tinh chỉnh qua biến môi trường:
| Biến | Mặc định | Ý nghĩa |
|---|---|---|
ASS_RAPIDMEDIA_VLM_REQUIRE_TRACK | true | Chỉ xử lý target đã có track_id |
ASS_RAPIDMEDIA_VLM_MIN_AREA_RATIO | 0.005 | Diện tích bbox tối thiểu (tỷ lệ so với frame) |
ASS_RAPIDMEDIA_VLM_ALLOW_LABELS | — | Chỉ xử lý các label này (person,car) |
ASS_RAPIDMEDIA_VLM_DENY_LABELS | — | Loại trừ label |
ASS_RAPIDMEDIA_VLM_METRICS_ENABLED | true | Xuất metrics định kỳ ra log |
Phân tích ảnh tĩnh (batch)
Dùng sample enrichment để phân tích thư mục ảnh snapshot:
bash
./event_snapshot_vlm_enrichment_sample \
--input /path/to/images \
--model qwen3-vl:latest \
--api http://127.0.0.1:11434 \
--backend ollama \
--output /tmp/results.jsonlOutput JSONL mỗi dòng một ảnh:
json
{"image":"/path/img.jpg","embedding_dim":128,"vlm_label":"vlm:object:red text, rectangular sign, pinkish background"}Hiệu năng
- Tăng
run_every_n_frameskhi FPS nguồn cao — không cần VLM mọi frame. - Giới hạn
max_targets_per_frameđể latency ổn định. - Dùng model nhỏ (
qwen3-vl:2b) cho realtime, model lớn cho batch/offline. - VLM là semantic feature extractor — không thay thế detector.