otx.api.utils.shape_drawer#

This module implements helpers for drawing shapes.

Classes

DrawerEntity()

An interface to draw a shape of type T onto an image.

Helpers()

Contains variables which are used by all subclasses.

ShapeDrawer(show_count, is_one_label)

ShapeDrawer to draw any shape on a numpy array.

class otx.api.utils.shape_drawer.DrawerEntity[source]#

Bases: Generic[_Any]

An interface to draw a shape of type T onto an image.

abstract draw(image: ndarray, entity: _Any, labels: List[ScoredLabel]) ndarray[source]#

Draw an entity to a given frame.

Parameters:
  • image (np.ndarray) – The image to draw the entity on.

  • entity (T) – The entity to draw.

  • labels (List[ScoredLabel]) – Labels of the shapes to draw

Returns:

frame with shape drawn on it

Return type:

np.ndarray

class otx.api.utils.shape_drawer.Helpers[source]#

Bases: object

Contains variables which are used by all subclasses.

Contains functions which help with generating coordinates, text and text scale. These functions are use by the DrawerEntity Classes when drawing to an image.

static draw_flagpole(image: ndarray, flagpole_start_point: Coordinate, flagpole_end_point: Coordinate)[source]#

Draw a small flagpole between two points.

Parameters:
  • image – Image

  • flagpole_start_point – Start of the flagpole

  • flagpole_end_point – End of the flagpole

Returns:

Image

static draw_transparent_rectangle(img: ndarray, x1: int, y1: int, x2: int, y2: int, color: Tuple[int, int, int], alpha: float) ndarray[source]#

Draw a rectangle on an image.

Parameters:
  • img (np.ndarray) – Image

  • x1 (int) – Left side

  • y1 (int) – Top side

  • x2 (int) – Right side

  • y2 (int) – Bottom side

  • color (Tuple[int, int, int]) – Color

  • alpha (float) – Alpha value between 0 and 1

generate_draw_command_for_labels(labels: Sequence[LabelEntity | ScoredLabel], image: ndarray, show_labels: bool, show_confidence: bool) Tuple[Callable[[ndarray], ndarray], int, int][source]#

Generate draw function and content width and height for labels.

Generates a function which can be called to draw a list of labels onto an image relatively to the cursor position. The width and height of the content is also returned and can be determined to compute the best position for content before actually drawing it.

Parameters:
  • labels (Sequence[Union[LabelEntity, ScoredLabel]]) – List of labels

  • image (np.ndarray) – Image (used to compute font size)

  • show_labels (bool) – Whether to show the label name

  • show_confidence (bool) – Whether to show the confidence probability

Returns:

A tuple containing the drawing function, the content width, and the content height

generate_draw_command_for_text(text: str, text_scale: float, thickness: int, color: Tuple[int, int, int]) Tuple[Callable[[ndarray], ndarray], int, int][source]#

Generate function to draw text on image relative to cursor position.

Generate a function which can be called to draw the given text onto an image relatively to the cursor position.

The width and height of the content is also returned and can be determined to compute the best position for content before actually drawing it.

Parameters:
  • text (str) – Text to draw

  • text_scale (float) – Font size

  • thickness (int) – Thickness of the text

  • color (Tuple[int, int, int]) – Color of the text

Returns:

A tuple containing the drawing function, the content width, and the content height

static generate_text_for_label(label: LabelEntity | ScoredLabel, show_labels: bool, show_confidence: bool) str[source]#

Return a string representing a given label and its associated probability if label is a ScoredLabel.

The exact format of the string depends on the function parameters described below.

Parameters:
  • label (Union[LabelEntity, ScoredLabel]) – Label

  • show_labels (bool) – Whether to render the labels above the shape

  • show_confidence (bool) – Whether to render the confidence above the shape

Returns:

Formatted string (e.g. “Cat 58%”)

Return type:

str

generate_text_scale(image: ndarray) float[source]#

Calculates the scale of the text.

Parameters:

