Results & Boxes¶
DFINE.predict returns a list of Results (one per image); each holds the detected
Boxes (original-scale xyxy), with .plot()/.save() and interop converters
(to_pandas/to_coco/to_supervision).
For segmentation models a result also carries a segmentation container, both at the original image resolution:
task="segment"→Results.masks— aMasksobject whose rows align 1:1 withBoxes(.datais[N, H, W]bool).Results.plot()overlays each instance's mask under its box, andto_supervision()attaches the masks.task="sem_seg"→Results.sem_seg— aSemSegobject with a dense uint8[H, W]class-id map (255= void).plot()tints each class; these results are boxless (Boxesis empty).
On a detection result both are None.
dfine.results.Results ¶
Results(orig_img: Image, boxes: Boxes, names: dict[int, str], masks: Masks | None = None, sem_seg: SemSeg | None = None)
Detections for one image + helpers to visualize them.
Source code in dfine/results.py
plot ¶
Draw boxes+labels on a copy of the image; return an RGB HWC uint8 array.
When the boxes carry track ids (boxes.id), each label is prefixed with
#<id> and boxes are colored by track id so an object keeps its color.
Instance masks (self.masks), when present, are overlaid semi-transparently
in each detection's color before boxes/labels are drawn on top. A semantic-
segmentation label map (self.sem_seg) is overlaid per class with a palette
color (255 void pixels left untouched).
Source code in dfine/results.py
save ¶
Render via :meth:plot and write to filename; return the path.
to_pandas ¶
Return detections as a pandas.DataFrame (one row per box).
Columns xmin, ymin, xmax, ymax, confidence, class, name — the
ultralytics .pandas().xyxy[0] layout. An empty Results yields an
empty frame that still carries those columns. Requires pandas.
Source code in dfine/results.py
to_coco ¶
Detections as COCO-format result dicts (the loadRes layout).
Each box becomes {"image_id", "category_id", "bbox": [x, y, w, h],
"score"} with the bbox in COCO xywh (top-left + size, original-image
pixels). category_id is the contiguous class id this library predicts;
pass image_id to tag the detections with a dataset image id. When instance
masks are present (task="segment") each dict also carries a segmentation
in COCO uncompressed RLE ({"size": [h, w], "counts": [...]}, original
scale, aligned 1:1 with the box) — normalize to compressed RLE with the standard
pycocotools/faster_coco_eval frPyObjects when a tool needs it. Pure
Python — no extra dependency.
Source code in dfine/results.py
to_supervision ¶
Convert to a supervision.Detections (xyxy/confidence/class_id).
Boxes are the original-scale xyxy corners (float32); class ids are the
contiguous labels. Instance masks (when present) are attached as a bool
[N, H, W] mask array. Requires the supervision package.
Source code in dfine/results.py
save_txt ¶
Write detections to a YOLO-format .txt label file (ultralytics-style).
One line per detection, coordinates normalized to the original image size:
- detection —
class cx cy w h(box center + size,cxcywh); - segmentation —
class x1 y1 x2 y2 … xn yn(the mask's largest polygon; falls back to the box corners when a mask has no contour).
save_conf=True appends the confidence as the final field. Lines are
appended to txt_file (matching ultralytics — pass a per-image path), the
parent directory is created, and the format round-trips through
:func:~dfine.convert.yolo_to_coco. Returns the path, or None when there are
no detections (no file is written). Polygon extraction needs OpenCV
(pip install pydfine[video]); without it — or when a mask has no contour — the
row falls back to the box corners, so the detection path is dependency-free.
Source code in dfine/results.py
summary ¶
Detections as a list of plain dicts (ultralytics Results.summary() layout).
One dict per detection: {"name", "class", "confidence", "box": {x1,y1,x2,y2}}.
A track id (from :meth:DFINE.predict_video with track=True) adds
"track_id"; an instance mask adds "segments": {"x": [...], "y": [...]} (the
largest polygon). Coordinates are original-image pixels, or fractions of the image
when normalize=True; floats are rounded to decimals. Pure Python — the
result is JSON-serializable (see :meth:tojson).
Source code in dfine/results.py
tojson ¶
:meth:summary serialized to a JSON string (ultralytics Results.tojson).
save_crop ¶
Save each detection's cropped image to save_dir/<class_name>/ (ultralytics-style).
The original image is cropped to every box (clipped to the frame) and written under
a per-class subfolder as <file_name> — a numeric suffix (_2, _3, …) is
appended when several detections of the same class would collide, so nothing is
overwritten. Returns the list of written paths (empty when there are no detections).
Uses PIL only (dependency-free).
Source code in dfine/results.py
verbose ¶
A human-readable per-class summary, e.g. "2 persons, 1 car" (ultralytics-style).
Counts detections per class (naive plural s), ordered by class id. For a
sem_seg result it instead lists the classes present in the label map (255
void excluded). Returns "(no detections)" / "(empty)" when there is nothing.
Source code in dfine/results.py
dfine.results.Boxes ¶
Detected boxes for one image: xyxy (pixels), conf, cls.
id holds per-box track ids when the boxes came from a tracker (e.g.
:meth:DFINE.predict_video with track=True); it is None otherwise.
Source code in dfine/results.py
dfine.results.Masks ¶
Per-instance binary masks for one image: data is [N, H, W] (original scale).
Rows align 1:1 with the image's :class:Boxes. data is a bool CPU tensor at the
original image resolution (post-threshold, cleaned to each box); cast as needed.
dfine.results.SemSeg ¶
Dense semantic-segmentation label map for one image: data is uint8 [H, W].
Each pixel holds a class id at the original image resolution; 255 is treated as
void/ignore (left un-tinted by :meth:Results.plot). Populated for task="sem_seg"
models; None on detection/instance-segmentation results.