HumanSignal / label-studio-sdk

Label Studio SDK
https://api.labelstud.io
Apache License 2.0
89 stars 58 forks source link

How can I use export_snapshot_create & export_snapshot_download to download a zip including images? #128

Open Yobol opened 1 year ago

Yobol commented 1 year ago

Hi! I encountered an issue when I use label studio SDK for Python. I use export_snapshot_create & export_snapshot_download to download project task results in type of YOLO as a zipfile, but no image files in it, my code as following:

# Define the URL where Label Studio is accessible and the API key for your user account
LABEL_STUDIO_URL = 'http://172.16.250.101:31010' # 'http://mlops-ls-app:80'
API_KEY = '3f968a3eefd3d17aa1f4fb70dc13e677995f88f8'

# Import the SDK and the client module
from label_studio_sdk import Client

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

PROJECT_ID = 1
'''
https://github.com/heartexlabs/label-studio-converter/blob/master/label_studio_converter/converter.py#L44

SUPPORTED FORMATS:
    JSON = 1
    JSON_MIN = 2
    CSV = 3
    TSV = 4
    CONLL2003 = 5
    COCO = 6
    VOC = 7
    BRUSH_TO_NUMPY = 8
    BRUSH_TO_PNG = 9
    ASR_MANIFEST = 10
    YOLO = 11
    CSV_OLD = 12
'''
EXPORT_TYPE = 'YOLO'

project = ls.get_project(PROJECT_ID)

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
    }
)
print('create snapshot for project {}: {}'.format(PROJECT_ID, result['status']))

result = project.export_snapshot_download(
    export_id=result['id'],
    export_type=EXPORT_TYPE,
    path='.'
)
status, zip_file_path = result
print('download snapshot for project {}: {}'.format(PROJECT_ID, status))

I successfully downloaded a YOLO annotation result zipfile, but it has no any files in image folder. What parameters can I set to do this?

Parxd commented 1 year ago

Using project.export_snapshot_create() gives me RecursionError: maximum recursion depth exceeded. Is anyone else facing this issue?

Digital2Slave commented 8 months ago

same issue. How to fix it ?

Digital2Slave commented 8 months ago
  1. export JSON file with the following function.
    
    import time
    from label_studio_sdk import Client

https://labelstud.io/guide/export

https://github.com/HumanSignal/label-studio-sdk/blob/master/examples/export_snapshots.py

def ExportSnapshot(LABEL_STUDIO_URL, API_KEY, PROJECT_ID, SAVE_PATH):

connect to Label Studio

ls = Client(url=LABEL_STUDIO_URL, api_key=API_KEY)
ls.check_connection()

# get existing project
project = ls.get_project(PROJECT_ID)

# get the first tab
views = project.get_views()
task_filter_options = {'view': views[0]['id']} if views else {}

# create new export snapshot
export_result = project.export_snapshot_create(
    title='Export SDK Snapshot', task_filter_options=task_filter_options
)
# assert 'id' in export_result
export_id = export_result['id']

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

# download snapshot file
status, file_name = project.export_snapshot_download(export_id, export_type='JSON', path=SAVE_PATH)
assert status == 200
assert file_name is not None
print(f"Status of the export is {status}.\nFile name is {file_name}")

2. set `LS_UPLOAD_DIR` in `.zshrc` or `.bashrc`  and  `source ~/.zshrc`  or `source ~/.bashrc` 

label studio

export LS_UPLOAD_DIR=/home/epbox/AI/data/media/upload


3. save **.xml** file of the export project

![image](https://github.com/HumanSignal/label-studio/assets/7224107/307b2e79-0c8b-47f6-ade2-b4c6ef4bb790)

4. use `label-studio-converter` to convert JSON  to YOLO format

`pip install label-studio-converter`

label-studio-converter export -i .json --config .xml -o "train" -f YOLO


5. check the convert YOLO  **train** folder

(label) ➜ train -h --filelimit=10 --dirsfirst train ├── [ 20K] images [208 entries exceeds filelimit, not opening dir] ├── [ 20K] labels [208 entries exceeds filelimit, not opening dir] ├── [ 124] classes.txt └── [ 840] notes.json

2 directories, 2 files