polimorfo.datasets package

Submodules

polimorfo.datasets.coco module

class polimorfo.datasets.coco.CocoDataset(coco_path: str, image_path: str = None, verbose: bool = True)[source]

Bases: object

Process the dataset in COCO format Data Format ——— annotation{

“id”: int, “image_id”: int, “category_id”: int, “segmentation”: RLE or [polygon], “area”: float, “bbox”: [x,y,width,height], “iscrowd”: 0 or 1,

} categories[{ “id”: int, “name”: str, “supercategory”: str, }]

add_annotation(img_id: int, cat_id: int, segmentation: List[List[int]], area: float, bbox: List[T], is_crowd: int, score: float = None) → int[source]

add a new annotation to the dataset

Args:
img_id (int): [description] cat_id (int): [description] segmentation (List[List[int]]): [description] area (float): [description] bbox (List): [description] is_crowd (int): [description] score (float): [optional score of the prediction]
Returns:
int: [description]
add_category(name: str, supercategory: str) → int[source]

add a new category to the dataset

Args:
name (str): [description] supercategory (str): [description]
Returns:
int: cat id
add_image(file_name: Union[str, pathlib.Path], height: int, width: int, **kwargs) → int[source]

Add a new image to the dataset .

Args:
file_name (Union[str, Path]): the file name holding the image height (int): the height of the image width (int): the width of the image
Returns:
int: [description]
compute_area() → None[source]

compute the area of the annotations

copy()[source]

returns a copy of the given dataset

Returns:
[CocoDataset]: a copy of the dataset
count_annotations_per_category(sort_by='value') → Dict[str, int][source]

the count of annotations per category

Args:s
sort_by (str, optional): [description]. Defaults to ‘value’.
Returns:
list – a list of tuples (category_name, number of annotations)
count_images_per_category()[source]

get the number of images per category Returns:

list – a list of tuples category number of images
crop_image(img_idx: int, bbox: Tuple[float, float, float, float], dst_path: pathlib.Path) → str[source]

crop the image id with respect the given bounding box to the specified path

Args:
img_idx (int): the id of the image bbox (Tuple[float, float, float, float]): a bounding box with the format [Xmin, Ymin, Xmax, Ymax] dst_path (Path): the path where the image has to be saved
Returns:
str: the name of the image
dump(path=None, **kvargs)[source]

dump the dataset annotations and the images to the given path

Args:
path ([type]): the path to save the json and the images
Raises:
ValueError: [description]
dumps()[source]

dump the filtered annotations to a json Returns:

object – an object with the dumped annotations
enlarge_box(bbox, height, width, pxls=10)[source]

enlarge a given box of pxls pixels

Args:
bbox ([type]): a tuple, list of np.arry of shape (4,) height (int): the height of the image width (int): the width of the image pxls (int, optional): the number of pixels to add. Defaults to 10.
Returns:
boundingbox: the enlarged bounding box
get_annotations(img_idx: int, category_idxs: List[int] = None) → List[T][source]

returns the annotations of the given image

Args:
img_idx (int): the image idx category_idxs (List[int]): the list of the category to filter the returned annotations
Returns:
List: a list of the annotations in coco format
get_segmentation_mask(img_idx: int, cats_idx: List[int] = None, remapping_dict: Dict[int, int] = None, min_conf: float = 0.5) → Tuple[PIL.Image.Image, float][source]

generate a mask and weight for the given image idx

Args:
img_idx (int): [the id of the image] cats_idx (List[int], optional): [an optional filter over the classes]. Defaults to None. remapping_dict (Dict[int, int], optional): [description]. Defaults to None. min_conf (float): the min confidence to generate the segment, segments with conf below the threshold are replaced as 255 ignore_index (int): the value used to replace segments with confidence below min_conf
Returns:
Tuple[Image.Image, float]: [description]
get_segmentation_mask_multilabel(img_idx: int, cats_idx: List[int] = None, remapping_dict: Dict[int, int] = None, min_conf: float = 0.5) → Tuple[numpy.ndarray, float][source]

get a segmentation mask for multilabel task with shape [C, H, W]

Args:
img_idx (int): [description] cats_idx (List[int], optional): [description]. Defaults to None. remapping_dict (Dict[int, int], optional): [description]. Defaults to None. min_conf (float, optional): [description]. Defaults to 0.5.
Returns:
Tuple[np.np.ndarray, float]: [description]
images_path
keep_categories(ids: List[int], remove_images: bool = False)[source]

keep images and annotations only from the selected categories Arguments:

id_categories {list} – the list of the id categories to keep
load_anns(ann_idxs)[source]
load_image(idx)[source]

load an image from the idx

Args:
idx ([int]): the idx of the image
Returns:
[Pillow.Image]: []
make_index()[source]
mean_pixels(sample: int = 1000) → List[float][source]

compute the mean of the pixels

Args:
sample (int, optional): [description]. Defaults to 1000.
Returns:
List[float]: [description]
merge_categories(cat_to_merge: List[str], new_cat: str) → None[source]
Merge two or more categories labels to a new single category.
Remove from __content the category to be merged and update annotations cat_ids and reindex data with update content.
Args:
cat_to_merge (List[str]): categories to be merged new_cat (str): new label to assign to the merged categories
merge_category_ids(cat_to_merge: Union[List[int], List[str]], new_cat: str) → None[source]
Merge two or more categories labels to a new single category.
Remove from __content the category to be merged and update annotations cat_ids and reindex data with update content.
Args:
cat_to_merge (List[int | str]): categories to be merged new_cat (str): new label to assign to the merged categories
move_annotation(idx: int, bbox: Tuple[float, float, float, float]) → Dict[KT, VT][source]

