polimorfo.utils package

Submodules

polimorfo.utils.datautils module

polimorfo.utils.imageutils module

polimorfo.utils.maskutils module

polimorfo.utils.maskutils.mask_to_polygon(mask, min_score: float = 0.5, approx: float = 0.0, relative: bool = True)[source]

generate polygons from masks

Args:
mask (np.ndarray): a binary mask min_score (float, optional): [description]. Defaults to 0.5. approx (float, optional): it approximate the polygons to reduce the number of points. Defaults to 0.0 relative (bool, optional): it the value of the approximation is computed on the relative amount of point or with respect to all the points
Returns:
[type]: [description]
polimorfo.utils.maskutils.polygons_to_mask(polygons, height, width)[source]

convert polygons to mask. Filter all the polygons with less than 4 points

Args:
polygons ([type]): [description] height ([type]): [description] width ([type]): [description]
Returns:
[type]: a mask of format num_classes, heigth, width
polimorfo.utils.maskutils.area(mask, min_score=0.5)[source]
polimorfo.utils.maskutils.bbox(polygons, height, width)[source]
polimorfo.utils.maskutils.coco_poygons_to_mask(segmentations, height, width) → numpy.ndarray[source]

polimorfo.utils.visualizeutils module

class polimorfo.utils.visualizeutils.BoxType[source]

Bases: enum.Enum

An enumeration.

xywh = 2
xyxy = 1
polimorfo.utils.visualizeutils.change_color_brightness(color: Tuple, brightness_factor: float)[source]

Depending on the brightness_factor, gives a lighter or darker color i.e. a color with less or more saturation than the original color. Args:

color: color of the polygon. Refer to matplotlib.colors for a full list of
formats that are accepted.
brightness_factor (float): a value in [-1.0, 1.0] range. A lightness factor of
0 will correspond to no change, a factor in [-1.0, 0) range will result in a darker color and a factor in (0, 1.0] range will result in a lighter color.
Returns:
modified_color (tuple[double]): a tuple containing the RGB values of the
modified color. Each value in the tuple is in the [0.0, 1.0] range.
polimorfo.utils.visualizeutils.create_text_labels(classes: List[int], scores: List[float], idx_class_dict: Dict[int, str])[source]
Args:
classes (list[int] or None): scores (list[float] or None): idx_class_dict (Dict[int, str] or None):
Returns:
list[str]
polimorfo.utils.visualizeutils.draw_instances(img: Union[PIL.Image.Image, numpy.ndarray], boxes: Union[numpy.ndarray, List[T]], labels: Union[numpy.ndarray, List[T]], scores: Union[numpy.ndarray, List[T]], masks: Union[numpy.ndarray, List[T]], idx_class_dict: Dict[int, str], title: str = '', figsize: Tuple = (16, 8), show_boxes: bool = False, show_masks: bool = True, min_score: float = 0.5, min_area: int = 0, colors: List[T] = None, ax: matplotlib.axes._axes.Axes = None, box_type: polimorfo.utils.visualizeutils.BoxType = <BoxType.xywh: 2>, only_class_idxs: List[int] = None, color_border_only: bool = False, alpha: float = 0.3, line_width: int = 2, font_size: int = 10, show_text: bool = True, *args, **kwargs)[source]

draw the instances from a object detector or an instance segmentation model

Args:
img (np.ndarray): an image with shape (width, height, channels) boxes (np.ndarray): an array of shape (nboxes, 4) labels (np.ndarray): an array of shape (nlabels,) scores (np.ndarray): an array of shape (nscores,) masks (np.ndarray): an array of shape [num_masks, width, height ] idx_class_dict (Dict[int, str]): a dictionary that maps class id to class name title (str, optional): [description]. Defaults to ‘’. figsize (Tuple, optional): [description]. Defaults to (16, 8). show_boxes (bool, optional): [description]. Defaults to False. show_masks (bool, optional): [description]. Defaults to True. min_score (float, optional): [description]. Defaults to 0.5. colors (List, optional): [description]. Defaults to None. ax (plt.Axes, optional): [description]. Defaults to None. box_type (BoxType, optional): [description]. Defaults to BoxType.xywh. only_class_idxs (List[int], optional): [description]. Defaults to None. color_only_border (bool): if true if color only the border (default is False) line_width: (float): the width of the line. Defaults is 2
Returns:
[type]: [description]
polimorfo.utils.visualizeutils.draw_segmentation(img: Union[numpy.ndarray, PIL.Image.Image], probs: numpy.ndarray, idx_name_dict: Dict[int, str], min_conf: float, colors: List[T] = None, title: str = '', ax: matplotlib.axes._axes.Axes = None, figsize: Tuple[int, int] = (16, 8), alpha: float = 0.3, fill: bool = True, min_score: float = 0.5)[source]

draw the result from a segmentation model

Args:

img (Union[np.ndarray, Image.Image]): an PIL image or a numpy array probs (np.ndarray): it accepts:

  • the logits coming from the model with shape (n_classes, H, W), or
  • the mask coming from true annotations with shape (H,W) and containing pixel classification

idx_name_dict (Dict[int, str]): min_conf (float): the min confidence of the mask given as output colors (List, optional): the colors to diplay categories. Defaults to None. title (str, optional): [description]. Defaults to ‘’. ax (plt.Axes, optional): [description]. Defaults to None. figsize (Tuple[int, int], optional): [description]. Defaults to (16, 8).

Returns:
[plt.Axes]: the ax of the given plot
polimorfo.utils.visualizeutils.draw_segmentation_multilabel(img: PIL.Image.Image, probs: numpy.ndarray, idx_name_dict: Dict[int, str], min_conf: float, colors: List[T] = None, title: str = '', ax: matplotlib.axes._axes.Axes = None, figsize: Tuple[int, int] = (16, 8))[source]

draw the result from a segmentation model

Args:
img (Union[np.ndarray, Image.Image]): an PIL image or a numpy array probs (np.ndarray): a probability tensor of shape (n_classes, H, W) idx_name_dict (Dict[int, str]): min_conf (float): the min confidence of the mask given as output colors (List, optional): the colors to diplay categories. Defaults to None. title (str, optional): [description]. Defaults to ‘’. ax (plt.Axes, optional): [description]. Defaults to None. figsize (Tuple[int, int], optional): [description]. Defaults to (16, 8).
Returns:
[plt.Axes]: the ax of the given plot
polimorfo.utils.visualizeutils.draw_text(ax: matplotlib.axes._axes.Axes, text: str, position: Tuple, font_size: float, color: str = 'g', horizontal_alignment: str = 'center', rotation: int = 0)[source]
Args:

text (str): class label position (tuple): a tuple of the x and y coordinates to place text on image. font_size (int, optional): font of the text. If not provided, a font size

proportional to the image width is calculated and used.
color: color of the text. Refer to matplotlib.colors for full list
of formats that are accepted.

horizontal_alignment (str): see matplotlib.text.Text rotation: rotation angle in degrees CCW

Returns:
output (VisImage): image object with text drawn.
polimorfo.utils.visualizeutils.generate_colormap(nelems: int, scaled: bool = False, bright: bool = True)[source]

Module contents