Connecting to Label Studio API on a different server than the one the service is hosted on

Hi. My label studio service is hosted on server X but I want to export the annotations / images from server Y through the SDK. Is this possible?

Hey @ras, thanks for the question and welcome to the community :wave:

Yes, it is possible to export annotations and images from a Label Studio instance hosted on server Y using the SDK, even if your service is hosted on server X.

  1. Connect to the Label Studio Instance on Server Y
    Use the SDK to connect to the Label Studio instance on server Y by providing the URL and API key.
   from label_studio_sdk import Client

   LABEL_STUDIO_URL = 'http://server-y-url:port'
   API_KEY = 'your_api_key'

   ls = Client(url=LABEL_STUDIO_URL, api_key=API_KEY)
   ls.check_connection()
  1. Export Annotations:
    Use the SDK to export annotations from the project on server Y. You can specify the export format and other parameters as needed.
   project_id = 'your_project_id'
   project = ls.get_project(project_id)

   # Export annotations in JSON format
   export = project.export_tasks(export_type='JSON', download_all_tasks=True)
  1. Download Exported Annotations:
    Download the exported annotations to your local machine or server X.
   export_file_path = 'path_to_save_exported_file.json'
   with open(export_file_path, 'wb') as f:
       f.write(export)
  1. Export Images (if needed):
    If you need to export images along with annotations, you can use the label-studio-converter tool to convert and download images.

label-studio-converter export --project-id your_project_id --export-type YOLO --download-resources --export-location /path/to/export

  1. Handle Cloud Storage (if applicable):
    If your images are stored in cloud storage (e.g., AWS S3, Google Cloud Storage), ensure that the storage is configured correctly in your Label Studio project settings. You can then sync the images using the SDK or API.

Here is an example of how to sync annotations to an S3 bucket using the SDK:

import requests

LABEL_STUDIO_URL = 'http://server-y-url:port'
API_KEY = 'your_api_key'
PROJECT_ID = 'your_project_id'
TARGET_STORAGE_ID = 'your_target_storage_id'

SYNC_STORAGE_URL = f"{LABEL_STUDIO_URL}/api/storages/export/s3/{TARGET_STORAGE_ID}/sync"
response = requests.post(SYNC_STORAGE_URL, headers={'Authorization': f'Token {API_KEY}'})

if response.status_code == 200:
    print("Sync initiated successfully.")
else:
    print(f"Failed to initiate sync. Status code: {response.status_code}, Response: {response.text}")

Let me know if this is helpful!

1 Like

Thank you for the helpful information! I will update on whether this works :slight_smile: