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]
-
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
-
dataset_report
() → None[source]¶ returns: - the a box plot for the images areas - a boxplot for the bounding boxes area - a boxplot for the bounding boxes area per class - a boxplot of the masks area - a boxplot of the mask area per class - the weights per class in terms of segmentation mask
-
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, ignore_index: int = 255) → 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]
-
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_image
(idx)[source]¶ load an image from the idx
- Args:
- idx ([int]): the idx of the image
- Returns:
- [Pillow.Image]: []
-
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
-
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]
-
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, ignore_index: int = 255, min_num_annotations: int = None) → 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, ignore_index: int = 255) → 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 ignore_index (int): the value used to replace segments with confidence below min_conf
-
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) → 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
- 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) → 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]
-