A python package bpycv
can render annotations for instance segmentation and panoptic segmentation.
First, follow the instruction to install bpycv
in Blender.
Here is a simple code to show how to get segmentation annotations. Copy-paste this code to Text Editor
and click Run Script
button(or Alt+P
)
import cv2
import bpy
import bpycv
import random
# remove all MESH objects
[bpy.data.objects.remove(obj) for obj in bpy.data.objects if obj.type == "MESH"]
for inst_id in range(1, 20):
# create cube and sphere as instance at random location
location = [random.random() * 4 - 2 for _ in range(3)]
if inst_id % 2:
bpy.ops.mesh.primitive_cube_add(size=0.5, location=location)
else:
bpy.ops.mesh.primitive_uv_sphere_add(radius=0.5, location=location)
obj = bpy.context.active_object
# set each instance a unique inst_id, which is used to generate instance annotation.
obj["inst_id"] = inst_id
# render image, instance annoatation and depth in one line code
result = bpycv.render_data()
# save result
cv2.imwrite("demo-rgb.jpg", result["image"])
cv2.imwrite("demo-inst.png", result["inst"])
# normalizing depth
cv2.imwrite("demo-depth.png", result["depth"] / result["depth"].max() * 255)
# visualization inst|rgb|depth for human
cv2.imwrite("demo-vis(inst|rgb|depth).jpg", result.vis())
The visualization result of instance segmentation, RGB image and depth
More detial: bpycv