pixelflow 0.9.0__tar.gz → 0.11.0__tar.gz
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- {pixelflow-0.9.0/pixelflow.egg-info → pixelflow-0.11.0}/PKG-INFO +8 -1
- {pixelflow-0.9.0 → pixelflow-0.11.0}/README.md +7 -0
- pixelflow-0.11.0/docs/core-concepts/classifications.md +108 -0
- pixelflow-0.11.0/docs/core-concepts/converters.md +229 -0
- pixelflow-0.11.0/docs/core-concepts/detections.md +145 -0
- pixelflow-0.11.0/docs/core-concepts/filters.md +132 -0
- pixelflow-0.11.0/docs/core-concepts/saving-results.md +98 -0
- pixelflow-0.11.0/docs/index.md +76 -0
- pixelflow-0.11.0/docs/llms.txt +54 -0
- pixelflow-0.11.0/docs/tracking/crossings.md +120 -0
- pixelflow-0.11.0/docs/tracking/smoothing.md +101 -0
- pixelflow-0.11.0/docs/tracking/time-in-view.md +107 -0
- pixelflow-0.11.0/docs/tracking/tracker-moving-camera.md +129 -0
- pixelflow-0.11.0/docs/tracking/tracker.md +154 -0
- pixelflow-0.11.0/docs/tracking/zones.md +121 -0
- pixelflow-0.11.0/docs/utilities/buffer.md +121 -0
- pixelflow-0.11.0/docs/utilities/media.md +190 -0
- pixelflow-0.11.0/docs/utilities/sliced-inference.md +121 -0
- pixelflow-0.11.0/docs/utilities/transforms.md +148 -0
- pixelflow-0.11.0/docs/visualization/anchors.md +53 -0
- pixelflow-0.11.0/docs/visualization/blur.md +46 -0
- pixelflow-0.11.0/docs/visualization/box.md +38 -0
- pixelflow-0.11.0/docs/visualization/classification.md +48 -0
- pixelflow-0.11.0/docs/visualization/crossings.md +55 -0
- pixelflow-0.11.0/docs/visualization/filled-box.md +39 -0
- pixelflow-0.11.0/docs/visualization/fps-counter.md +49 -0
- pixelflow-0.11.0/docs/visualization/grid-overlay.md +48 -0
- pixelflow-0.11.0/docs/visualization/keypoint-skeleton.md +66 -0
- pixelflow-0.11.0/docs/visualization/keypoint.md +54 -0
- pixelflow-0.11.0/docs/visualization/label.md +75 -0
- pixelflow-0.11.0/docs/visualization/mask.md +45 -0
- pixelflow-0.11.0/docs/visualization/oval.md +47 -0
- pixelflow-0.11.0/docs/visualization/overview.md +93 -0
- pixelflow-0.11.0/docs/visualization/pixelate.md +42 -0
- pixelflow-0.11.0/docs/visualization/polygon.md +44 -0
- pixelflow-0.11.0/docs/visualization/zones.md +58 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/__init__.py +1 -1
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/media.py +122 -29
- {pixelflow-0.9.0 → pixelflow-0.11.0/pixelflow.egg-info}/PKG-INFO +8 -1
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow.egg-info/SOURCES.txt +34 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pyproject.toml +24 -3
- {pixelflow-0.9.0 → pixelflow-0.11.0}/LICENSE +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/NOTICE +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/anchors.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/blur.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/box.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/classification.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/crossing.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/dot.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/filled_box.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/fps_counter.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/grid_overlay.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/heatmap.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/keypoint.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/keypoint_skeleton.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/label.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/mask.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/motion_dots.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/motion_trails.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/oval.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/pixelate.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/polygon.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/scale_bar.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/utils.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/annotators/zones.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/arrays.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/buffer.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/classifications/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/classifications/classifications.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/classifications/converters.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/colors.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/crossings.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/detections/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/detections/converters.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/detections/detections.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/detections/filters.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/labels.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/masks.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/serialization.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/slicer.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/smoother.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/strategies.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/timer.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/UPSTREAM.md +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/base.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/botsort/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/botsort/tracker.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/botsort/tracklet.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/botsort/utils.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/bytetrack/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/bytetrack/tracker.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/bytetrack/tracklet.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/bytetrack/utils.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/cbiou/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/cbiou/tracker.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/ocsort/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/ocsort/tracker.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/ocsort/tracklet.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/ocsort/utils.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/sort/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/sort/tracker.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/sort/tracklet.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/sort/utils.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/base_tracklet.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/cmc.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/converters.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/iou.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/kalman_filter.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/matrix.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/motion_models.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/predict_timing.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/scale_aware_tracklet.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/tracker/utils/state_representations.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/transforms/__init__.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/transforms/detections.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/transforms/image.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/transforms/inverse.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/validators.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow/zones.py +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow.egg-info/dependency_links.txt +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow.egg-info/requires.txt +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/pixelflow.egg-info/top_level.txt +0 -0
- {pixelflow-0.9.0 → pixelflow-0.11.0}/setup.cfg +0 -0
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: pixelflow
|
|
3
|
-
Version: 0.
|
|
3
|
+
Version: 0.11.0
|
|
4
4
|
Summary: Computer vision processing library for visualizing and analyzing detection results from ML models
|
|
5
5
|
Author-email: Emrah Nazif <emrah@datamarkin.com>
|
|
6
6
|
License: Apache-2.0
|
|
@@ -474,6 +474,13 @@ detections = pf.Detections.from_dict(data)
|
|
|
474
474
|
|
|
475
475
|
Full documentation available at [https://emrah.fr/pixelflow/](https://emrah.fr/pixelflow/)
|
|
476
476
|
|
|
477
|
+
The docs pages also come with the package, so coding agents can read the docs of the
|
|
478
|
+
version you have installed, offline. `llms.txt` in that folder lists every page:
|
|
479
|
+
|
|
480
|
+
```bash
|
|
481
|
+
python -c "import pixelflow, pathlib; print(pathlib.Path(pixelflow.__file__).parent / 'docs')"
|
|
482
|
+
```
|
|
483
|
+
|
|
477
484
|
## Contributing
|
|
478
485
|
|
|
479
486
|
Contributions welcome! Please read our contributing guidelines.
|
|
@@ -436,6 +436,13 @@ detections = pf.Detections.from_dict(data)
|
|
|
436
436
|
|
|
437
437
|
Full documentation available at [https://emrah.fr/pixelflow/](https://emrah.fr/pixelflow/)
|
|
438
438
|
|
|
439
|
+
The docs pages also come with the package, so coding agents can read the docs of the
|
|
440
|
+
version you have installed, offline. `llms.txt` in that folder lists every page:
|
|
441
|
+
|
|
442
|
+
```bash
|
|
443
|
+
python -c "import pixelflow, pathlib; print(pathlib.Path(pixelflow.__file__).parent / 'docs')"
|
|
444
|
+
```
|
|
445
|
+
|
|
439
446
|
## Contributing
|
|
440
447
|
|
|
441
448
|
Contributions welcome! Please read our contributing guidelines.
|
|
@@ -0,0 +1,108 @@
|
|
|
1
|
+
# Classifications: what a model says about an image
|
|
2
|
+
|
|
3
|
+
<!--
|
|
4
|
+
summary: Read a classifier's answers as a Classifications object: take the best one, the top few, or every class above a score.
|
|
5
|
+
label: Classifications
|
|
6
|
+
keywords: pixelflow, image classification, top-k, class scores, zero-shot
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
A classification model does not find objects. It gives a score to every class it knows, for
|
|
10
|
+
the whole image. PixelFlow turns those scores into a `Classifications` object: one
|
|
11
|
+
`Classification` per class, each with a `class_id`, a `class_name` and a `confidence`.
|
|
12
|
+
|
|
13
|
+
The example below uses torchvision's ResNet-18, because Mayaku does not publish a
|
|
14
|
+
classification model.
|
|
15
|
+
|
|
16
|
+
```python
|
|
17
|
+
import torch
|
|
18
|
+
import pixelflow as pf
|
|
19
|
+
from torchvision.models import resnet18, ResNet18_Weights
|
|
20
|
+
|
|
21
|
+
weights = ResNet18_Weights.DEFAULT
|
|
22
|
+
model = resnet18(weights=weights).eval()
|
|
23
|
+
preprocess = weights.transforms()
|
|
24
|
+
|
|
25
|
+
image = pf.read_image("street.jpg")
|
|
26
|
+
batch = preprocess(pf.to_pil(image)).unsqueeze(0)
|
|
27
|
+
with torch.no_grad():
|
|
28
|
+
probabilities = model(batch).softmax(dim=-1)[0]
|
|
29
|
+
|
|
30
|
+
result = pf.from_scores(probabilities, labels=weights.meta["categories"])
|
|
31
|
+
|
|
32
|
+
print(len(result))
|
|
33
|
+
print(result.top1)
|
|
34
|
+
for answer in result.top_k(3):
|
|
35
|
+
print(answer.class_name, answer.confidence)
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
```text
|
|
39
|
+
1000
|
|
40
|
+
Classification(class_id=670, class_name='motor scooter', confidence=0.315)
|
|
41
|
+
motor scooter 0.315
|
|
42
|
+
golfcart 0.28
|
|
43
|
+
tricycle 0.073
|
|
44
|
+
```
|
|
45
|
+
|
|
46
|
+
The model knows 1,000 classes, so the result has 1,000 rows. `top1` is the best answer and
|
|
47
|
+
`top_k(3)` the three best.
|
|
48
|
+
|
|
49
|
+
## Detections or classifications
|
|
50
|
+
|
|
51
|
+
PixelFlow keeps the two apart because they mean different things:
|
|
52
|
+
|
|
53
|
+
- Three **detections** are three objects in the image, each with its own box.
|
|
54
|
+
- Three **classifications** are three competing answers about one image. None has a box,
|
|
55
|
+
because the model did not say where anything is.
|
|
56
|
+
|
|
57
|
+
So a `Classifications` object has no boxes, and the functions that need boxes (the tracker,
|
|
58
|
+
zones, box drawing) do not take it. To draw the answers on the image, use
|
|
59
|
+
[`pf.annotate.classification`](/docs/visualization/classification.md).
|
|
60
|
+
|
|
61
|
+
## Rank or threshold
|
|
62
|
+
|
|
63
|
+
Which method to use depends on what the scores mean:
|
|
64
|
+
|
|
65
|
+
- **One right answer** (a softmax over all classes, as above): rank them. `result.top1` is
|
|
66
|
+
the best answer, `result.top_k(5)` the five best, sorted best first.
|
|
67
|
+
- **Any number of right answers** (independent scores, such as a multi-label model or CLIP
|
|
68
|
+
similarities against several prompts): keep every class above a score with
|
|
69
|
+
`result.filter_by_confidence(0.3)`. The number of answers can change from image to image,
|
|
70
|
+
which a fixed `top_k` cannot follow.
|
|
71
|
+
|
|
72
|
+
`top_k`, `filter_by_confidence` and slicing (`result[:3]`) all return a new
|
|
73
|
+
`Classifications`. `len()`, `for answer in result:` and `result[0]` work as on
|
|
74
|
+
[Detections](/docs/core-concepts/detections.md).
|
|
75
|
+
|
|
76
|
+
## Scores are kept as given
|
|
77
|
+
|
|
78
|
+
PixelFlow does not apply softmax or change the scores in any way: `confidence` is exactly the
|
|
79
|
+
number you passed. A softmax probability, a sigmoid score and a CLIP similarity are all valid,
|
|
80
|
+
and only you know which one your model gives. If your model returns raw logits, convert them
|
|
81
|
+
first: `logits.softmax(dim=-1)` when there is one right answer, `logits.sigmoid()` when there
|
|
82
|
+
can be several.
|
|
83
|
+
|
|
84
|
+
`from_scores` takes a PyTorch tensor, a NumPy array or a list. `labels` gives the class names,
|
|
85
|
+
as a list (position = class number), a dictionary (`{0: "cat", 1: "dog"}`) or a list of
|
|
86
|
+
`{"id": ..., "name": ...}` dictionaries. A class with no name in `labels` keeps its number and
|
|
87
|
+
has `class_name` set to `None`.
|
|
88
|
+
|
|
89
|
+
## Other ways to get classifications
|
|
90
|
+
|
|
91
|
+
- **Ultralytics classification models** (`yolo11n-cls.pt` and other `-cls` checkpoints):
|
|
92
|
+
`pf.from_ultralytics_classification(model.predict("street.jpg"))`. Pass `top_k=5` to keep
|
|
93
|
+
only the five best on every video frame, instead of 1,000 rows each time.
|
|
94
|
+
- **Your own values:**
|
|
95
|
+
|
|
96
|
+
```python
|
|
97
|
+
import pixelflow as pf
|
|
98
|
+
|
|
99
|
+
result = pf.from_scores([0.62, 0.31, 0.07], labels=["street", "market", "parking lot"])
|
|
100
|
+
print(result.top1.class_name)
|
|
101
|
+
```
|
|
102
|
+
|
|
103
|
+
```text
|
|
104
|
+
street
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
To store or send the result, `to_dict()` and `to_json()` work as for detections. See
|
|
108
|
+
[Saving results](/docs/core-concepts/saving-results.md).
|
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
# Converters: read results from any model
|
|
2
|
+
|
|
3
|
+
<!--
|
|
4
|
+
summary: Turn the output of Mayaku, Ultralytics, RF-DETR, SAM, Florence-2, EasyOCR, Detectron2 or your own model into PixelFlow Detections.
|
|
5
|
+
label: Converters
|
|
6
|
+
keywords: pixelflow, converters, mayaku, ultralytics, rf-detr, segment anything
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
A converter takes a model's output, exactly as the model returns it, and gives back a
|
|
10
|
+
[`Detections`](/docs/core-concepts/detections.md) object. It is the only line that depends on
|
|
11
|
+
the model: everything after it works the same whichever converter you used.
|
|
12
|
+
|
|
13
|
+
| Model or library | Converter |
|
|
14
|
+
|------------------|-----------|
|
|
15
|
+
| [Mayaku](https://emrah.fr/mayaku/) | `pf.from_mayaku` |
|
|
16
|
+
| Ultralytics (YOLO detection, segmentation, pose) | `pf.from_ultralytics` |
|
|
17
|
+
| RF-DETR | `pf.from_rfdetr` |
|
|
18
|
+
| supervision (`sv.Detections`) | `pf.from_supervision` |
|
|
19
|
+
| Segment Anything (SAM, SAM 2) | `pf.from_sam` |
|
|
20
|
+
| EfficientTAM | `pf.from_efficienttam` |
|
|
21
|
+
| Florence-2 | `pf.from_florence2` |
|
|
22
|
+
| EasyOCR | `pf.from_easyocr` |
|
|
23
|
+
| Detectron2 | `pf.from_detectron2` |
|
|
24
|
+
| Falcon Perception | `pf.from_falcon_perception` |
|
|
25
|
+
| Any model that returns arrays | `pf.from_arrays` |
|
|
26
|
+
|
|
27
|
+
For classification models, see [Classifications](/docs/core-concepts/classifications.md).
|
|
28
|
+
|
|
29
|
+
## Class names: the `labels` argument
|
|
30
|
+
|
|
31
|
+
Models return class numbers. To get class names in `class_name`, most converters take
|
|
32
|
+
`labels`, in one of three forms:
|
|
33
|
+
|
|
34
|
+
- a list, where the position is the class number: `["person", "bicycle", "car"]`
|
|
35
|
+
- a dictionary from class number to name: `{1: "person", 2: "bicycle"}`
|
|
36
|
+
- a list of dictionaries, which can also name keypoints:
|
|
37
|
+
`[{"id": 0, "name": "person", "keypoints": ["nose", "left_eye", ...]}]`
|
|
38
|
+
|
|
39
|
+
Take the names from the model itself, as the examples below do, rather than typing a list: a
|
|
40
|
+
list that does not match the model's numbering gives every detection the wrong name, with no
|
|
41
|
+
error. A class number that has no name in `labels` is kept, and `class_name` stays `None`.
|
|
42
|
+
|
|
43
|
+
## Mayaku
|
|
44
|
+
|
|
45
|
+
```python
|
|
46
|
+
import pixelflow as pf
|
|
47
|
+
from mayaku import from_pretrained
|
|
48
|
+
|
|
49
|
+
predictor = from_pretrained("mayaku-n-det")
|
|
50
|
+
result = predictor("street.jpg")
|
|
51
|
+
detections = pf.from_mayaku(result, labels=predictor.class_names)
|
|
52
|
+
```
|
|
53
|
+
|
|
54
|
+
Fills boxes, scores and class numbers, and masks with a segmentation model such as
|
|
55
|
+
`mayaku-n-seg`. `predictor.class_names` holds the names the model was trained with. For several
|
|
56
|
+
images, `predictor.batch` returns one result per image: convert each one separately.
|
|
57
|
+
|
|
58
|
+
## Ultralytics
|
|
59
|
+
|
|
60
|
+
```python
|
|
61
|
+
import pixelflow as pf
|
|
62
|
+
from ultralytics import YOLO
|
|
63
|
+
|
|
64
|
+
model = YOLO("yolo11n-seg.pt")
|
|
65
|
+
results = model.predict("street.jpg", verbose=False)
|
|
66
|
+
detections = pf.from_ultralytics(results)
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
Works with detection, segmentation and pose models. Ultralytics stores class names in the model,
|
|
70
|
+
so `labels` is only needed to rename them. A segmentation model fills both `masks` and
|
|
71
|
+
`segments`.
|
|
72
|
+
|
|
73
|
+
## RF-DETR and supervision
|
|
74
|
+
|
|
75
|
+
```python
|
|
76
|
+
import pixelflow as pf
|
|
77
|
+
from rfdetr import RFDETRNano
|
|
78
|
+
from rfdetr.assets.coco_classes import COCO_CLASSES
|
|
79
|
+
|
|
80
|
+
model = RFDETRNano()
|
|
81
|
+
predictions = model.predict("street.jpg", threshold=0.5)
|
|
82
|
+
detections = pf.from_rfdetr(predictions, labels=COCO_CLASSES)
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
> **Note:** RF-DETR numbers the COCO classes from 1 (`1` is person), but `model.class_names`
|
|
86
|
+
> is a list that starts at 0. Passing that list shifts every name by one. `COCO_CLASSES` is a
|
|
87
|
+
> dictionary with RF-DETR's own numbers, so the names come out right. For a model you trained
|
|
88
|
+
> on your own classes, check the numbering the same way.
|
|
89
|
+
|
|
90
|
+
RF-DETR returns an `sv.Detections` from the supervision library, so `pf.from_rfdetr` is the
|
|
91
|
+
same function as `pf.from_supervision`: use either name for any `sv.Detections`.
|
|
92
|
+
|
|
93
|
+
## Segment Anything
|
|
94
|
+
|
|
95
|
+
SAM and SAM 2 return candidate masks for the point or box you give them, each with a quality
|
|
96
|
+
score. `pf.from_sam` turns each candidate into a detection, with the box taken from the mask:
|
|
97
|
+
|
|
98
|
+
```python
|
|
99
|
+
import numpy as np
|
|
100
|
+
import pixelflow as pf
|
|
101
|
+
from sam2.sam2_image_predictor import SAM2ImagePredictor
|
|
102
|
+
|
|
103
|
+
predictor = SAM2ImagePredictor.from_pretrained("facebook/sam2.1-hiera-tiny", device="cpu")
|
|
104
|
+
image = pf.read_image("street.jpg")
|
|
105
|
+
predictor.set_image(image)
|
|
106
|
+
|
|
107
|
+
masks, scores, _ = predictor.predict(
|
|
108
|
+
point_coords=np.array([[170, 900]]), point_labels=np.array([1])
|
|
109
|
+
)
|
|
110
|
+
detections = pf.from_sam(masks, scores)
|
|
111
|
+
|
|
112
|
+
for detection in detections:
|
|
113
|
+
print(detection.confidence, detection.bbox)
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
```text
|
|
117
|
+
0.446 [8.0, 599.0, 365.0, 1279.0]
|
|
118
|
+
0.459 [55.0, 762.0, 319.0, 1108.0]
|
|
119
|
+
0.883 [29.0, 718.0, 322.0, 1107.0]
|
|
120
|
+
```
|
|
121
|
+
|
|
122
|
+
Use `device="cuda"` on an NVIDIA GPU. SAM does not name what it segments, so `class_id` and
|
|
123
|
+
`class_name` are `None`. EfficientTAM returns the same format: use `pf.from_efficienttam`.
|
|
124
|
+
|
|
125
|
+
## Florence-2
|
|
126
|
+
|
|
127
|
+
Florence-2 does several tasks, chosen by a prompt such as `<OD>` (object detection). Pass the
|
|
128
|
+
same prompt to the converter:
|
|
129
|
+
|
|
130
|
+
```python
|
|
131
|
+
import pixelflow as pf
|
|
132
|
+
from PIL import Image
|
|
133
|
+
from transformers import AutoProcessor, Florence2ForConditionalGeneration
|
|
134
|
+
|
|
135
|
+
model_id = "florence-community/Florence-2-base"
|
|
136
|
+
model = Florence2ForConditionalGeneration.from_pretrained(model_id)
|
|
137
|
+
processor = AutoProcessor.from_pretrained(model_id)
|
|
138
|
+
|
|
139
|
+
image = Image.open("street.jpg")
|
|
140
|
+
task = "<OD>"
|
|
141
|
+
inputs = processor(text=task, images=image, return_tensors="pt")
|
|
142
|
+
generated = model.generate(**inputs, max_new_tokens=1024)
|
|
143
|
+
text = processor.batch_decode(generated, skip_special_tokens=False)[0]
|
|
144
|
+
parsed = processor.post_process_generation(text, task=task, image_size=image.size)
|
|
145
|
+
|
|
146
|
+
detections = pf.from_florence2(parsed, task_prompt=task, image_size=image.size)
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
Object detection tasks fill `class_name`. Captioning, grounding and OCR tasks with regions put
|
|
150
|
+
the text the model produced in `text`. Segmentation tasks fill `segments`. Text-only tasks, which
|
|
151
|
+
return no regions, raise a `ValueError`.
|
|
152
|
+
|
|
153
|
+
## EasyOCR
|
|
154
|
+
|
|
155
|
+
```python
|
|
156
|
+
import easyocr
|
|
157
|
+
import pixelflow as pf
|
|
158
|
+
|
|
159
|
+
reader = easyocr.Reader(["en"])
|
|
160
|
+
detections = pf.from_easyocr(reader.readtext("street.jpg"))
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
One detection per text region: the text read in `text`, the four corners of the region in
|
|
164
|
+
`segments`, and the box around them in `bbox`. Call `readtext` with its default `detail=1`:
|
|
165
|
+
with `detail=0` EasyOCR returns only the strings, which have no position to convert.
|
|
166
|
+
|
|
167
|
+
## Detectron2
|
|
168
|
+
|
|
169
|
+
```python
|
|
170
|
+
import pixelflow as pf
|
|
171
|
+
from detectron2.data import MetadataCatalog
|
|
172
|
+
|
|
173
|
+
outputs = predictor(image) # a detectron2 DefaultPredictor, BGR image
|
|
174
|
+
class_names = MetadataCatalog.get(cfg.DATASETS.TRAIN[0]).thing_classes
|
|
175
|
+
detections = pf.from_detectron2(outputs, labels=class_names)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
Fills boxes, scores, class numbers, masks and keypoints.
|
|
179
|
+
|
|
180
|
+
## Falcon Perception
|
|
181
|
+
|
|
182
|
+
Falcon Perception finds what you name in a query, so pass the query as `label` to use it as the
|
|
183
|
+
class name, and the image size, because the model returns coordinates relative to the image:
|
|
184
|
+
|
|
185
|
+
```python
|
|
186
|
+
detections = pf.from_falcon_perception(
|
|
187
|
+
output, image_size=(image.width, image.height), label="cat"
|
|
188
|
+
)
|
|
189
|
+
```
|
|
190
|
+
|
|
191
|
+
`output` is what the local engine's `generate` returns, or the JSON from the Falcon Perception
|
|
192
|
+
server.
|
|
193
|
+
|
|
194
|
+
## Any other model: `from_arrays`
|
|
195
|
+
|
|
196
|
+
When your model returns NumPy arrays or PyTorch tensors instead of a framework object, pass them
|
|
197
|
+
to `pf.from_arrays`:
|
|
198
|
+
|
|
199
|
+
```python
|
|
200
|
+
import numpy as np
|
|
201
|
+
import pixelflow as pf
|
|
202
|
+
|
|
203
|
+
boxes = np.array([[120, 80, 260, 400], [300, 120, 520, 330]])
|
|
204
|
+
scores = np.array([0.91, 0.78])
|
|
205
|
+
class_ids = np.array([0, 2])
|
|
206
|
+
|
|
207
|
+
detections = pf.from_arrays(boxes, scores, class_ids, labels=["person", "bicycle", "car"])
|
|
208
|
+
|
|
209
|
+
for detection in detections:
|
|
210
|
+
print(detection.class_name, detection.confidence, detection.bbox)
|
|
211
|
+
```
|
|
212
|
+
|
|
213
|
+
```text
|
|
214
|
+
person 0.91 [120.0, 80.0, 260.0, 400.0]
|
|
215
|
+
car 0.78 [300.0, 120.0, 520.0, 330.0]
|
|
216
|
+
```
|
|
217
|
+
|
|
218
|
+
| Argument | Shape | |
|
|
219
|
+
|----------|-------|---|
|
|
220
|
+
| `boxes` | `(N, 4)` | `[x1, y1, x2, y2]` in pixels |
|
|
221
|
+
| `scores` | `(N,)` | confidence |
|
|
222
|
+
| `class_ids` | `(N,)` | optional: leave it out for a model that finds things without naming them |
|
|
223
|
+
| `masks` | `(N, H, W)` | optional, turned into boolean masks |
|
|
224
|
+
| `keypoints` | `(N, K, 3)` | optional, `(x, y, score)` per keypoint |
|
|
225
|
+
| `segments` | `N` polygons | optional |
|
|
226
|
+
| `texts` | `(N,)` | optional, text per detection |
|
|
227
|
+
| `labels` | | optional class names, as above |
|
|
228
|
+
|
|
229
|
+
Tensors on a GPU are moved to the CPU for you.
|
|
@@ -0,0 +1,145 @@
|
|
|
1
|
+
# Detections: one format for every model's results
|
|
2
|
+
|
|
3
|
+
<!--
|
|
4
|
+
summary: What a Detections object holds, how to read boxes, classes, masks and keypoints from it, and how to build one yourself.
|
|
5
|
+
label: Detections
|
|
6
|
+
keywords: pixelflow, detections, bounding box, segmentation mask, keypoints
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
A detection model tells you what it found and where. Each framework returns that in its own
|
|
10
|
+
objects, with its own field names and box formats. PixelFlow converts every result into a
|
|
11
|
+
`Detections` object: a list of `Detection` objects, one per object found, each with the same
|
|
12
|
+
fields whatever model produced it.
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
import pixelflow as pf
|
|
16
|
+
from mayaku import from_pretrained
|
|
17
|
+
|
|
18
|
+
predictor = from_pretrained("mayaku-n-det")
|
|
19
|
+
result = predictor("street.jpg")
|
|
20
|
+
detections = pf.from_mayaku(result, labels=predictor.class_names)
|
|
21
|
+
detections = detections.filter_by_confidence(0.5)
|
|
22
|
+
|
|
23
|
+
print(len(detections))
|
|
24
|
+
for detection in detections:
|
|
25
|
+
print(detection.class_name, detection.confidence, detection.bbox)
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```text
|
|
29
|
+
11
|
|
30
|
+
Person 0.835 [4.56, 588.91, 333.44, 1273.22]
|
|
31
|
+
Person 0.721 [275.62, 535.19, 469.88, 795.32]
|
|
32
|
+
Person 0.656 [624.09, 561.33, 743.54, 906.53]
|
|
33
|
+
Person 0.649 [944.12, 558.44, 1151.99, 874.55]
|
|
34
|
+
Umbrella 0.619 [716.73, 427.97, 957.37, 627.64]
|
|
35
|
+
...
|
|
36
|
+
```
|
|
37
|
+
|
|
38
|
+
Every function that comes after the model (filters, the tracker, zones, drawing) takes this
|
|
39
|
+
object, so the rest of your code does not change when you change the model.
|
|
40
|
+
|
|
41
|
+
## What a detection holds
|
|
42
|
+
|
|
43
|
+
A converter fills the fields the model provides and leaves the others empty (`None`, or an
|
|
44
|
+
empty list). The last fields are filled later, by the PixelFlow step named in the table.
|
|
45
|
+
|
|
46
|
+
| Field | What it holds | Filled by |
|
|
47
|
+
|-------|---------------|-----------|
|
|
48
|
+
| `bbox` | Box as `[x1, y1, x2, y2]`: the top-left and bottom-right corners, in pixels from the image's top-left corner, rounded to 2 decimals | the converter |
|
|
49
|
+
| `confidence` | The model's score, from 0 to 1, rounded to 3 decimals | the converter |
|
|
50
|
+
| `class_id` | The class number from the model | the converter |
|
|
51
|
+
| `class_name` | The class name, such as `"Person"`, when the model or the `labels` you pass gives one | the converter |
|
|
52
|
+
| `masks` | Segmentation masks, from segmentation models | the converter |
|
|
53
|
+
| `segments` | The object's outline as one polygon, `[[x, y], ...]` | the converter, or `masks_to_segments()` |
|
|
54
|
+
| `keypoints` | Landmarks as `KeyPoint` objects (`x`, `y`, `id`, `name`, `confidence`), from pose models | the converter |
|
|
55
|
+
| `text` | Text the model produced for this object: what an OCR model read, or a caption | the converter |
|
|
56
|
+
| `tracker_id` | The same number for the same object in every frame of a video | [the tracker](/docs/tracking/tracker.md) |
|
|
57
|
+
| `zones`, `zone_names` | The zones the object is in | [`Zones`](/docs/tracking/zones.md) |
|
|
58
|
+
| `line_crossings` | The lines the object crossed | [`Crossings`](/docs/tracking/crossings.md) |
|
|
59
|
+
| `first_seen_time`, `total_time` | When the object first appeared, and for how many seconds since | [`TimeTracker`](/docs/tracking/time-in-view.md) |
|
|
60
|
+
| `metadata` | Anything else, as a dictionary | you |
|
|
61
|
+
|
|
62
|
+
`class_name` and `text` are different things: `class_name` is one of the classes the model was
|
|
63
|
+
trained on, and `text` is free text the model produced for this one object. A detection can have
|
|
64
|
+
both.
|
|
65
|
+
|
|
66
|
+
## Masks and outlines
|
|
67
|
+
|
|
68
|
+
A segmentation model fills `masks` with a list holding one mask per object: a boolean array the
|
|
69
|
+
size of the image, `True` where the object is.
|
|
70
|
+
|
|
71
|
+
```python
|
|
72
|
+
import pixelflow as pf
|
|
73
|
+
from mayaku import from_pretrained
|
|
74
|
+
|
|
75
|
+
predictor = from_pretrained("mayaku-n-seg")
|
|
76
|
+
result = predictor("street.jpg")
|
|
77
|
+
detections = pf.from_mayaku(result, labels=predictor.class_names)
|
|
78
|
+
detections = detections.filter_by_confidence(0.5)
|
|
79
|
+
|
|
80
|
+
person = detections[0]
|
|
81
|
+
mask = person.masks[0]
|
|
82
|
+
print(mask.shape, mask.dtype, mask.sum())
|
|
83
|
+
```
|
|
84
|
+
|
|
85
|
+
```text
|
|
86
|
+
(1280, 1920) bool 117697
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
A full-size mask per object takes memory and cannot be written to JSON as it is. When you need
|
|
90
|
+
the outline instead, for example to store it or send it to a web page, `masks_to_segments()`
|
|
91
|
+
traces each mask into a polygon and stores it in `segments`:
|
|
92
|
+
|
|
93
|
+
```python
|
|
94
|
+
detections.masks_to_segments()
|
|
95
|
+
print(len(person.segments), person.segments[:3])
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
```text
|
|
99
|
+
105 [[15.0, 685.0], [15.0, 700.0], [19.0, 718.0]]
|
|
100
|
+
```
|
|
101
|
+
|
|
102
|
+
Pass `keep_masks=False` to drop the masks once they are traced, and `tolerance` (in pixels,
|
|
103
|
+
default 1.5) to trade outline detail for fewer points.
|
|
104
|
+
|
|
105
|
+
## Working with a Detections object
|
|
106
|
+
|
|
107
|
+
- `len(detections)` is the number of objects. An empty `Detections` is false, so
|
|
108
|
+
`if not detections:` checks for "nothing found".
|
|
109
|
+
- `for detection in detections:` goes through them, and `detections[0]` gets one by position.
|
|
110
|
+
- Filters return a new `Detections` and leave the original as it is, so they chain:
|
|
111
|
+
`detections.filter_by_confidence(0.5).filter_by_class_id(0)`. See
|
|
112
|
+
[Filters](/docs/core-concepts/filters.md).
|
|
113
|
+
- `detections.copy()` returns an independent copy, for when you are about to change detections
|
|
114
|
+
in place and still need the originals.
|
|
115
|
+
- `to_dict()` and `to_json()` turn the detections into plain data. See
|
|
116
|
+
[Saving results](/docs/core-concepts/saving-results.md).
|
|
117
|
+
|
|
118
|
+
## Make detections yourself
|
|
119
|
+
|
|
120
|
+
When your model has no converter, build the detections from your own values:
|
|
121
|
+
|
|
122
|
+
```python
|
|
123
|
+
import pixelflow as pf
|
|
124
|
+
|
|
125
|
+
detections = pf.Detections()
|
|
126
|
+
detections.add_detection(
|
|
127
|
+
pf.Detection(bbox=[120, 80, 260, 400], class_id=0, class_name="person", confidence=0.91)
|
|
128
|
+
)
|
|
129
|
+
detections.add_detection(
|
|
130
|
+
pf.Detection(bbox=[300, 50, 300, 90], class_name="person")
|
|
131
|
+
)
|
|
132
|
+
print(len(detections))
|
|
133
|
+
```
|
|
134
|
+
|
|
135
|
+
```text
|
|
136
|
+
1
|
|
137
|
+
```
|
|
138
|
+
|
|
139
|
+
The second detection was not added: its box has no width. `add_detection` leaves out a
|
|
140
|
+
detection whose box is missing, invalid or encloses no pixels, and one whose confidence is
|
|
141
|
+
NaN. So every detection in a `Detections` has a usable box, and the code after it does not need
|
|
142
|
+
to check.
|
|
143
|
+
|
|
144
|
+
When your model returns NumPy arrays of boxes, scores and class numbers, `pf.from_arrays` builds
|
|
145
|
+
the whole `Detections` in one call. See [Converters](/docs/core-concepts/converters.md).
|
|
@@ -0,0 +1,132 @@
|
|
|
1
|
+
# Filters: keep only the detections you need
|
|
2
|
+
|
|
3
|
+
<!--
|
|
4
|
+
summary: Keep detections by confidence, class, size, shape, position or tracking time, and remove duplicates, by chaining filter methods.
|
|
5
|
+
label: Filters
|
|
6
|
+
keywords: pixelflow, filter detections, confidence threshold, class filter, remove duplicates
|
|
7
|
+
-->
|
|
8
|
+
|
|
9
|
+
A model returns everything it might have seen, including guesses with a low score and classes
|
|
10
|
+
you do not care about. Filters keep the detections you need. Each one is a method of
|
|
11
|
+
[`Detections`](/docs/core-concepts/detections.md) that returns a new `Detections`, so they
|
|
12
|
+
chain, and the original stays as it was.
|
|
13
|
+
|
|
14
|
+
```python
|
|
15
|
+
import pixelflow as pf
|
|
16
|
+
from mayaku import from_pretrained
|
|
17
|
+
|
|
18
|
+
predictor = from_pretrained("mayaku-n-det")
|
|
19
|
+
result = predictor("street.jpg")
|
|
20
|
+
detections = pf.from_mayaku(result, labels=predictor.class_names)
|
|
21
|
+
|
|
22
|
+
person = predictor.class_names.index("Person")
|
|
23
|
+
people = detections.filter_by_confidence(0.5).filter_by_class_id(person)
|
|
24
|
+
|
|
25
|
+
print(len(detections), len(people))
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
```text
|
|
29
|
+
279 8
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
The model returned 279 detections; 8 are people with a confidence of 0.5 or more.
|
|
33
|
+
|
|
34
|
+
## Confidence and class
|
|
35
|
+
|
|
36
|
+
| Filter | Keeps |
|
|
37
|
+
|--------|-------|
|
|
38
|
+
| `filter_by_confidence(threshold)` | detections with a confidence of `threshold` or more |
|
|
39
|
+
| `filter_by_class_id(class_ids)` | detections whose `class_id` is in `class_ids`: one id or a list |
|
|
40
|
+
| `remap_class_ids(from_ids, to_id)` | every detection, with the ids in `from_ids` changed to `to_id` |
|
|
41
|
+
|
|
42
|
+
`filter_by_class_id` compares class numbers, not names. Get the numbers from the model's class
|
|
43
|
+
names, as above, rather than writing them by hand: the same number means different classes in
|
|
44
|
+
different models.
|
|
45
|
+
|
|
46
|
+
`remap_class_ids` merges several classes into one, for example to count every two-wheeler
|
|
47
|
+
together:
|
|
48
|
+
|
|
49
|
+
```python
|
|
50
|
+
two_wheelers = [predictor.class_names.index(name) for name in ("Motorcycle", "Scooter", "Bicycle")]
|
|
51
|
+
vehicles = detections.filter_by_confidence(0.3).filter_by_class_id(two_wheelers)
|
|
52
|
+
vehicles = vehicles.remap_class_ids(two_wheelers, two_wheelers[0])
|
|
53
|
+
```
|
|
54
|
+
|
|
55
|
+
The remapped detections have `class_name` set to `None`, because the old name no longer
|
|
56
|
+
matches the new number.
|
|
57
|
+
|
|
58
|
+
## Size and shape
|
|
59
|
+
|
|
60
|
+
| Filter | Keeps detections whose box |
|
|
61
|
+
|--------|----------------------------|
|
|
62
|
+
| `filter_by_size(min_area=None, max_area=None)` | has an area, in pixels, within the range |
|
|
63
|
+
| `filter_by_dimensions(min_width=None, max_width=None, min_height=None, max_height=None)` | has a width and height, in pixels, within the ranges |
|
|
64
|
+
| `filter_by_aspect_ratio(min_ratio=None, max_ratio=None)` | has a width ÷ height within the range |
|
|
65
|
+
| `filter_by_relative_size(min_percent=None, max_percent=None, frame_width=None, frame_height=None)` | covers a share of the image within the range, from 0 to 1 |
|
|
66
|
+
|
|
67
|
+
Leave out a limit to keep everything on that side. All limits are inclusive.
|
|
68
|
+
|
|
69
|
+
Pixel sizes depend on the image size: in the same photo at half the width and height, every box
|
|
70
|
+
covers a quarter of the pixels. When your images come in different sizes,
|
|
71
|
+
`filter_by_relative_size` keeps the same objects in all of them:
|
|
72
|
+
|
|
73
|
+
```python
|
|
74
|
+
# people covering at least 2% of a 1920x1280 image
|
|
75
|
+
close = people.filter_by_relative_size(min_percent=0.02, frame_width=1920, frame_height=1280)
|
|
76
|
+
```
|
|
77
|
+
|
|
78
|
+
An aspect ratio below 1 means taller than wide: `max_ratio=0.8` keeps standing people and drops
|
|
79
|
+
boxes that are wider than they are tall.
|
|
80
|
+
|
|
81
|
+
## Position in the image
|
|
82
|
+
|
|
83
|
+
`filter_by_position` keeps detections by where the center of their box is:
|
|
84
|
+
|
|
85
|
+
```python
|
|
86
|
+
left = people.filter_by_position("left", margin_percent=0.25, frame_width=1920, frame_height=1280)
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
| `region` | Keeps detections whose center is |
|
|
90
|
+
|----------|----------------------------------|
|
|
91
|
+
| `"left"`, `"right"`, `"top"`, `"bottom"` | within `margin_percent` of that side (`0.25` = the outer quarter) |
|
|
92
|
+
| `"edge"` | within `margin_percent` of any side |
|
|
93
|
+
| `"corners"` | within `margin_percent` of two sides at once |
|
|
94
|
+
| `"center"` | further than `margin_percent` from every side |
|
|
95
|
+
|
|
96
|
+
`margin_percent` defaults to `0.1`. `frame_width` and `frame_height` are required.
|
|
97
|
+
|
|
98
|
+
For any other area, such as a doorway or a lane, draw it as a zone:
|
|
99
|
+
`filter_by_zones(zone_ids, exclude=False)` keeps the detections inside the zones you name, or
|
|
100
|
+
outside them with `exclude=True`. It reads what `Zones` recorded, so run the zones first. See
|
|
101
|
+
[Count objects in a zone](/docs/tracking/zones.md).
|
|
102
|
+
|
|
103
|
+
## Duplicates and overlaps
|
|
104
|
+
|
|
105
|
+
| Filter | Keeps |
|
|
106
|
+
|--------|-------|
|
|
107
|
+
| `remove_duplicates(iou_threshold=0.8, keep="first")` | one detection from each group of boxes that overlap by `iou_threshold` or more |
|
|
108
|
+
| `filter_overlapping(min_overlap=0.5, target_class_ids=None)` | detections that overlap another detection by `min_overlap` or more |
|
|
109
|
+
|
|
110
|
+
Overlap is measured as IoU: the area two boxes share divided by the area they cover together,
|
|
111
|
+
from 0 (apart) to 1 (the same box).
|
|
112
|
+
|
|
113
|
+
Some models, or two models whose results you combine, give the same object twice.
|
|
114
|
+
`remove_duplicates` keeps one from each group: `keep="first"`, `"last"` or
|
|
115
|
+
`"highest_confidence"`.
|
|
116
|
+
|
|
117
|
+
`filter_overlapping` finds objects that appear together. With `target_class_ids`, it only
|
|
118
|
+
counts overlaps with those classes: for example, the detections that overlap a person.
|
|
119
|
+
|
|
120
|
+
## Tracked objects
|
|
121
|
+
|
|
122
|
+
After [the tracker](/docs/tracking/tracker.md) and [`TimeTracker`](/docs/tracking/time-in-view.md)
|
|
123
|
+
have run on a video, three more filters use what they recorded:
|
|
124
|
+
|
|
125
|
+
| Filter | Keeps |
|
|
126
|
+
|--------|-------|
|
|
127
|
+
| `filter_tracked_objects(require_tracker_id=True)` | detections the tracker gave an id, or with `False`, the ones it did not |
|
|
128
|
+
| `filter_by_tracking_duration(min_seconds=None, max_seconds=None)` | objects seen for a time within the range |
|
|
129
|
+
| `filter_by_first_seen_time(start_time=None, end_time=None)` | objects first seen within the time range |
|
|
130
|
+
|
|
131
|
+
`filter_by_tracking_duration(min_seconds=2)` is a simple way to ignore objects that appear for a
|
|
132
|
+
moment: a false detection rarely lasts two seconds.
|