In this tutorial, you will learn how to programmatically create classes and labels of different shapes and upload them to Supervisely platform. Supervisely supports different types of shapes / geometries for image annotation:
bounding box (rectangle)
polygon
mask (also known as bitmap)
polyline
point
keypoints (also known as graph, skeleton, landmarks) - will be covered in other tutorials
cuboids - will be covered in other tutorials
Learn more about Supervisely Annotation JSON format here.
Everything you need to reproduce this tutorial is on GitHub: source code, Visual Studio Code configuration, and a shell script for creating virtual env.
How to debug this tutorial
Step 1. Prepare ~/supervisely.env file with credentials. Learn more here.
Step 3. Open repository directory in Visual Studio Code.
code-r.
Step 4. change ✅ workspace ID ✅ in local.env file by copying the ID from the context menu of the workspace. A new project with annotated images will be created in the workspace you define:
WORKSPACE_ID=506# ⬅️ change value
Step 5. Start debugging src/main.py
Python Code
Import libraries
import osimport cv2import supervisely as slyfrom dotenv import load_dotenv
Init API client
Init api for communicating with Supervisely Instance. First, we load environment variables with credentials and workspace ID:
With next lines we will check the you did everything right - API client initialized with correct credentials and you defined the correct workspace ID in local.env.
workspace_id = sly.env.workspace_id()workspace = api.workspace.get_info_by_id(workspace_id)if workspace isNone:print("you should put correct workspaceId value to local.env")raiseValueError(f"Workspace with id={workspace_id} not found")
Create project
Create empty project with name "Demo" with one dataset "berries" in your workspace on server. If the project with the same name exists in your dataset, it will be automatically renamed (Demo_001, Demo_002, etc ...) to avoid name collisions.
project = api.project.create(workspace.id, name="Demo", change_name_if_conflict=True)dataset = api.dataset.create(project.id, name="berries")print(f"Project has been sucessfully created, id={project.id}")
Every blackberry will be labeled with a mask. So we are going to create three masks from the following black and white images:
Supervisely SDK allows creating masks from NumPy arrays with the following values:
0 - nothing, 1 - pixels of target mask
0 - nothing, 255 - pixels of target mask
False - nothing, True - pixels of target mask
Mask has to be the same size as the image
labels_masks = []for mask_path in ["data/masks/Blackberry_01.png","data/masks/Blackberry_02.png","data/masks/Blackberry_03.png",]:# read only first channel of an image image_black_and_white = cv2.imread(mask_path)[:,:,0]# supports masks with values (0, 1) or (0, 255) or (False, True) mask = sly.Bitmap(image_black_and_white) label = sly.Label(geometry=mask, obj_class=blackberry) labels_masks.append(label)
image_path ="data/berries-02.jpg"height, width = cv2.imread(image_path).shape[0:2]# result image annotationann = sly.Annotation(img_size=[height, width], labels=[*labels_points, label_line])# upload image to the dataset on serverimage_name = sly.fs.get_file_name_with_ext(image_path)image_info = api.image.upload_path(dataset.id, image_name, image_path)print(f"Image has been sucessfully uploaded, id={image_info.id}")# upload annotation to the image on serverapi.annotation.upload_ann(image_info.id, ann)print(f"Annotation has been sucessfully uploaded to the image {image_name}")