Structures package

Submodules

Structures.BoundingBox module

class Structures.BoundingBox.BoundingBox(xmin: float, ymin: float, xmax: float, ymax: float)[source]

Bases: object

Structure to store a bounding box.

xmin

Minimum relative x coordinate.

Type:

float

ymin

Minimum relative y coordinate.

Type:

float

xmax

Maximum relative x coordinate.

Type:

float

ymax

Maximum relative y coordinate.

Type:

float

Overloaded operators:
  • __repr__

  • __eq__

  • __bool__

  • __iter__

Create a bounding box object with the relative coordinates to the image size of the top-left and bottom-right corners.

Parameters:
  • xmin (float) – Minimum relative x coordinate.

  • ymin (float) – Minimum relative y coordinate.

  • xmax (float) – Maximum relative x coordinate.

  • ymax (float) – Maximum relative y coordinate.

crop_image(image: ndarray) ndarray[source]

Crop an image with the bounding box.

Parameters:

image (np.ndarray) – Numpy image of shape (h, w, c) or (h, w).

Returns:

A cropped copy of the image with the region inside the

bounding box.

Return type:

np.ndarray

static deserialize(value: Dict[str, float]) BoundingBox[source]

Deserialize value.

Parameters:

value (Dict[str, float]) –

Returns:

BoundingBox

classmethod from_absolute(xmin: int, ymin: int, xmax: int, ymax: int, image_width: int, image_height: int) BoundingBox[source]

Create a bounding box object from the absolute image coordinates of the top-left and bottom-right corners.

Parameters:
  • xmin (int) – Minimum x coordinate.

  • ymin (int) – Minimum y coordinate.

  • xmax (int) – Maximum x coordinate.

  • ymax (int) – Maximum y coordinate.

  • image_width (int) – Image width.

  • image_height (int) – Image height.

Returns:

A BoundingBox object.

Return type:

BoundingBox

static from_xywh(x: float | int, y: float | int, w: float | int, h: float | int, absolute: bool = False, image_width: int | None = None, image_height: int | None = None) BoundingBox[source]

Create a bounding box object from the center coordinates and the width and height of a box.

Parameters:
  • x (float) – Center x coordinate.

  • y (float) – Center y coordinate.

  • w (float) – Width of the box.

  • h (float) – Height of the box.

  • absolute (bool, optional) – If the coordinates passed are relative. Defaults to False.

  • image_width (Optional[int], optional) – Image width used when absolute is True. Defaults to None.

  • image_height (Optional[int], optional) – Image height used when absolute is True. Defaults to None.

Returns:

A BoundingBox object.

Return type:

BoundingBox

get_area(absolute: bool = False, image_width: int | None = None, image_height: int | None = None) float | int[source]

Get the area of the bounding box.

Parameters:
  • absolute (bool, optional) – Return the absolute value, otherwise relative to the image. Defaults to False.

  • image_width (Optional[int], optional) – Image width. Defaults to None.

  • image_height (Optional[int], optional) – Image height. Defaults to None.

Returns:

The area of the bounding box.

Return type:

float

get_center_x(absolute: bool = False, image_width: int | None = None) float | int[source]

Get the center x coordinate of the bounding box.

Parameters:
  • absolute (bool, optional) – Return the absolute coordinate, otherwise relative to the image. Defaults to False.

  • image_width (Optional[int], optional) – Image width. Defaults to None.

Returns:

The center x coordinate.

Return type:

Union[float, int]

get_center_y(absolute: bool = False, image_height: int | None = None) float | int[source]

Get the center y coordinate of the bounding box.

Parameters:
  • absolute (bool, optional) – Return the absolute coordinate, otherwise relative to the image. Defaults to False.

  • image_height (Optional[int], optional) – Image height. Defaults to None.

Returns:

The center y coordinate.

Return type:

Union[float, int]

get_height(absolute: bool = False, image_height: int | None = None) float | int[source]

