blenderproc.python.writer.CocoWriterUtility module¶
-
class
blenderproc.python.writer.CocoWriterUtility.
CocoWriterUtility
[source]¶ Bases:
object
-
static
bbox_from_binary_mask
(binary_mask)[source]¶ Returns the smallest bounding box containing all pixels marked “1” in the given image mask.
Parameters: binary_mask ( ndarray
) – A binary image mask with the shape [H, W].Return type: List
[int
]Returns: The bounding box represented as [x, y, width, height]
-
static
binary_mask_to_polygon
(binary_mask, tolerance=0)[source]¶ Converts a binary mask to COCO polygon representation
Parameters: - binary_mask (
ndarray
) – a 2D binary numpy array where ‘1’s represent the object - tolerance (
int
) – Maximum distance from original points of polygon to approximated polygonal chain. If tolerance is 0, the original coordinate array is returned.
Return type: List
[ndarray
]- binary_mask (
-
static
calc_binary_mask_area
(binary_mask)[source]¶ Returns the area of the given binary mask which is defined as the number of 1s in the mask.
Parameters: binary_mask ( ndarray
) – A binary image mask with the shape [H, W].Return type: int
Returns: The computed area
-
static
close_contour
(contour)[source]¶ Makes sure the given contour is closed.
Parameters: contour ( ndarray
) – The contour to close.Return type: ndarray
Returns: The closed contour.
-
static
create_annotation_info
(annotation_id, image_id, category_id, binary_mask, mask_encoding_format, tolerance=2)[source]¶ Creates info section of coco annotation
Parameters: - annotation_id (
int
) – integer to uniquly identify the annotation - image_id (
int
) – integer to uniquly identify image - category_id (
int
) – Id of the category - binary_mask (
ndarray
) – A binary image mask of the object with the shape [H, W]. - mask_encoding_format (
str
) – Encoding format of the mask. Type: string. - tolerance (
int
) – The tolerance for fitting polygons to the objects mask.
Return type: - annotation_id (
-
static
create_image_info
(image_id, file_name, image_size)[source]¶ Creates image info section of coco annotation
Parameters: Return type:
-
static
generate_coco_annotations
(inst_segmaps, inst_attribute_maps, image_paths, supercategory, mask_encoding_format, existing_coco_annotations=None, label_mapping=None)[source]¶ Generates coco annotations for images
Parameters: - inst_segmaps – List of instance segmentation maps
- inst_attribute_maps – per-frame mappings with idx, class and optionally supercategory/bop_dataset_name
- image_paths – A list of paths which points to the rendered images.
- supercategory – name of the dataset/supercategory to filter for, e.g. a specific BOP dataset
- mask_encoding_format – Encoding format of the binary mask. Type: string.
- existing_coco_annotations – If given, the new coco annotations will be appended to the given coco annotations dict.
- label_mapping (
Optional
[LabelIdMapping
]) – The label mapping which should be used to label the categories based on their ids. If None, is given then the name field in the csv files is used or - if not existing - the category id itself is used.
Returns: dict containing coco annotations
-
static
merge_coco_annotations
(existing_coco_annotations, new_coco_annotations)[source]¶ Merges the two given coco annotation dicts into one.
Currently this requires both coco annotations to have the exact same categories/objects. The “images” and “annotations” sections are concatenated and respective ids are adjusted.
Parameters: - existing_coco_annotations – A dict describing the first coco annotations.
- new_coco_annotations – A dict describing the second coco annotations.
Returns: A dict containing the merged coco annotations.
-
static
-
blenderproc.python.writer.CocoWriterUtility.
binary_mask_to_rle
(binary_mask)[source]¶ Converts a binary mask to COCOs run-length encoding (RLE) format. Instead of outputting a mask image, you give a list of start pixels and how many pixels after each of those starts are included in the mask. :type binary_mask:
ndarray
:param binary_mask: a 2D binary numpy array where ‘1’s represent the object :rtype:Dict
[str
,List
[int
]] :return: Mask in RLE format
-
blenderproc.python.writer.CocoWriterUtility.
rle_to_binary_mask
(rle)[source]¶ Converts a COCOs run-length encoding (RLE) to binary mask. :type rle:
Dict
[str
,List
[int
]] :param rle: Mask in RLE format :rtype:ndarray
:return: a 2D binary numpy array where ‘1’s represent the object
-
blenderproc.python.writer.CocoWriterUtility.
write_coco_annotations
(output_dir, instance_segmaps=None, instance_attribute_maps=None, colors=None, color_file_format='PNG', mask_encoding_format='rle', supercategory='coco_annotations', append_to_existing_output=True, segmap_output_key='segmap', segcolormap_output_key='segcolormap', rgb_output_key='colors', jpg_quality=95, label_mapping=None, file_prefix='')[source]¶ Writes coco annotations in the following steps: 1. Locate the seg images 2. Locate the rgb maps 3. Locate the seg mappings 4. Read color mappings 5. For each frame write the coco annotation
Parameters: - output_dir (
str
) – Output directory to write the coco annotations - instance_segmaps (
Optional
[List
[ndarray
]]) – List of instance segmentation maps - instance_attribute_maps (
Optional
[List
[dict
]]) – per-frame mappings with idx, class and optionally supercategory/bop_dataset_name - colors (
Optional
[List
[ndarray
]]) – List of color images - color_file_format (
str
) – Format to save color images in - mask_encoding_format (
str
) – Encoding format of the binary masks. Default: ‘rle’. Available: ‘rle’, ‘polygon’. - supercategory (
str
) – name of the dataset/supercategory to filter for, e.g. a specific BOP dataset set by ‘bop_dataset_name’ or any loaded object with specified ‘cp_supercategory’ - append_to_existing_output (
bool
) – If true and if there is already a coco_annotations.json file in the output directory, the new coco annotations will be appended to the existing file. Also the rgb images will be named such that there are no collisions. - segmap_output_key (
str
) – The output key with which the segmentation images were registered. Should be the same as the output_key of the SegMapRenderer module. Default: segmap. - segcolormap_output_key (
str
) – The output key with which the csv file for object name/class correspondences was registered. Should be the same as the colormap_output_key of the SegMapRenderer module. Default: segcolormap. - rgb_output_key (
str
) – The output key with which the rgb images were registered. Should be the same as the output_key of the RgbRenderer module. Default: colors. - jpg_quality (
int
) – The desired quality level of the jpg encoding - label_mapping (
Optional
[LabelIdMapping
]) – The label mapping which should be used to label the categories based on their ids. If None, is given then the name field in the csv files is used or - if not existing - the category id itself is used. - file_prefix (
str
) – Optional prefix for image file names
- output_dir (