Notebooks

Split project by tags

iPython Project Created 37 minutes ago Free
Notebook creates new project only with images that contain given tags
Free Signup

Filter project by tags

Notebook creates new project with images that contain given tags

Input:

  • Source Project
  • set of tags

Output:

  • New Project with images that contain given tags

Configuration

Edit the following settings for your own case

In [1]:
import supervisely_lib as sly
import os
from tqdm import tqdm
In [2]:
team_name = "jupyter_tutorials"
workspace_name = "cookbook"
project_name = "lemons_annotated_01"

dst_project_name = "lemons_annotated_01_filtered"

filter_tag_names = {'train', 't1', 't2'}

# Obtain server address and your api_token from environment variables
# Edit those values if you run this notebook on your own PC
address = os.environ['SERVER_ADDRESS']
token = os.environ['API_TOKEN']
In [3]:
# Initialize API object
api = sly.Api(address, token)

Verify input values

Test that context (team / workspace / project) exists

In [4]:
# Get IDs of team, workspace and project by names

team = api.team.get_info_by_name(team_name)
if team is None:
    raise RuntimeError("Team {!r} not found".format(team_name))

workspace = api.workspace.get_info_by_name(team.id, workspace_name)
if workspace is None:
    raise RuntimeError("Workspace {!r} not found".format(workspace_name))
    
project = api.project.get_info_by_name(workspace.id, project_name)
if project is None:
    raise RuntimeError("Project {!r} not found".format(project_name))
    
print("Team: id={}, name={}".format(team.id, team.name))
print("Workspace: id={}, name={}".format(workspace.id, workspace.name))
print("Project: id={}, name={}".format(project.id, project.name))
Out [4]:
Team: id=30, name=jupyter_tutorials
Workspace: id=76, name=cookbook
Project: id=1130, name=lemons_annotated_01

Get Source ProjectMeta

In [5]:
project = api.project.get_info_by_name(workspace.id, project_name)
meta_json = api.project.get_meta(project.id)
meta = sly.ProjectMeta.from_json(meta_json)
print("Source ProjectMeta: \n", meta)
Out [5]:
Source ProjectMeta: 
 ProjectMeta:
Object Classes
+-------+-----------+-----------------+
|  Name |   Shape   |      Color      |
+-------+-----------+-----------------+
|   bg  | Rectangle | [153, 245, 169] |
|  kiwi |   Bitmap  |   [255, 0, 0]   |
| lemon |   Bitmap  |  [81, 198, 170] |
+-------+-----------+-----------------+
Image Tags
+-------+------------+-----------------+
|  Name | Value type | Possible values |
+-------+------------+-----------------+
|  val  |    none    |       None      |
| train |    none    |       None      |
+-------+------------+-----------------+
Object Tags
+------+------------+-----------------+
| Name | Value type | Possible values |
+------+------------+-----------------+
+------+------------+-----------------+

Construct Destination ProjectMeta

In [6]:
dst_meta = meta
print("Destination ProjectMeta:\n", dst_meta)
Out [6]:
Destination ProjectMeta:
 ProjectMeta:
Object Classes
+-------+-----------+-----------------+
|  Name |   Shape   |      Color      |
+-------+-----------+-----------------+
|   bg  | Rectangle | [153, 245, 169] |
|  kiwi |   Bitmap  |   [255, 0, 0]   |
| lemon |   Bitmap  |  [81, 198, 170] |
+-------+-----------+-----------------+
Image Tags
+-------+------------+-----------------+
|  Name | Value type | Possible values |
+-------+------------+-----------------+
|  val  |    none    |       None      |
| train |    none    |       None      |
+-------+------------+-----------------+
Object Tags
+------+------------+-----------------+
| Name | Value type | Possible values |
+------+------------+-----------------+
+------+------------+-----------------+

Create Destination project

In [7]:
# check if destination project already exists. If yes - generate new free name
if api.project.exists(workspace.id, dst_project_name):
    dst_project_name = api.project.get_free_name(workspace.id, dst_project_name)
print("Destination project name: ", dst_project_name)
Out [7]:
Destination project name:  lemons_annotated_01_filtered_003
In [8]:
dst_project = api.project.create(workspace.id, dst_project_name)
api.project.update_meta(dst_project.id, dst_meta.to_json())
print("Destination project has been created: id={}, name={!r}".format(dst_project.id, dst_project.name))
Out [8]:
Destination project has been created: id=1136, name='lemons_annotated_01_filtered_003'

Iterate over all images, filter them by tags and add to destination project

In [9]:
cnt_filtered_images = 0
for dataset in api.dataset.get_list(project.id):
    print('Dataset: {}'.format(dataset.name))
    dst_dataset = api.dataset.create(dst_project.id, dataset.name)

    for image in tqdm(api.image.get_list(dataset.id)):
        ann_json = api.annotation.download(image.id).annotation
        ann = sly.Annotation.from_json(ann_json, meta)
        
        filter_image = False
        for tag_name in filter_tag_names:
            if ann.img_tags.has_key(tag_name):
                filter_image = True
                cnt_filtered_images += 1
                break
        
        if filter_image == False:
            dst_image = api.image.add(dst_dataset.id, image.name, image.hash)
            api.annotation.upload(dst_image.id, ann.to_json())
Out [9]:
  0%|          | 0/72 [00:00<?, ?it/s]
Dataset: ds1
100%|██████████| 72/72 [00:02<00:00, 29.48it/s]
In [10]:
print("Project {!r} has been sucessfully uploaded".format(dst_project.name))
print("Project contains {} images".format(api.project.get_images_count(dst_project.id)))
print("Number of filtered images:", cnt_filtered_images)
Out [10]:
Project 'lemons_annotated_01_filtered_003' has been sucessfully uploaded
Project contains 9 images
Number of filtered images: 63

More Info

ID
89
First released
37 minutes ago
Last updated
3 hours ago

Owner

s