COCOVisualizer
- class cocohelper.visualizer.COCOVisualizer[source]
Bases:
object
This class contains methods to visualize COCO images and annotations.
- Parameters:
helper – Coco dataset to visualize.
Method List
_visualize
(image, annotations[, show_bbox, ...])Draws an image and its annotations using matplotlib.
draw_bounding_box
(image, bbox[, color, ...])Draws a bounding box on top of the input image.
draw_segmentation_mask
(image, segmentation)Draw segmentation mask on top of the input image.
load_image_array
(img_id)Load image with a given image id and returns it as numpy array.
pick_color_palette
(number)Returns standard RGB colors palette (+ random colors, if needed).
pick_random_colors
(number)Generate random RGB colors.
visualize
(img_id[, show_bbox, show_segmentation])Visualize an image given its image id using matplotlib.
Methods Details
- _visualize(image, annotations, show_bbox=False, show_segmentation=False, title=None, img_id=None, figsize=None, dpi=None, ax=None, **kwargs)[source]
Draws an image and its annotations using matplotlib.
- Parameters:
image (ndarray) – image to be shown.
annotations (list) – annotations for the given the image.
show_bbox (bool) – if true, show bounding boxes on top of the image.
show_segmentation (bool) – if true, show segmentation masks on top of the image.
title (Optional[str]) – title for the plotted image.
img_id (Optional[int]) – image id in the COCO dataset. Needed only if title is None.
figsize (Optional[Sequence]) – figure size for the plotted image (for details, see matplotlib plt.figure()).
dpi (Optional[int]) – dpi for the plotted image (for details, see matplotlib plt.figure()).
ax (Optional[Axes]) –
optional Axes object, pass one of the axes generated by plt.subplots to show the figure in a subplot. .. rubric:: Examples
To show two figures in two columns >>> fig, axs = plt.subplots(1, 2) >>> ch = COCOHelper(…) >>> visualizer = COCOVisualizer(ch) >>> visualizer.visualize(img_id=0, ax=axs[0]) >>> visualizer.visualize(img_id=1, ax=axs[1]) >>> plt.show()
**kwargs – additional kwargs for matplotlib plt.figure().
- Returns:
None.
- Return type:
None
- static draw_bounding_box(image, bbox, color=(128, 128, 128), title=None, bbox_thickness=2, **kwargs)[source]
Draws a bounding box on top of the input image.
- Parameters:
image (ndarray) – numpy array with the image.
bbox (Sequence) – bounding box coordinates.
color (Sequence) – bounding box color, defaults to gray color.
title (Optional[str]) – title for the bounding box, default to empty title.
bbox_thickness (int) – thickness of the bounding box. Use larger number to make the boxes more visible in large images.
**kwargs – optional kwargs for plotting bounding boxes.
- Returns:
Numpy array with the given image and a bounding box drawn on top of it.
- Return type:
ndarray
- static draw_segmentation_mask(image, segmentation, color=(128, 128, 128), **kwargs)[source]
Draw segmentation mask on top of the input image.
- Parameters:
image (ndarray) – numpy array with the image.
segmentation (Sequence) – segmentation mask.
color (Sequence) – bounding box color, defaults to gray color.
**kwargs – optional kwargs for plotting the segmentation mask.
- Returns:
Numpy array with the given image and a segmentation drawn on top of it.
- Return type:
ndarray
- load_image_array(img_id)[source]
Load image with a given image id and returns it as numpy array.
- Parameters:
img_id (int) – image id.
- Returns:
A numpy array with the image associated with the image id in the COCO dataset.
- Return type:
ndarray
- pick_color_palette(number)[source]
Returns standard RGB colors palette (+ random colors, if needed).
- Parameters:
number (int) – the number of RGB color triplets to fetch.
- Returns:
A list of triplets with a number RGB colors. The first twelve colors follow a standard palette, the remaining colors will be randomly picked.
- Return type:
List
- static pick_random_colors(number)[source]
Generate random RGB colors.
- Parameters:
number (int) – the number of RGB color triplets to fetch
- Returns:
A list of triplets containing a number RGB colors. The colors are randomly picked.
- Return type:
List
- visualize(img_id, show_bbox=False, show_segmentation=False, **kwargs)[source]
Visualize an image given its image id using matplotlib.
If show_bbox or show_segmentation are True, show also the image annotations on top of the plotted image.
- Parameters:
img_id (int) – image id in the COCO dataset.
show_bbox (bool) – if true, show bounding boxes on top of the image.
show_segmentation (bool) – show segmentation masks on top of the image.
**kwargs – additional kwargs for the generated matplotlib figure (see COCOVisualizer._visualize() docs).
- Returns:
None.
- Return type:
None