move the bounding box and the segments of the annotation with respect to given bounding box

Args:
idx (int): the annotation idx bbox (Tuple[float, float, float, float]): the bounding box
Returns:
Dict: a dictioary with the keys iscrowd, bboox, area, segmentation
reindex(by_image_name=True)[source]

reindex images and annotations to be zero based and categories one based

remap_categories(remapping_dict: Dict[int, int]) → None[source]
remove_annotations(ids: List[int], remove_images: bool = False) → None[source]

Remove from the dataset all the annotations ids passes as parameter

Arguments:
img_ann_ids {Dict[int, List[Int]]} – the dictionary of
image id annotations ids to remove
remove_categories(idxs: List[int], remove_images: bool = False) → None[source]

Remove the categories with the relative annotations

Args:
idxs (List[int]): [description]
remove_images(image_idxs: List[int]) → None[source]

remove all the images and annotations in the specified list

Arguments:
image_idxs {List[int]} – [description]
remove_images_without_annotations()[source]
remove_missing_images()[source]

remove the images missing from images folder

save_idx_class_dict(path: Union[str, pathlib.Path] = None) → pathlib.Path[source]

save the idx class dict for the dataset

Args:
path (Union[str, Path], optional): [description]. Defaults to None.
Returns:
Path: [description]
save_images_and_masks(path: Union[str, pathlib.Path], cats_idx: List[int] = None, remapping_dict: Dict[int, int] = None, min_conf: float = 0.5, min_num_annotations: int = None, mode: polimorfo.datasets.coco.MaskMode = <MaskMode.MULTICLASS: 1>) → Tuple[pathlib.Path, pathlib.Path][source]
Save images and segmentation mask into folders:
  • segments
  • images
  • weights.csv that contains the pairs image_name, weight

children of the specified path

Args:
path (Union[str, Path], optional): the path to save the masks. Defaults to None. cats_idx (List[int], optional): [an optional filter over the classes]. Defaults to None. remapping_dict (Dict[int, int], optional): a remapping dictionary for the index to save. Defaults to None. min_conf (float): the min confidence to generate the segment, segments with conf below the threshold are replaced as 255 ignore_index (int): the value used to replace segments with confidence below min_conf min_num_annotations (int, optional): [description]. Defaults to None.
save_segmentation_masks(path: Union[str, pathlib.Path] = None, cats_idx: List[int] = None, remapping_dict: Dict[int, int] = None, min_conf: float = 0.5, mode: polimorfo.datasets.coco.MaskMode = <MaskMode.MULTICLASS: 1>) → None[source]

save the segmentation mask for the given dataset

Args:
path (Union[str, Path], optional): the path to save the masks. Defaults to None. cats_idx (List[int], optional): [an optional filter over the classes]. Defaults to None. remapping_dict (Dict[int, int], optional): a remapping dictionary for the index to save. Defaults to None. min_conf (float): the min confidence to generate the segment, segments with conf below the threshold are replaced as 255 mode: (MaskMode): the mode to save the mask if multiclass are saved as png else as npy file
show_image(img_idx: int = None, img_name: str = None, anns_idx: List[int] = None, ax=None, title: str = None, figsize=(18, 6), colors=None, show_boxes=False, show_masks=True, min_score=0.5, min_area: int = 0, cats_idx: List[int] = None, color_border_only: bool = False, line_width: int = 2, font_size: int = 10) → matplotlib.axes._axes.Axes[source]

show an image with its annotations

Args:
img_idx (int, optional): the idx of the image to load (Optional: None)
in case the value is not specified take a random id

img_name (str, optional): the name of the image to load anns_idx (List[int], optional): [description]. Defaults to None. ax ([type], optional): [description]. Defaults to None. title (str, optional): [description]. Defaults to None. figsize (tuple, optional): [description]. Defaults to (18, 6). colors ([type], optional): [description]. Defaults to None. 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. cats_idx (List, optional): the list of categories to show. Defaults to None to display all the categories color_border_only (bool, optional): if True color only the border of the component. Defaults to False, font_size (int, optional): the font size default is 10

Returns:
plt.Axes: [description]
show_images(idxs_or_num: Union[List[int], int] = None, num_cols=4, figsize=(32, 32), show_masks=True, show_boxes=False, min_score: float = 0.5, min_area: int = 0, cats_idx: List[int] = None, color_border_only: bool = False, line_width: int = 2, font_size: int = 10, colors=None) → matplotlib.figure.Figure[source]

show the images with their annotations

Args:
img_idxs (Union[List[int], int]): a list of image idxs to display or the number of images (Optional: None)
If None a random sample of 8 images is taken from the db

num_cols (int, optional): [description]. Defaults to 4. figsize (tuple, optional): [description]. Defaults to (32, 32). show_masks (bool, optional): [description]. Defaults to True. show_boxes (bool, optional): [description]. Defaults to False. min_score (float, optional): [description]. Defaults to 0.5. min_area (int, optional): the min area of the annotations to display, Default to 0

Returns:
plt.Figure: [description]
update_images_path(func)[source]

update the images path Args:

update_images (UpdateImages): a class with a callable function to change the path

polimorfo.datasets.utils module

Module contents