blenderproc.python.writer.CocoWriterUtility module
Allows rendering the content of the scene in the coco file format.
- 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 objecttolerance (
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
]
- 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 annotationimage_id (
int
) – integer to uniquly identify imagecategory_id (
int
) – Id of the categorybinary_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:
Optional
[Dict
[str
,Union
[str
,int
]]]
- static create_image_info(image_id, file_name, image_size)[source]
Creates image info section of coco annotation
- Parameters:
image_id (
int
) – integer to uniquly identify imagefile_name (
str
) – filename for imageimage_size (
Tuple
[int
,int
]) – The size of the image, given as [W, H]
- Return type:
Dict
[str
,Union
[str
,int
]]
- 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.
- 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, instance_attribute_maps, colors, color_file_format='PNG', mask_encoding_format='rle', supercategory='coco_annotations', append_to_existing_output=True, jpg_quality=95, label_mapping=None, file_prefix='', indent=None)[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 annotationsinstance_segmaps (
List
[ndarray
]) – List of instance segmentation mapsinstance_attribute_maps (
List
[dict
]) – per-frame mappings with idx, class and optionally supercategory/bop_dataset_namecolors (
List
[ndarray
]) – List of color images. Does not support stereo images, enter left and right inputs subsequently.color_file_format (
str
) – Format to save color images inmask_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.jpg_quality (
int
) – The desired quality level of the jpg encodinglabel_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 namesindent (
Union
[int
,str
,None
]) – If indent is a non-negative integer or string, then the annotation output will be pretty-printed with that indent level. An indent level of 0, negative, or “” will only insert newlines. None (the default) selects the most compact representation. Using a positive integer indent indents that many spaces per level. If indent is a string (such as “ “), that string is used to indent each level.