image (np.ndarray) – Image to calculate the text scale for.

Returns:

scale for the text

newline()[source]#

Move the cursor to the next line.

set_cursor_pos(cursor_pos: Coordinate | None = None)[source]#

Move the cursor to a new position.

Parameters:

cursor_pos (Optional[Coordinate]) – New position of the cursor; (0,0) if not specified.

class otx.api.utils.shape_drawer.ShapeDrawer(show_count, is_one_label)[source]#

Bases: DrawerEntity[AnnotationSceneEntity]

ShapeDrawer to draw any shape on a numpy array. Will overlay the shapes in the same way that the UI does.

Parameters:
  • show_count – Whether or not to render the amount of objects on screen in the top left.

  • is_one_label – Whether there is only one label present in the project.

class EllipseDrawer(show_labels, show_confidence)[source]#

Bases: Helpers, DrawerEntity[Ellipse]

Draws ellipses.

draw(image: ndarray, entity: Ellipse, labels: List[ScoredLabel]) ndarray[source]#

Draw the ellipse on the image.

Parameters:
  • image (np.ndarray) – Image to draw on.

  • entity (Ellipse) – Ellipse to draw.

  • labels (List[ScoredLabel]) – Labels to draw.

Returns:

Image with the ellipse drawn on it.

Return type:

np.ndarray

class PolygonDrawer(show_labels, show_confidence)[source]#

Bases: Helpers, DrawerEntity[Polygon]

Draws polygons.

draw(image: ndarray, entity: Polygon, labels: List[ScoredLabel]) ndarray[source]#

Draw polygon and labels on image.

Parameters:
  • image (np.ndarray) – Image to draw on.

  • entity (Polygon) – Polygon to draw.

  • labels (List[ScoredLabel]) – List of labels to draw.

Returns:

Image with polygon drawn on it.

Return type:

np.ndarray

class RectangleDrawer(show_labels, show_confidence)[source]#

Bases: Helpers, DrawerEntity[Rectangle]

Draws rectangles.

draw(image: ndarray, entity: Rectangle, labels: List[ScoredLabel]) ndarray[source]#

Draws a rectangle on the image along with labels.

Parameters:
  • image (np.ndarray) – Image to draw on.

  • entity (Rectangle) – Rectangle to draw.

  • labels (List[ScoredLabel]) – List of labels.

Returns:

Image with rectangle drawn on it.

Return type:

np.ndarray

class TopLeftDrawer(show_labels, show_confidence, is_one_label)[source]#

Bases: Helpers, DrawerEntity[Annotation]

Draws labels in an image’s top left corner.

draw(image: ndarray, entity: Annotation, labels: List[ScoredLabel]) ndarray[source]#

Draw the labels of a shape in the image top left corner.

Parameters:
  • image (np.ndarray) – Image

  • entity (Annotation) – Annotation

  • labels (List[ScoredLabels]) – (Unused) labels to be drawn on the image

Returns:

Image with label on top.

Return type:

np.ndarray

draw_annotation_count(image: ndarray, num_annotations: int) ndarray[source]#

Draw the number of annotations to the top left corner of the image.

Parameters:
  • image (np.ndarray) – Image

  • num_annotations (int) – Number of annotations

Returns:

Image with annotation count on top.

Return type:

np.ndarray

draw_labels(image: ndarray, labels: Sequence[LabelEntity | ScoredLabel]) ndarray[source]#

Draw the labels in the image top left corner.

Parameters:
Returns:

Image with label on top.

Return type:

np.ndarray

draw(image: ndarray, entity: AnnotationSceneEntity, labels: List[ScoredLabel]) ndarray[source]#

Use a compatible drawer to draw all shapes of an annotation to the corresponding image.

Also render a label in the top left if we need to.

Parameters:
  • image – Numpy image, one frame of a video on which to draw something

  • entity – AnnotationSceneEntity entity corresponding to this particular frame of the video

  • labels – Can be passed as an empty list since they are already present in annotation_scene

Returns:

Modified image.