Search code examples
pythonrestcvat

CVAT REST API to upload files


I am able to create a Task in an existing project in CVAT using the below code, but I am unable to upload files, even if I try to reference this link here: https://github.com/opencv/cvat/issues/4704

Any advice would be greatly appreciated please!

import requests

# to create project (this works)
link = 'http://xx.x.xxx.xx:8080/api/tasks'
d = {
"name": "ABC",
"project_id": 3
}

Header = {
"X-Organization":"ABC_labelling"
}

r = requests.post(link, data=d, headers=Header, auth=('username','pw'))
r.json
# to upload file (doesn't work)
task_id = 8
link = 'http://xx.x.xxx.xx:8080/api/tasks/{task_id}/data/'
files = [
'/Users/username/Documents/images/abc001.tif', 
'/Users/username/Documents/images/abc002.tif'
]

Header = {
"X-Organization":"ABC_labelling",
"Upload-Start":"true",
"Upload-Finish":"true"
}

d = {
"image-quality": 70,
"server_files": files
}

r = requests.post(link, data=d, headers=Header, auth=('username','pw'))


Solution

  • While it's sometimes a bit buggy for me. Voxel51 is by far the easiest way to achieve this.

    # A unique identifier for this run
    import fiftyone as fo 
    
    # Just the CVAT password and username
    assert os.environ['FIFTYONE_CVAT_URL'], 'Set FIFTYONE_CVAT_URL env var'
    assert os.environ['FIFTYONE_CVAT_PASSWORD'], 'Set FIFTYONE_CVAT_PASSWORD env var'
    
    dataset = fo.Dataset.from_dir(
        dataset_dir=<coco-dir-path>,
        dataset_type=fo.types.COCODetectionDataset,
    )
    dataset.name = "example_task_name"
    
    anno_key = 'something_voxel51_uses'
    
    dataset.annotate(
        anno_key,
        label_field="ground_truth",
        attributes=["iscrowd"],
        launch_editor=True,
        classes=['car','truck','person'],
    )
    

    https://docs.voxel51.com/api/fiftyone.core.collections.html#fiftyone.core.collections.SampleCollection.annotate

    I may be missing some detail but the CVAT Api is a pain to use, while the voxel51 design is much simpler.