Models¶
Model is the base class. Every wrapper below subclasses it and fills in the
methods for the roles it can play. See Models for which model
fills which role.
Model
¶
Base class for both roles.
A primary implements track. A secondary implements find, name,
or both, depending on which refiner mode it is meant to serve. Anything not
implemented raises, so a mismatch between model and mode fails loudly on the
first frame instead of silently doing nothing.
names is the class id to name mapping the model works in. Leave it None
if the model has no fixed vocabulary.
track
¶
find
¶
name
¶
Classify a BGR crop. Returns a class id, or None if unsure.
hint is what the primary thought the object was.
batch
¶
Classify several BGR crops. Returns one class id or None per crop.
The default asks name once per crop, so a model that only implements
name works unchanged. Override this when the model can do the whole
list in one call, which is what makes crop mode cheap.
hints is what the primary thought each object was, in the same order.
Source code in vizor/models/base.py
grid
¶
Classify collages, each one object shown tiles times over a video.
Returns one class id or None per collage, the same shape as batch.
The default forwards to batch, which treats a collage as an ordinary
image, so a model that has never heard of collages still answers.
Override it to word the prompt for a grid.
Source code in vizor/models/base.py
menu
¶
Render a class mapping as id: name lines for a prompt.
Source code in vizor/models/base.py
parse_id
¶
Pull a class id out of a model's reply. None if there isn't a usable one.
Source code in vizor/models/base.py
parse_ids
¶
Pull n class ids out of a batched reply, in order.
A reply with too few numbers is padded with None, one with too many is cut.
Either way the caller gets exactly n entries, so a model that miscounts
costs some crops their refinement rather than shifting every later answer
onto the wrong object.
Source code in vizor/models/base.py
VLM
¶
Bases: Model
A hosted vision model asked which class a crop is.
These models describe an image well but do not give boxes, so use them with
mode="crop". The primary keeps the box, the VLM only fixes the class.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
model id, e.g. |
required |
api
|
str
|
|
'openai'
|
key
|
str | None
|
API key. Read from |
None
|
url
|
str | None
|
base url, for self-hosted or third party OpenAI-compatible servers.
Gemini has one built in, so |
None
|
prompt
|
str | None
|
format string overriding the default. It is given |
None
|
chunk
|
int
|
how many crops go in one request. 1 sends them one at a time. |
8
|
batch
|
str | None
|
format string overriding the batched prompt. It is given |
None
|
grid
|
str | None
|
format string overriding the collage prompt, used by |
None
|
kw
|
Any
|
forwarded to the chat completion call, e.g. |
{}
|
Never pass a key as a literal in code you commit. Put it in the environment.
Source code in vizor/models/api.py
encode
staticmethod
¶
BGR array to a base64 data url the chat APIs accept.
Source code in vizor/models/api.py
ask
¶
Send one or more images with one question, return the reply as a string.
Several images go in a single message, each preceded by its number, so the model can answer about all of them at once.
Source code in vizor/models/api.py
name
¶
Ask the model which class the crop is. Returns a class id, or None if unsure.
Source code in vizor/models/api.py
batch
¶
Classify several crops, chunk of them per request.
One request carrying eight crops costs one round trip instead of eight, which is most of the wall clock in crop mode. The trade is that the model has to keep the order straight, and a reply with the wrong count loses the crops it did not cover rather than shifting the rest.
Source code in vizor/models/api.py
grid
¶
Ask about one collage per request. Returns a class id or None per collage.
chunk does not apply here. Several grids in one request would ask the
model to keep the tiles and the grids straight at the same time, and
collage mode already fires once per track rather than once per frame, so
the round trips are not where the cost is.
Source code in vizor/models/api.py
HF
¶
Bases: Model
A chat-style VLM loaded locally with transformers.
Like the hosted models it classifies crops but does not localise, so use it
with mode="crop".
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
hub id or local path. |
required |
device
|
str | None
|
|
None
|
dtype
|
str | None
|
torch dtype, defaults to bfloat16 on GPU and float32 on CPU. |
None
|
prompt
|
str | None
|
format string overriding the default. Given |
None
|
grid
|
str | None
|
format string overriding the collage prompt, used by |
None
|
gen
|
dict[str, Any] | None
|
generation keyword arguments, e.g. |
None
|
Source code in vizor/models/hf.py
ask
¶
Send one image and one question, return the decoded reply.
Source code in vizor/models/hf.py
name
¶
Ask the model which class the crop is. Returns a class id, or None if unsure.
Source code in vizor/models/hf.py
grid
¶
Ask about each collage in its own forward pass, with the grid prompt.
Source code in vizor/models/hf.py
Florence
¶
Florence(model='microsoft/Florence-2-base-ft', names=None, task='<CAPTION_TO_PHRASE_GROUNDING>', **kw)
Bases: HF
Microsoft Florence-2, used as an open-vocabulary detector.
Florence grounds phrases to boxes, so it works in mode="full": it labels
the whole frame in one pass and the refiner matches those boxes to the
tracks by IoU. It gives no confidence, so every box comes back at 1.0.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
model
|
str
|
hub id, e.g. |
'microsoft/Florence-2-base-ft'
|
names
|
dict[int, str] | list[str] | None
|
class id to name mapping. Labels outside it are dropped. |
None
|
task
|
str
|
task token. |
'<CAPTION_TO_PHRASE_GROUNDING>'
|
Source code in vizor/models/hf.py
run
¶
Run one Florence task and return its parsed dict.
Source code in vizor/models/hf.py
find
¶
Ground the class names in a BGR frame. Every box comes back at confidence 1.0.
Source code in vizor/models/hf.py
name
¶
Class of the largest thing Florence finds in the crop.
Source code in vizor/models/hf.py
grid
¶
Largest thing Florence grounds in each collage. It has no chat prompt to word.
Pkl
¶
Bases: Model
One saved list of per-frame boxes, replayed in order.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file
|
str | Path
|
pickle holding a list of (N, 6) or (N, 7) arrays or tensors. |
required |
cols
|
list[int] | None
|
column order to reindex each frame into |
None
|
names
|
dict[int, str] | list[str] | None
|
class id to name mapping to hand downstream. |
None
|
Frames are handed out in order on each track or find call, so the
file must line up with the video you feed the pipeline.
Source code in vizor/models/pkl.py
reset
¶
next
¶
The next frame as a float32 array, or an empty one past the end.