Get the height of the bounding box.

Parameters:
  • absolute (bool, optional) – Return the absolute value, otherwise relative to the image. Defaults to False.

  • image_height (Optional[int], optional) – Image height. Defaults to None.

Returns:

The height of the bounding box.

Return type:

Union[float, int]

get_width(absolute: bool = False, image_width: int | None = None) float | int[source]

Get the width of the bounding box.

Parameters:
  • absolute (bool, optional) – Return the absolute value, otherwise relative to the image. Defaults to False.

  • image_width (Optional[int], optional) – Image width. Defaults to None.

Returns:

The width of the bounding box.

Return type:

Union[float, int]

get_xywh(absolute: bool = False, image_width: int | None = None, image_height: int | None = None) ndarray[source]

Get the center coordinates and the box width and height as a numpy array.

Parameters:
  • absolute (bool, optional) – Return the absolute value, otherwise relative to the image. Defaults to False.

  • image_width (Optional[int], optional) – Image width. Defaults to None.

  • image_height (Optional[int], optional) – Image height. Defaults to None.

Returns:

[center x, center y, box width, box height].

Return type:

np.ndarray

get_xyxy(absolute: bool = False, image_width: int | None = None, image_height: int | None = None) ndarray[source]

Get the maximum and minimum bounding box coordinates as a numpy array.

Parameters:
  • absolute (bool, optional) – Return the absolute value, otherwise relative to the image. Defaults to False.

  • image_width (Optional[int], optional) – Image width. Defaults to None.

  • image_height (Optional[int], optional) – Image height. Defaults to None.

Returns:

[minimum x, minimum y, maximum x, maximum y].

Return type:

np.ndarray

is_empty() bool[source]

Return True if height or width is 0.

scale(factor: Tuple[float, float] | float) BoundingBox[source]

Scale the bounding box from the center by a factor.

Parameters:

factor (Union[Tuple[float, float], float]) – A value by which both width and height will be scaled or a tuple with the width-factor and the height-factor.

Returns:

A new scaled BoundingBox object.

Return type:

BoundingBox

serialize() Dict[str, float][source]

Serialize to a basic Python datatype.

Returns:

Dict[str, float]

classmethod validate(v)[source]

Structures.Emotion module

class Structures.Emotion.Emotion(value)[source]

Bases: Enum

An enumeration.

ANGER = 'ANGER'
DISGUST = 'DISGUST'
FEAR = 'FEAR'
HAPPINESS = 'HAPPINESS'
NEUTRAL = 'NEUTRAL'
SADNESS = 'SADNESS'
SURPRISE = 'SURPRISE'

Structures.Gender module

class Structures.Gender.Gender(value)[source]

Bases: Enum

An enumeration.

FEMALE = 'FEMALE'
MALE = 'MALE'
OTHER = 'OTHER'

Structures.Image module

class Structures.Image.Image(path: str | Path = '', image: ndarray | None = None, width: int | None = None, height: int | None = None, id: str = '')[source]

Bases: object

Structure to store an image. Allow to load an image from a local file or an URL.

path

Path or URL to an image.

Type:

Union[str, Path]

id

Id of a ngsi-ld image entity.

Type:

str

Overloaded operators:
  • __str__

  • __eq__

  • __repr__

  • __iter__

Create an Image object.

Parameters:
  • path (Union[str, Path], optional) – Path or URL to an image. Defaults to “”.

  • image (Optional[np.ndarray], optional) – A np.ndarray image. Defaults to None.

  • width (Optional[int], optional) – Width of the image. Automatically obtained from the image if supplied. Defaults to None.

  • height (Optional[int], optional) – Height of the image. Automatically obtained from the image if supplied. Defaults to None.

  • id (str, optional) – Id of an ngsi-ld image entity. Defaults to “”.

static deserialize(value: Dict[str, int]) Image[source]

Deserialize value.

Parameters:

