Exporting images and annotations using SDK

Hi!

I am trying to export my annotations through the SDK in the YOLO format. However, It does not export the images. I want the SDK to export images as well as the annotations. Can anyone help me?

Hey @ras, thanks for the question!

To export annotations along with images in YOLO format using the Label Studio SDK, you can follow these steps:

  1. Ensure Images are Accessible: Make sure that the images are accessible from the location where the SDK is running. If the images are stored in cloud storage, ensure that the SDK has access to download them.
  2. Use the export_snapshot_create and export_snapshot_download Methods: These methods allow you to create an export snapshot and download it. However, by default, the images might not be included. You need to ensure that the download_resources parameter is set to True.

Here is an rough script to achieve this:

import os
import time
from label_studio_sdk import Client

# Define the URL where Label Studio is accessible and the API key for your user account
LABEL_STUDIO_URL = 'http://your-label-studio-url'
API_KEY = 'your_api_key'

# Connect to the Label Studio API
ls = Client(url=LABEL_STUDIO_URL, api_key=API_KEY)
ls.check_connection()

PROJECT_ID = 1
EXPORT_TYPE = 'YOLO'

# Get the project
project = ls.get_project(PROJECT_ID)

# Create an export snapshot
export_result = project.export_snapshot_create(
    title='export-test-01',
    task_filter_options={
        'view': 1,
        'finished': 'only',  # include all finished tasks (is_labeled = true)
        'annotated': 'only',  # include all tasks with at least one not skipped annotation
    }
)
export_id = export_result['id']

# Wait until the snapshot is ready
while project.export_snapshot_status(export_id).is_in_progress():
    time.sleep(1.0)

# Download the snapshot
status, zip_file_path = project.export_snapshot_download(
    export_id=export_id,
    export_type=EXPORT_TYPE,
    path='.',
    download_resources=True  # Ensure resources (images) are downloaded
)

print(f'Status of the export is {status}.\nFile path is {zip_file_path}')

Important Notes:

  • Environment Variables: Ensure that the environment variable CONVERTER_DOWNLOAD_RESOURCES is set to 1 if you are running Label Studio in an environment where this is required.

  • Local Storage: If you are using local storage, ensure that the LS_UPLOAD_DIR environment variable is set correctly to point to the directory where images are stored.

Troubleshooting:

  • Empty Image Folder: If the image folder in the exported zip is empty, double-check the paths and permissions to ensure that the SDK can access and download the images.

  • Timeouts: For large datasets, exporting might take a long time. Ensure that your server settings allow for sufficient timeout periods.

If you encounter any issues or need further assistance, feel free to ask!

Hi!!

Thank you for your reply. However, it does not seem to be working for me. It throws an error saying:

TypeError: Project.export_snapshot_download() got an unexpected keyword argument ‘download_resources’

Edit:

The download_resources parameter is available only for the export_tasks function and not for the export_snapshot_download function.

Hi, I tried your code and got this error

SDK error traceback:
Traceback (most recent call last):
  File "/home/ubuntu/devel/export_label.py", line 23, in <module>
    export_result = project.export_snapshot_create(
  File "/home/ubuntu/.virtualenvs/lab/lib/python3.9/site-packages/label_studio_sdk/_legacy/project.py", line 2325, in export_snapshot_create
    response = self.make_request(
  File "/home/ubuntu/.virtualenvs/lab/lib/python3.9/site-packages/label_studio_sdk/_legacy/client.py", line 448, in make_request
    response.raise_for_status()
  File "/home/ubuntu/.virtualenvs/lab/lib/python3.9/site-packages/requests/models.py", line 1024, in raise_for_status
    raise HTTPError(http_error_msg, response=self)
requests.exceptions.HTTPError: 502 Server Error: Bad Gateway for url: https:/...

It works with postman. Any idea what is wrong?