Skip to content

Commit 432a75a

Browse files
Merge pull request #229 from tryolabs/drawing-custom-objects
Enable users to draw custom objects
2 parents d062300 + 9e7559e commit 432a75a

File tree

4 files changed

+38
-5
lines changed

4 files changed

+38
-5
lines changed

norfair/drawing/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,6 @@
33
from .color import Color, ColorType, Palette
44
from .draw_boxes import draw_boxes, draw_tracked_boxes
55
from .draw_points import draw_points, draw_tracked_objects
6+
from .drawer import Drawable
67
from .fixed_camera import FixedCamera
78
from .path import AbsolutePaths, Paths

norfair/drawing/draw_boxes.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -132,7 +132,10 @@ def draw_boxes(
132132
text_color = parse_color(text_color)
133133

134134
for obj in drawables:
135-
d = Drawable(obj)
135+
if not isinstance(obj, Drawable):
136+
d = Drawable(obj)
137+
else:
138+
d = obj
136139

137140
if color == "by_id":
138141
obj_color = Palette.choose_color(d.id)

norfair/drawing/draw_points.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,10 @@ def draw_points(
126126
radius = int(round(max(max(frame.shape) * 0.002, 1)))
127127

128128
for o in drawables:
129-
d = Drawable(o)
129+
if not isinstance(o, Drawable):
130+
d = Drawable(o)
131+
else:
132+
d = o
130133

131134
if hide_dead_points and not d.live_points.any():
132135
continue

norfair/drawing/drawer.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
from typing import Optional, Sequence, Tuple, Union
1+
from typing import Any, Optional, Sequence, Tuple, Union
22

33
import numpy as np
44

@@ -304,16 +304,36 @@ class Drawable:
304304
305305
Parameters
306306
----------
307-
obj : Union[Detection, TrackedObject]
307+
obj : Union[Detection, TrackedObject], optional
308308
A [Detection][norfair.tracker.Detection] or a [TrackedObject][norfair.tracker.TrackedObject]
309+
that will be used to initialized the drawable.
310+
If this parameter is passed, all other arguments are ignored
311+
points : np.ndarray, optional
312+
Points included in the drawable, shape is `(N_points, N_dimensions)`. Ignored if `obj` is passed
313+
id : Any, optional
314+
Id of this object. Ignored if `obj` is passed
315+
label : Any, optional
316+
Label specifying the class of the object. Ignored if `obj` is passed
317+
scores : np.ndarray, optional
318+
Confidence scores of each point, shape is `(N_points,)`. Ignored if `obj` is passed
319+
live_points : np.ndarray, optional
320+
Bolean array indicating which points are alive, shape is `(N_points,)`. Ignored if `obj` is passed
309321
310322
Raises
311323
------
312324
ValueError
313325
If obj is not an instance of the supported classes.
314326
"""
315327

316-
def __init__(self, obj: Union[Detection, TrackedObject]) -> None:
328+
def __init__(
329+
self,
330+
obj: Union[Detection, TrackedObject] = None,
331+
points: np.ndarray = None,
332+
id: Any = None,
333+
label: Any = None,
334+
scores: np.ndarray = None,
335+
live_points: np.ndarray = None,
336+
) -> None:
317337
if isinstance(obj, Detection):
318338
self.points = obj.points
319339
self.id = None
@@ -331,6 +351,12 @@ def __init__(self, obj: Union[Detection, TrackedObject]) -> None:
331351
# it could be the scores of the last detection or some kind of moving average
332352
self.scores = None
333353
self.live_points = obj.live_points
354+
elif obj is None:
355+
self.points = points
356+
self.id = id
357+
self.label = label
358+
self.scores = scores
359+
self.live_points = live_points
334360
else:
335361
raise ValueError(
336362
f"Extecting a Detection or a TrackedObject but received {type(obj)}"

0 commit comments

Comments
 (0)