value (Dict[str, int]) –

Returns:

Image

static from_path(path: Path) Image[source]

Create an Image object from a Path.

Parameters:

path (Path) – Path to an image file.

Returns:

Image.

static from_url(url: str) Image[source]

Create an Image object from an URL.

Parameters:

url (str) – URL to an image.

Returns:

Image.

property height: int

Return the height of the image, load the image if it’s necessary.

property image: ndarray

Return the image as a numpy array, load the image if it’s necessary.

load_image()[source]

Manually load the image into memory.

save_image(path: str | Path | None = None)[source]

Save the image to a file.

Parameters:

path (Optional[Union[str, Path]]) – Optional output file path. If None, the self.path will be used

serialize() Dict[str, int][source]

Serialize to a basic Python datatype.

Returns:

Dict[str, int]

classmethod validate(v)[source]
property width: int

Return the width of the image, load the image if it’s necessary.

Structures.Instance module

class Structures.Instance.Instance(fields: dict | None = None)[source]

Bases: object

Structure used to store the output of a machine learning model.

Overloaded operators:
  • __getattr__

  • __getitem__

  • __setattr__

  • __eq__

  • __iter__

  • __str__

Example:

instance = Instance().set("label", "dog").set("confidence", 0.8)
confidence = instance.confidence

Initialize an Instance. If fields is not None, it will be used to set the initial attributes.

Parameters:

fields (Optional[dict], optional) – Optional values to set. Defaults to None.

property fields: List[str]

List of names of the attributes set.

Returns:

List of attribute names.

Return type:

List[str]

get(name: str, default: Any | None = None) Any[source]

Get an attribute by its name.

Parameters:
  • name (str) – The name of the attribute.

  • default (Any, optional) – Default value in case the attribute does not exist. Defaults to None.

Returns:

The value of the attribute or the default value if it does not

exists.

Return type:

Any

has(name: str) bool[source]

Check if the instance has an attribute named name.

Parameters:

name (str) – The name of an attribute.

Returns:

True if an attribute with the given name exits.

Return type:

bool

remove(name: str)[source]

Remove an attribute from the instance by its name.

Parameters:

name (str) – Name of the attribute to remove.

set(name: str, value: Any) Instance[source]

Store a value with the given name.

Parameters:
  • name (str) – Name that will be used to access the value.

  • value (Any) – Any object.

Returns:

Self.

Return type:

Instance

set_dict(fields: dict) Instance[source]

Set multiple values at once. The keys of the dictionary will be used as the names of the attributes.

Parameters:

fields (dict) – Dictionary of values to set.

Returns:

self

Return type:

Instance

Structures.Keypoints module

class Structures.Keypoints.BaseKeypoints(keypoints: ndarray, confidence_threshold: float = 0.05)[source]

Bases: object

Store keypoints data.

Overloaded operators:
  • __len__

  • __eq__

  • __str__

  • __repr__

  • __iter__

Create a Keypoints object.

Parameters:
  • keypoints (np.ndarray) – Array of shape (K, 3), where K is the number of keypoints and the last dimension corresponds to (x, y, confidence), where x and y are the relative image coordinates.

  • confidence_threshold (float, optional) – Keypoints confidence threshold to determine if a keypoint is visible or not. Defaults to 0.05.

classmethod deserialize(keypoints_dict: dict) BaseKeypoints[source]

Deserialize value.

Parameters:

keypoints_dict (dict) –

Returns:

BaseKeypoints

classmethod from_absolute_keypoints(keypoints: ndarray, image_width: int, image_height: int, **kwargs) Type[BaseKeypoints][source]
static from_named_keypoints(named_keypoints: Dict[str, Tuple[float, float, float]], **kwargs) BaseKeypoints[source]

Create a Keypoints object from a dict of named keypoints.

Parameters:

named_keypoints (Dict[str, Tuple[float, float, float]]) – A dict with the keypoints by its name. Keypoints must come in the form of (x, y, confidence), where x and y are the relative image coordinates.

