polimorfo.utils package¶
Submodules¶
polimorfo.utils.datautils module¶
-
polimorfo.utils.datautils.
download_file
(name: str, url: str, file_hash=None, extract: bool = True, cache_dir: str = '~/.polimorfo', cache_subdir: str = 'datasets') → List[pathlib.Path][source]¶ Downloads a file frolsm a URL if it not already saved Arguments:
name {str} – the name of the file (e.g. ) url {str} – the url of the file or the idx of the file in
case files are download from google drive- Keyword Arguments:
file_hash {str} – the hash of the file downloads (default: {None}) extract {bool} – try to extract the file (default: {True}) cache_dir {str} – the default folder where the file is saved
(default: {carambola.utils.datautils.CACHE_DIR})- cache_subdir {str} – the subdir where the file is downloaded
- (default: {carambola.utils.datautils.CACHE_SUBDIR})
-
polimorfo.utils.datautils.
download_from_gdrive
(uri: str, dst_path: str) → pathlib.Path[source]¶ download a file/folder from google drive Given a url https://drive.google.com/file/d/1EcUzQPNQXQGiHES9gU7oh-886wbBH3VF/view?usp=sharing the idx -> 1EcUzQPNQXQGiHES9gU7oh-886wbBH3VF Arguments:
uri {str} – the id of the file to download or the full google drive url dst_path {str} – the path to save the file- Returns:
- Path – the path where the file is saved
-
polimorfo.utils.datautils.
download_url
(url: str, dst_path: str) → Tuple[pathlib.Path, int][source]¶ download a url to the destination folder Arguments:
url {str} – [description] dst_path {str} – [description]- Returns:
- Tuple[Path, int] – [description]
-
polimorfo.utils.datautils.
extract_archive
(file_path: str, dst_path: str = '', archive_format='auto') → List[pathlib.Path][source]¶ Extract the archive if it match tar, tar.gz, tar.bz or zip format Arguments:
file_path {str} – the path to the archive- Keyword Arguments:
- dst_path {str} – the path to extract the folder (default: {None}
- the directory where the archive is placed)
- archive_format {str} – The format of the archive (default: {‘auto’})
- Options are: ‘auto’, ‘tar’, ‘zip’
- Returns:
- List[Path] – the paths where the file is saved
-
polimorfo.utils.datautils.
validate_file
(fpath, file_hash, algorithm='auto', chunk_size=65535)[source]¶ Validates a file against a sha256 or md5 hash. Arguments:
fpath: path to the file being validated file_hash: The expected hash string of the file.
The sha256 and md5 hash algorithms are both supported.- algorithm: Hash algorithm, one of ‘auto’, ‘sha256’, or ‘md5’.
- The default ‘auto’ detects the hash algorithm in use.
chunk_size: Bytes to read at a time, important for large files.
- Returns:
- Whether the file is valid
polimorfo.utils.imageutils module¶
polimorfo.utils.maskutils module¶
-
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.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.xyxy: 1>, only_class_idxs: List[int] = None)[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 [nmasks, 1, 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.xyxy. only_class_idxs (List[int], optional): [description]. Defaults to None.
- Returns:
- [type]: [description]
-
polimorfo.utils.visualizeutils.
draw_segmentation
(img: Union[numpy.ndarray, PIL.Image.Image], logits_or_mask: 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 logits_or_mask (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_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.