Supervisely
About SuperviselyEcosystemContact usSlack
  • 💻Supervisely Developer Portal
  • 🎉Getting Started
    • Installation
    • Basics of authentication
    • Intro to Python SDK
    • Environment variables
    • Supervisely annotation format
      • Project Structure
      • Project Meta: Classes, Tags, Settings
      • Objects
      • Tags
      • Image Annotation
      • Video Annotation
      • Point Clouds Annotation
      • Point Cloud Episode Annotation
      • Volumes Annotation
    • Python SDK tutorials
      • Images
        • Images
        • Image and object tags
        • Spatial labels on images
        • Keypoints (skeletons)
        • Multispectral images
        • Multiview images
        • Advanced: Optimized Import
        • Advanced: Export
      • Videos
        • Videos
        • Video and object tags
        • Spatial labels on videos
      • Point Clouds
        • Point Clouds (LiDAR)
        • Point Cloud Episodes and object tags
        • 3D point cloud object segmentation based on sensor fusion and 2D mask guidance
        • 3D segmentation masks projection on 2D photo context image
      • Volumes
        • Volumes (DICOM)
        • Spatial labels on volumes
      • Common
        • Iterate over a project
        • Iterate over a local project
        • Progress Bar tqdm
        • Cloning projects for development
    • Command Line Interface (CLI)
      • Enterprise CLI Tool
        • Instance administration
        • Workflow automation
      • Supervisely SDK CLI
    • Connect your computer
      • Linux
      • Windows WSL
      • Troubleshooting
  • 🔥App development
    • Basics
      • Create app from any py-script
      • Configuration file
        • config.json
        • Example 1. Headless
        • Example 2. App with GUI
        • v1 - Legacy
          • Example 1. v1 Modal Window
          • Example 2. v1 app with GUI
      • Add private app
      • Add public app
      • App Compatibility
    • Apps with GUI
      • Hello World!
      • App in the Image Labeling Tool
      • App in the Video Labeling Tool
      • In-browser app in the Labeling Tool
    • Custom import app
      • Overview
      • From template - simple
      • From scratch - simple
      • From scratch GUI - advanced
      • Finding directories with specific markers
    • Custom export app
      • Overview
      • From template - simple
      • From scratch - advanced
    • Neural Network integration
      • Overview
      • Serving App
        • Introduction
        • Instance segmentation
        • Object detection
        • Semantic segmentation
        • Pose estimation
        • Point tracking
        • Object tracking
        • Mask tracking
        • Image matting
        • How to customize model inference
        • Example: Custom model inference with probability maps
      • Serving App with GUI
        • Introduction
        • How to use default GUI template
        • Default GUI template customization
        • How to create custom user interface
      • Inference API
      • Training App
        • Overview
        • Tensorboard template
        • Object detection
      • High level scheme
      • Custom inference pipeline
      • Train and predict automation model pipeline
    • Advanced
      • Advanced debugging
      • How to make your own widget
      • Tutorial - App Engine v1
        • Chapter 1 Headless
          • Part 1 — Hello world! [From your Python script to Supervisely APP]
          • Part 2 — Errors handling [Catching all bugs]
          • Part 3 — Site Packages [Customize your app]
          • Part 4 — SDK Preview [Lemons counter app]
          • Part 5 — Integrate custom tracker into Videos Annotator tool [OpenCV Tracker]
        • Chapter 2 Modal Window
          • Part 1 — Modal window [What is it?]
          • Part 2 — States and Widgets [Customize modal window]
        • Chapter 3 UI
          • Part 1 — While True Script [It's all what you need]
          • Part 2 — UI Rendering [Simplest UI Application]
          • Part 3 — APP Handlers [Handle Events and Errors]
          • Part 4 — State and Data [Mutable Fields]
          • Part 5 — Styling your app [Customizing the UI]
        • Chapter 4 Additionals
          • Part 1 — Remote Developing with PyCharm [Docker SSH Server]
      • Custom Configuration
        • Fixing SSL Certificate Errors in Supervisely
        • Fixing 400 HTTP errors when using HTTP instead of HTTPS
      • Autostart
      • Coordinate System
      • MLOps Workflow integration
    • Widgets
      • Input
        • Input
        • InputNumber
        • InputTag
        • BindedInputNumber
        • DatePicker
        • DateTimePicker
        • ColorPicker
        • TimePicker
        • ClassesMapping
        • ClassesColorMapping
      • Controls
        • Button
        • Checkbox
        • RadioGroup
        • Switch
        • Slider
        • TrainValSplits
        • FileStorageUpload
        • Timeline
        • Pagination
      • Text Elements
        • Text
        • TextArea
        • Editor
        • Copy to Clipboard
        • Markdown
        • Tooltip
        • ElementTag
        • ElementTagsList
      • Media
        • Image
        • LabeledImage
        • GridGallery
        • Video
        • VideoPlayer
        • ImagePairSequence
        • Icons
        • ObjectClassView
        • ObjectClassesList
        • ImageSlider
        • Carousel
        • TagMetaView
        • TagMetasList
        • ImageAnnotationPreview
        • ClassesMappingPreview
        • ClassesListPreview
        • TagsListPreview
        • MembersListPreview
      • Selection
        • Select
        • SelectTeam
        • SelectWorkspace
        • SelectProject
        • SelectDataset
        • SelectItem
        • SelectTagMeta
        • SelectAppSession
        • SelectString
        • Transfer
        • DestinationProject
        • TeamFilesSelector
        • FileViewer
        • Dropdown
        • Cascader
        • ClassesListSelector
        • TagsListSelector
        • MembersListSelector
        • TreeSelect
        • SelectCudaDevice
      • Thumbnails
        • ProjectThumbnail
        • DatasetThumbnail
        • VideoThumbnail
        • FolderThumbnail
        • FileThumbnail
      • Status Elements
        • Progress
        • NotificationBox
        • DoneLabel
        • DialogMessage
        • TaskLogs
        • Badge
        • ModelInfo
        • Rate
        • CircleProgress
      • Layouts and Containers
        • Card
        • Container
        • Empty
        • Field
        • Flexbox
        • Grid
        • Menu
        • OneOf
        • Sidebar
        • Stepper
        • RadioTabs
        • Tabs
        • TabsDynamic
        • ReloadableArea
        • Collapse
        • Dialog
        • IFrame
      • Tables
        • Table
        • ClassicTable
        • RadioTable
        • ClassesTable
        • RandomSplitsTable
        • FastTable
      • Charts and Plots
        • LineChart
        • GridChart
        • HeatmapChart
        • ApexChart
        • ConfusionMatrix
        • LinePlot
        • GridPlot
        • ScatterChart
        • TreemapChart
        • PieChart
      • Compare Data
        • MatchDatasets
        • MatchTagMetas
        • MatchObjClasses
        • ClassBalance
        • CompareAnnotations
      • Widgets demos on github
  • 😎Advanced user guide
    • Objects binding
    • Automate with Python SDK & API
      • Start and stop app
      • User management
      • Labeling Jobs
  • 🖥️UI widgets
    • Element UI library
    • Supervisely UI widgets
    • Apexcharts - modern & interactive charts
    • Plotly graphing library
  • 📚API References
    • REST API Reference
    • Python SDK Reference
Powered by GitBook
On this page
  • Introduction
  • How to debug this tutorial
  • Import libraries
  • Init API client
  • Get variables from environment
  • Create new project and dataset
  • Upload images from local directory to Supervisely
  • Upload single image.
  • Upload list of images.
  • Upload images as NumPy matrix
  • Single image
  • Upload list of images
  • Get information about images
  • Single image
  • Get all images from dataset.
  • Download images to local directory
  • Single image
  • Download list of images to local directory
  • Download images as RGB NumPy matrix
  • Single image
  • Download list of images as RGB NumPy matrix
  • Get and update image metadata
  • Get image metadata from server
  • Update image metadata
  • Get metadata in Image labeling toolbox
  • Remove images from Supervisely
  • Remove one image.
  • Remove list of images.
  • Custom image sorting for Image Labeling Toolbox
  • Upload list of images with added custom sorting parameter
  • Upload whole images project in Supervisely format with added custom sorting parameter
  • Add custom sorting parameter to meta object

Was this helpful?

Edit on GitHub
  1. Getting Started
  2. Python SDK tutorials
  3. Images

Images

PreviousImagesNextImage and object tags

Last updated 3 months ago

Was this helpful?

Introduction

In this tutorial we will focus on working with images using Supervisely SDK.

You will learn how to:

📗 Everything you need to reproduce : source code and demo data.

How to debug this tutorial

Step 1. Prepare ~/supervisely.env file with credentials.

Step 2. Clone with source code and demo data and create .

git clone https://github.com/supervisely-ecosystem/tutorial-image.git

cd tutorial-image

./create_venv.sh

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.

WORKSPACE_ID=654 # ⬅️ change value

Step 5. Start debugging src/main.py.

Import libraries

import os
from dotenv import load_dotenv
import supervisely as sly

Init API client

First, we load environment variables with credentials and init API for communicating with Supervisely Instance.

if sly.is_development():
    load_dotenv("local.env")
    load_dotenv(os.path.expanduser("~/supervisely.env"))
api = sly.Api()

Get variables from environment

workspace_id = sly.env.workspace_id()

Create new project and dataset

Create new project.

Source code:

project = api.project.create(workspace_id, "Fruits", change_name_if_conflict=True)

print(f"Project ID: {project.id}")

Output:

# Project ID: 15599

Create new dataset.

Source code:

dataset = api.dataset.create(project.id, "Fruits ds1")

print(f"Dataset ID: {dataset.id}")

Output:

# Dataset ID: 53465

Upload images from local directory to Supervisely

Upload single image.

Source code:

original_dir = "src/images/original"
path = os.path.join(original_dir, "lemons.jpg")
meta = {"my-field-1": "my-value-1", "my-field-2": "my-value-2"}

image = api.image.upload_path(
    dataset.id,
    name="Lemons",
    path=path,
    meta=meta # optional
)

print(f'Image "{image.name}" uploaded to Supervisely with ID:{image.id}')

Output:

# Image "Lemons.jpeg" uploaded to Supervisely platform with ID:17539453

Upload list of images.

✅ Supervisely API allows uploading multiple images in a single request. The code sample below sends fewer requests and it leads to a significant speed-up of our original code.

Source code:

names = [
    "grapes-1.jpg",
    "grapes-2.jpg",
    "oranges-2.jpg",
    "oranges-1.jpg",
]
paths = [os.path.join(original_dir, name) for name in names]

upload_info = api.image.upload_paths(dataset.id, names, paths)

print(f"{len(upload_info)} images successfully uploaded to Supervisely platform")

Output:

# 4 images successfully uploaded to Supervisely platform

Upload images as NumPy matrix

Single image

Source code:

img_np = sly.image.read(path)

np_image_info = api.image.upload_np(dataset.id, name="Lemons-np.jpeg", img=img_np)

print(f"Image successfully uploaded as NumPy matrix to Supervisely (ID: {np_image_info.id})")

Output:

# Image successfully uploaded as NumPy matrix to Supervisely (ID: 17539458)

Upload list of images

Source code:

names_np = [f"np-{name}" for name in names]
images_np = [sly.image.read(img_path) for img_path in paths]

np_images_info = api.image.upload_nps(dataset.id, names_np, images_np)

print(f"{len(images_np)} images successfully uploaded to platform as NumPy matrix")

Output:

# 4 images successfully uploaded to platform as NumPy matrix

Get information about images

Single image

Get information about image from Supervisely by id.

Source code:

image_info = api.image.get_info_by_id(image.id)

print(image_info)

Output:

# ImageInfo(
#     id=17539453,
#     name='Lemons.jpeg',
#     link=None,
#     hash='0jirgXvKGTJ8Yi0I9nCdf9MllQ9jP3Les1fD7/dt+Zk=',
#     mime='image/jpeg',
#     ext='jpeg',
#     size=66066,
#     width=640,
#     height=427,
#     labels_count=0,
#     dataset_id=54008,
#     created_at='2022-12-23T15:57:35.707Z',
#     updated_at='2022-12-23T15:57:35.707Z',
#     meta={},
#     path_original='nAXBxaxQJRARr0Ljkj6FfREj1Fq89.jpg',
#     full_storage_url='https://dev.supervisely.com/h5unublic/images/original/3/e/OK/d8Y7NnEj1Fq89.jpg',
#     tags=[]
# )

You can also get information about image from Supervisely by name.

Source code:

image_name = get_file_name(image.name)

image_info_by_name = api.image.get_info_by_name(dataset.id, image_name)

print(f"image name - {image_info_by_name.name}")

Output:

# image name - Lemons.jpeg

Get all images from dataset.

Get information about image from Supervisely by id.

Source code:

image_info_list = api.image.get_list(dataset.id)

print(f"{len(image_info_list)} images information received.")

Output:

# 10 images information received.

Download images to local directory

Single image

Download image from Supervisely to local directory by id.

Source code:

save_path = os.path.join(result_dir, image_info.name)

api.image.download_path(image_info.id, save_path)

print(f"Image has been successfully downloaded to '{save_path}'")

Output:

# Image has been successfully downloaded to 'src/images/result/Lemons.jpeg'

Download list of images to local directory

Download list of images from Supervisely to local directory by ids.

Source code:

image_ids = [img.id for img in image_info_list]
image_names = [img.name for img in image_info_list]
save_paths = [os.path.join(result_dir, img_name) for img_name in image_names]

api.image.download_paths(dataset.id, image_ids, save_paths)

print(f"{len(image_info_list)} images has been successfully downloaded.")

Output:

# 10 images has been successfully downloaded.

Download images as RGB NumPy matrix

Single image

Download image from Supervisely to local directory by id.

Source code:

image_np = api.image.download_np(image_info.id)

print(f"Image downloaded as RGB NumPy matrix. Image shape: {image_np.shape}")

Output:

# Image downloaded as RGB NumPy matrix. Image shape: (427, 640, 3)

Download list of images as RGB NumPy matrix

Download list of images from Supervisely to local directory by ids.

Source code:

image_np = api.image.download_nps(dataset.id, image_ids)

print(f"{len(image_np)} images downloaded in RGB NumPy matrix.")

Output:

# 10 images downloaded in RGB NumPy matrix.

Get and update image metadata

Get image metadata from server

Source code:

image_info = api.image.get_info_by_id(image.id)
meta = image_info.meta

print(meta)

Output:

# {'my-field-1': 'my-value-1', 'my-field-2': 'my-value-2'}

Update image metadata

Source code:

new_meta = {"my-field-3": "my-value-3", "my-field-4": "my-value-4"}

new_image_info = api.image.update_meta(id=image.id, meta=new_meta)

print(new_image_info["meta"])

Output:

# {'my-field-3': 'my-value-3', 'my-field-4': 'my-value-4'}

Get metadata in Image labeling toolbox

You can also get image metadata in Image labeling toolbox interface

Remove images from Supervisely

Remove one image.

Remove image from Supervisely by id

Source code:

api.image.remove(image.id)

print(f"Image (ID: {image.id}) successfully removed")

Output:

# Image (ID: 17539453) successfully removed

Remove list of images.

Remove list of images from Supervisely by ids.

Source code:

images_to_remove = api.image.get_list(dataset.id)
remove_ids = [img.id for img in images_to_remove]

api.image.remove_batch(remove_ids)

print(f"{len(remove_ids)} images successfully removed.")

Output:

# 9 images successfully removed.

Custom image sorting for Image Labeling Toolbox

To enhance the usability of working with images in the Image Labeling Toolbox, a custom sorting parameter can be added for project images. This parameter will define the order of images in the interface list.

  1. Sort button

  2. Sorting parameter

Upload list of images with added custom sorting parameter

The best and fastest way to accomplish this is to use context manager ImageApi.add_custom_sort This context manager allows you to set the sort_by attribute of ImageApi object for the duration of the context, then delete it. If nested functions support this functionality, each image they process will automatically receive a custom sorting parameter based on the available meta object. Currently, almost all image uploading methods support this functionality. Methods that support it have a corresponding description in the docstring.

Source code:

original_dir = "src/images/original"
names = ["Oranges 1", "Oranges 2"]
paths = [os.path.join(original_dir, "oranges-1.jpg"), os.path.join(original_dir, "oranges-2.jpg")]
metas = [{"key-1": "a", "my-key": "b"}, {"key-1": "c", "my-key": "f"}]

with api.image.add_custom_sort(key="my-key"):
    image_infos = api.image.upload_paths(
        dataset.id,
        names=names,
        paths=paths,
        metas=metas
    )
for i in image_infos:
    print(f"{i.name}: {i.meta}")

Output:

# Oranges 1.jpeg: {'key-1': 'a', 'my-key': 'b', 'customSort': 'b'}
# Oranges 2.jpeg: {'key-1': 'c', 'my-key': 'f', 'customSort': 'f'}

Upload whole images project in Supervisely format with added custom sorting parameter

Source code:

from supervisely.project.upload import upload

project_dir = "src/images_project"
project_name = "Project with Sorting"

with api.image.add_custom_sort(key="my-key"):
    upload(project_dir, api, workspace_id, project_name)

project_info = api.project.get_info_by_name(workspace_id, project_name)
dataset_info = api.dataset.get_list(project_info.id)[0]
images_infos = api.image.get_list(dataset_info.id)
for i in images_infos:
    print(f"{i.name}: {i.meta}")

Output:

# oranges-2.jpg: {'my-key': '5', 'customSort': '5'}
# oranges-1.jpg: {'my-key': '4', 'customSort': '4'}
# grapes-2.jpg: {'my-key': '1', 'customSort': '1'}
# lemons.jpg: {'my-key': '5', 'customSort': '5'}
# grapes-1.jpg: {'my-key': '2', 'customSort': '2'}

Add custom sorting parameter to meta object

Here are several ways to modify meta for images

1. Add parameter to meta dict and update meta on server

Source code:

meta = {"key-1": "a", "my-key": "b"}
new_meta = api.image.update_custom_sort(meta, "sort-value")
new_image_info = api.image.update_meta(id=images_infos[0].id, meta=new_meta)

print(new_image_info["meta"])

Output:

# {'key-1': 'a', 'my-key': 'b', 'customSort': 'sort-value'}

2. Set directly on server

Source code:

api.image.set_custom_sort(new_image_info["id"], "new-sort-value")
updated_image_info = api.image.get_info_by_id(new_image_info["id"])

print(updated_image_info.meta)

Output:

# {'key-1': 'a', 'my-key': 'b', 'customSort': 'new-sort-value'}

3. Set directly on server in bulk

Same as the previous case, but for more than one image

Source code:

image_ids = [image.id for image in images_infos]
sort_values = ["1st", "2nd", "3rd", "4th", "5th"]
api.image.set_custom_sort_bulk(image_ids, sort_values)
images_infos = api.image.get_list(dataset_info.id)
for i in images_infos:
    print(f"{i.name}: {i.meta}")

Output:

# oranges-2.jpg: {'key-1': 'a', 'my-key': 'b', 'customSort': '1st'}
# oranges-1.jpg: {'my-key': '4', 'customSort': '2nd'}
# grapes-2.jpg: {'my-key': '1', 'customSort': '3rd'}
# lemons.jpg: {'my-key': '5', 'customSort': '4th'}
# grapes-1.jpg: {'my-key': '2', 'customSort': '5th'}

In this tutorial, you will need an workspace ID that you can get from environment variables.

It is also recommended to use a context manager for uploading the entire project. The only difference from the previous point is that there is no need to pass meta in dictionaries. It can be stored either in image info files or in meta files within the project structure. To learn more about the project structure and its files, see the section.

🎉
Project Structure
this tutorial is on GitHub
Learn more here.
repository
Virtual Environment
upload images from local directory to Supervisely dataset.
upload images to Supervisely as NumPy matrix.
get information about images by id or name.
download images from Supervisely to local directory.
download images from Supervisely as NumPy matrix.
get and update image metadata
remove images from Supervisely.
custom image sorting for Image Labeling Toolbox
Learn more here