Returns:

BaseKeypoints

labels: List[str] = []
property named_keypoints: Dict[str, Tuple[float, float, float]]

Return a dict with the keypoints by its name.

serialize() dict[source]

Serialize to a basic Python datatype.

Returns:

dict

classmethod validate(v)[source]
property visible_keypoints: Dict[str, Tuple[float, float, float]]

Return a dict with only the visible keypoints. Visible keypoints are those with a confidence greater or equal than confidence_threshold.

Returns:

Dict of visible keypoints

by its name.

Return type:

Dict[str, Tuple[float, float, float]]

class Structures.Keypoints.COCOKeypoints(keypoints: ndarray, confidence_threshold: float = 0.05)[source]

Bases: BaseKeypoints

Store keypoints data of a person with the COCO format (17 keypoints).

Overloaded operators:
  • __len__

  • __eq__

  • __str__

  • __iter__

Create a Keypoints object.

Parameters:
  • keypoints (np.ndarray) – Array of shape (K, 3), where K is the number of keypoints and the last dimension corresponds to (x, y, confidence), where x and y are the relative image coordinates.

  • confidence_threshold (float, optional) – Keypoints confidence threshold to determine if a keypoint is visible or not. Defaults to 0.05.

static from_named_keypoints(named_keypoints: Dict[str, Tuple[float, float, float]], **kwargs) COCOKeypoints[source]

Create a Keypoints object from a dict of named keypoints.

Parameters:

named_keypoints (Dict[str, Tuple[float, float, float]]) – A dict with the keypoints by its name. Keypoints must come in the form of (x, y, confidence), where x and y are the relative image coordinates.

Returns:

COCOKeypoints

labels: List[str] = ['nose', 'left_eye', 'right_eye', 'left_ear', 'right_ear', 'left_shoulder', 'right_shoulder', 'left_elbow', 'right_elbow', 'left_wrist', 'right_wrist', 'left_hip', 'right_hip', 'left_knee', 'right_knee', 'left_ankle', 'right_ankle']
property named_keypoints: Dict[str, Tuple[float, float, float]]

Return a dict with the keypoints by its name.

Structures.Keypoints.keypoints_dict_to_absolute(keypoints_dict: Dict[str, Tuple[float, float, float]], image_width: int, image_height: int) Dict[str, Tuple[float, float, float]][source]
Structures.Keypoints.keypoints_to_absolute(keypoints: ndarray, image_width: int, image_height: int) ndarray[source]

Structures.SegmentationMask module

class Structures.SegmentationMask.SegmentationMask(mask: ndarray | None = None, rle: dict | None = None)[source]

Bases: object

Store data about a single segmentation mask.

mask

Binary mask of shape (H, W).

Type:

np.ndarray

Overloaded operators:
  • __str__

  • __repr__

  • __eq__

  • __iter__

Create a SegmentationMask from a binary mask or an encoded rle.

Parameters:
  • mask (Optional[np.ndarray], optional) – Binary mask of shape (H, W). Defaults to None.

  • rle (Optional[dict], optional) – Encoded rle mask. Defaults to None.

property area: float
static deserialize(hex_rle: dict) SegmentationMask[source]

Deserialize value.

Parameters:

hex_rle (dict) –

Returns:

SegmentationMask

property height: int
resize(width: int, height: int) SegmentationMask[source]

Return a resized copy of the mask.

Parameters:
  • width (int) – Target width of the mask.

  • height (int) – Target height of the mask.

Returns:

A new resized SegmentationMask object.

Return type:

SegmentationMask

property rle: dict

Get the rle-encoded mask.

Returns:

A dict with the size and rle-encoded mask.

Return type:

dict

serialize() dict[source]

Serialize to a basic Python datatype.

Returns:

dict

classmethod validate(v)[source]
property width: int

Module contents