annotation

Colors

This module defines the Color class, an abstraction over RGBA colors, as well ass ColorMap, that lets you easily access frequently used colors.

Annotation types

class backpack.annotation.LabelAnnotation(point, text, color=None, horizontal_anchor=HorizontalAnchor.LEFT, vertical_anchor=VerticalAnchor.BOTTOM, size=1.0)

Bases: Annotation

A label annotation to be rendered in an AnnotationDriver context.

The point refers to the position of the anchor point.

Parameters:
  • point (Point) – The origin of the label

  • text (str) – The text to be rendered

  • color (Color) – The color of the text

  • horizontal_anchor (HorizontalAnchor) –

  • vertical_anchor (VerticalAnchor) –

  • size (float) –

class HorizontalAnchor(value)

Bases: Enum

Horizontal anchor point location.

LEFT = (1,)

Left anchor point

CENTER = (2,)

Center anchor point

RIGHT = 3

Right anchor point

class VerticalAnchor(value)

Bases: Enum

Vertical anchor point location.

TOP = 1

Top anchor point

CENTER = 2

Center anchor point

BASELINE = 3

Text baseline anchor point

BOTTOM = 4

Bottom anchor point

point: Point

The origin of the label.

text: str

The text to be rendered.

color: Color = None

The color of the text. If None, the default drawing color will be used.

horizontal_anchor: HorizontalAnchor = (1,)

Horizontal anchor point location

vertical_anchor: VerticalAnchor = 4

Vertical anchor point location

size: float = 1.0

The relative size of the text

class backpack.annotation.RectAnnotation(rect, color=None)

Bases: Annotation

A rectangle annotation to be rendered in an AnnotationDriver context.

Parameters:
  • rect (Rectangle) – The rectangle to be rendered in the AnnotationDriver context.

  • color (Color) – The line color of the rectangle.

rect: Rectangle

The rectangle to be rendered in the AnnotationDriver context

color: Color = None

The line color of the rectangle. If None, the default drawing color will be used.

class backpack.annotation.LineAnnotation(line, color=None, thickness=1)

Bases: Annotation

A line annotation to be rendered in an AnnotationDriver context.

Parameters:
  • line (Line) – The line segment to be drawn

  • color (Color) – The color of the line

  • thickness (int) –

line: Line

The line segment to be drawn

color: Color = None

The color of the line. If None, the default drawing color will be used.

thickness: int = 1

The thickness of the line.

class backpack.annotation.MarkerAnnotation(point, style=Style.CROSS, color=None)

Bases: Annotation

A marker annotation to be rendered in an AnnotationDriver context.

Parameters:
  • point (Point) – The coordinates of the maker

  • style (MarkerAnnotation.Style) – The style of the marker

  • color (Color) – The color of the maker

class Style(value)

Bases: Enum

Possible set of marker types.

CROSS = 1

A crosshair marker shape.

TILTED_CROSS = 2

A 45 degree tilted crosshair marker shape.

STAR = 3

A star marker shape, combination of cross and tilted cross.

DIAMOND = 4

A diamond marker shape.

SQUARE = 5

A square marker shape.

TRIANGLE_UP = 6

An upwards pointing triangle marker shape.

TRIANGLE_DOWN = 7

A downwards pointing triangle marker shape.

point: Point

The coordinates of the maker.

style: Style = 1

The style of the marker.

color: Color = None

The color of the maker.

class backpack.annotation.TimestampAnnotation(timestamp=None, point=Point(x=0.02, y=0.04))

Bases: LabelAnnotation

A timestamp annotation to be rendered in an AnnotationDriver context.

Parameters:
  • timestamp (Optional[datetime]) – The timestamp to be rendered. If not specified, the current local time will be used.

  • point (Point) – The origin of the timestamp. If not specified, the timestamp will be places on the top-left corner of the image.

point: Point

The origin of the label.

text: str

The text to be rendered.

Annotation driver API

AnnotationDriverBase specifies the unified API to draw annotation on different backends.

class backpack.annotation.driver.AnnotationDriverBase(parent_logger=None)

Bases: ABC

Base class for annotating drawing drivers.

Annotation drivers provide an unified API to draw annotations on images of different backends. All annotation driver should derive from AnnotationDriverBase.

Parameters:

parent_logger (Optional[Logger]) – If you want to connect the logger of the annotation driver to a parent, specify it here.

render(annotations, context)

Renders a collection of annotations on a context.

Parameters:
  • annotations (Iterable[Annotation]) – An iterable collection of annotation type defined in this module.

  • context (Any) – The context of the backend. Type is implementation-specific.

Returns:

The context.

Return type:

Any

abstract add_rect(rect, context)

Renders a rectangle on the frame.

Parameters:
  • rect (RectAnnotation) – A rectangle annotation.

  • context (Any) – A backend-specific context object that was passed to the render() method.

Return type:

None

abstract add_label(label, context)

