Iterate over a local project

In this article, we will learn how to iterate through a project in Supervisely format, which is stored locally on your machine. It is one of the most frequent operations in Superviely Apps and python automation scripts. You will see how easy it is to get all the necessary information from the project, as well as how quickly you can visualize the contents of the project even without internet access.

Everything you need to reproduce this tutorial is on GitHub: source code, Visual Studio code configuration, and a shell script for creating venv.

In this guide we will go through the following steps:

**** Step 1. Get a demo project with labeled lemons and kiwis.

**** Step 2. Download the demo project to your local machine in Supervisely format using Export to Supervisely format app in Supervisely Ecosystem.

**** Step 3. Run python script.

1. Demo project

If you don't have any projects in Supervisely format on your local machine, go to the ecosystem and add the demo project 🍋 Lemons annotated to your current workspace.

2. Download project in Supervisely format

Go to the ecosystem and launch the app Export to Supervisely format. Select the demo project, you created in the previous step (or use any existing project in Supervisely), and download the result archive to your local machine.

Extract the archive to any folder and check that it has the following structure:

. 📦 (project root)
├── 📂 ds1 (dataset name)
│   ├── 📂 ann
│   │   ├── 📜 IMG_0748.jpeg.json
│   │   ├── 📜 IMG_1836.jpeg.json
│   │   ├── 📜 IMG_2084.jpeg.json
│   │   ├── 📜 IMG_3861.jpeg.json
│   │   ├── 📜 IMG_4451.jpeg.json
│   │   └── 📜 IMG_8144.jpeg.json
│   └── 📂 img
│       ├── 🖼️ IMG_0748.jpeg
│       ├── 🖼️ IMG_1836.jpeg
│       ├── 🖼️ IMG_2084.jpeg
│       ├── 🖼️ IMG_3861.jpeg
│       ├── 🖼️ IMG_4451.jpeg
│       └── 🖼️ IMG_8144.jpeg
└── 📜 meta.json

The project root directory contains the meta.json file with the project meta information and directories for each dataset. Each dataset directory contains two subdirectories: img with images and ann with annotations in Supervisely format.

3. Python script

To start debugging you need to:

  1. Clone the repo

  2. Create venv by running the script create_venv.sh

  3. Set the correct path to the downloaded and extracted project on your local machine in the script main.py

Source code:

import os
import json

import supervisely as sly
from tqdm import tqdm

input = "./lemons-fs"
output = "./results"
os.makedirs(output, exist_ok=True)

# Creating Supervisely project from local directory.
project = sly.Project(input, sly.OpenMode.READ)
print("Opened project: ", project.name)
print("Number of images in project:", project.total_items)

# Showing annotations tags and classes.
print(project.meta)

# Iterating over classes in project, showing their names, geometry types and colors.
for obj_class in project.meta.obj_classes:
    print(
        f"Class '{obj_class.name}': geometry='{obj_class.geometry_type}', color='{obj_class.color}'",
    )

# Iterating over tags in project, showing their names and colors.
for tag in project.meta.tag_metas:
    print(f"Tag '{tag.name}': color='{tag.color}'")

print("Number of datasets (aka folders) in project:", len(project.datasets))

progress = tqdm(project.datasets, desc="Processing datasets")
for dataset in project.datasets:
    # Iterating over images in dataset, using the paths to the images and annotations.
    for item_name, image_path, ann_path in dataset.items():
        print(f"Item '{item_name}': image='{image_path}', ann='{ann_path}'")

        ann_json = json.load(open(ann_path))
        ann = sly.Annotation.from_json(ann_json, project.meta)

        img = sly.image.read(image_path)  # rgb - order

        for label in ann.labels:
            # Drawing each label on the image.
            label.draw(img)

        res_image_path = os.path.join(output, item_name)
        sly.image.write(res_image_path, img)

        # Or alternatively draw annotation (all labels at once) preview with
        # ann.draw_pretty(img, output_path=res_image_path)

        progress.update(1)

Output

The script above produces the following output:

Opened project:  lemons-fs
Number of images in project: 6
ProjectMeta:
Object Classes
+-------+--------+----------------+--------+
|  Name | Shape  |     Color      | Hotkey |
+-------+--------+----------------+--------+
|  kiwi | Bitmap |  [255, 0, 0]   |        |
| lemon | Bitmap | [81, 198, 170] |        |
+-------+--------+----------------+--------+
Tags
+----------+------------+-----------------+--------+---------------+--------------------+
|   Name   | Value type | Possible values | Hotkey | Applicable to | Applicable classes |
+----------+------------+-----------------+--------+---------------+--------------------+
+----------+------------+-----------------+--------+---------------+--------------------+

Class 'kiwi': geometry='<class 'supervisely.geometry.bitmap.Bitmap'>', color='[255, 0, 0]'
Class 'lemon': geometry='<class 'supervisely.geometry.bitmap.Bitmap'>', color='[81, 198, 170]'
Number of datasets (aka folders) in project: 1

Item 'IMG_4451.jpeg': image='./lemons-fs/ds1/img/IMG_4451.jpeg', ann='./lemons-fs/ds1/ann/IMG_4451.jpeg.json'
Item 'IMG_0748.jpeg': image='./lemons-fs/ds1/img/IMG_0748.jpeg', ann='./lemons-fs/ds1/ann/IMG_0748.jpeg.json'
Item 'IMG_1836.jpeg': image='./lemons-fs/ds1/img/IMG_1836.jpeg', ann='./lemons-fs/ds1/ann/IMG_1836.jpeg.json'
Item 'IMG_3861.jpeg': image='./lemons-fs/ds1/img/IMG_3861.jpeg', ann='./lemons-fs/ds1/ann/IMG_3861.jpeg.json'
Item 'IMG_2084.jpeg': image='./lemons-fs/ds1/img/IMG_2084.jpeg', ann='./lemons-fs/ds1/ann/IMG_2084.jpeg.json'
Item 'IMG_8144.jpeg': image='./lemons-fs/ds1/img/IMG_8144.jpeg', ann='./lemons-fs/ds1/ann/IMG_8144.jpeg.json'

As a result of running the script there also will be created a directory results, which will contain the images with drawn annotations.

Last updated