Renders a label on the frame.

Parameters:
  • label (LabelAnnotation) – A label annotation.

  • context (Any) – A backend-specific context object that was passed to the render() method.

Return type:

None

abstract add_marker(marker, context)

Renders a marker on the frame.

Parameters:
  • marker (MarkerAnnotation) – A marker annotation.

  • context (Any) – A backend-specific context object that was passed to the render() method.

Return type:

None

abstract add_line(label, context)

Renders a line on the frame.

Parameters:
  • line – A line annotation.

  • context (Any) – A backend-specific context object that was passed to the render() method.

  • label (LineAnnotation) –

Return type:

None

abstract add_polyline(polyline, context)

Renders a polyline.

Parameters:
  • polyline (PolyLineAnnotation) – A polyline annotation.

  • context (Any) – A backend-specific context object that was passed to the render() method.

Return type:

None

add_bounding_box(bounding_box, context)

Renders a bounding box.

Parameters:
  • bounding_box (BoundingBoxAnnotation) – A bounding box annotation.

  • context (Any) – A backend-specific context object that was passed to the render() method.

Return type:

None

Annotation driver implementations

Currently, annotations can be drawn on panoramasdk.media and OpenCV images.

class backpack.annotation.panorama.PanoramaMediaAnnotationDriver(parent_logger=None)

Bases: AnnotationDriverBase

Annotation driver implementation for Panorama media type images.

You should pass an panoramasdk.media instance as the context argument of the render() method.

PanoramaMediaAnnotationDriver currently does not support colors.

Parameters:

parent_logger (Optional[Logger]) –

add_rect(rect_anno, context)

Renders a rectangle on the frame.

Parameters:
  • rect – A rectangle annotation.

  • context (panoramasdk.media) – A backend-specific context object that was passed to the render() method.

  • rect_anno (RectAnnotation) –

Return type:

None

add_label(label, context)

Renders a label on the frame.

Parameters:
  • label (LabelAnnotation) – A label annotation.

  • context (panoramasdk.media) – A backend-specific context object that was passed to the render() method.

Return type:

None

add_marker(marker, context)

Renders a marker on the frame.

Parameters:
  • marker (MarkerAnnotation) – A marker annotation.

  • context (panoramasdk.media) – A backend-specific context object that was passed to the render() method.

Return type:

None

add_line(label, context)

Renders a line on the frame.

Parameters:
  • line – A line annotation.

  • context (panoramasdk.media) – A backend-specific context object that was passed to the render() method.

  • label (LineAnnotation) –

Return type:

None

add_polyline(polyline, context)

Renders a polyline.

Parameters:
  • polyline (PolyLineAnnotation) – A polyline annotation.

  • context (panoramasdk.media) – A backend-specific context object that was passed to the render() method.

Return type:

None

class backpack.annotation.opencv.OpenCVImageAnnotationDriver(parent_logger=None)

Bases: AnnotationDriverBase

Annotation driver implementation for OpenCV images.

You should pass an numpy.ndarray instance as the context argument of the render() method.

Parameters:

parent_logger (Optional[Logger]) –

IMG_HEIGHT_FOR_UNIT_FONT_SCALE = 400

The height of the image where 1.0 font_scale will be used.

draw_transparent(alpha, context, drawer)

Semi-transparent drawing.

Parameters:
  • alpha (float) – The transparency factor. Set 0 for opaque drawing, 1 for complete transparency.

  • context (ndarray) – The drawing context.

  • drawer (Callable[[ndarray], None]) – A callable that will draw an opaque drawing on the passed-in context. This method will make this drawing semi-transparent and render it on the context.

Return type:

None

static scale(point, context)

Converts and scales a point instance to an image context

Parameters:
Return type:

Tuple[float, float]

add_rect(anno, context)

Renders a rectangle on the frame.

Parameters:
  • rect – A rectangle annotation.

  • context (ndarray) – A backend-specific context object that was passed to the render() method.

  • anno (RectAnnotation) –

Return type:

None

add_label(anno, context)

Renders a label on the frame.

Parameters:
  • label – A label annotation.

  • context (ndarray) – A backend-specific context object that was passed to the render() method.

  • anno (LabelAnnotation) –

Return type:

None

add_marker(anno, context)

Renders a marker on the frame.

Parameters:
  • marker – A marker annotation.

  • context (ndarray) – A backend-specific context object that was passed to the render() method.

  • anno (MarkerAnnotation) –

Return type:

None

add_line(anno, context)

Renders a line on the frame.

Parameters:
  • line – A line annotation.

  • context (Any) – A backend-specific context object that was passed to the render() method.

  • anno (LineAnnotation) –

Return type:

None

add_polyline(anno, context)

Renders a polyline.

Parameters:
  • polyline – A polyline annotation.

  • context (Any) – A backend-specific context object that was passed to the render() method.

  • anno (PolyLineAnnotation) –

Return type:

None