Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

creating task with image via rest api #493

Closed
roik7 opened this issue Jun 11, 2019 · 5 comments
Closed

creating task with image via rest api #493

roik7 opened this issue Jun 11, 2019 · 5 comments
Labels
question Further information is requested

Comments

@roik7
Copy link

roik7 commented Jun 11, 2019

Hello,
I'm new to cvat but from what I saw so far it's a great project, thank you!
I'm trying to create multiple tasks using the rest api.
Task is being created but when updating its data (http://localhost:8080/api/v1/tasks/<task_id>/data) I get a failure.
Looking at the task log inside the docker image I see the following error:
Traceback (most recent call last):
File "/usr/local/lib/python3.5/dist-packages/rq/worker.py", line 812, in perform_job
rv = job.perform()
File "/usr/local/lib/python3.5/dist-packages/rq/job.py", line 588, in perform
self._result = self._execute()
File "/usr/local/lib/python3.5/dist-packages/rq/job.py", line 594, in _execute
return self.func(*self.args, **self.kwargs)
File "/usr/lib/python3.5/contextlib.py", line 30, in inner
return func(*args, **kwds)
File "/home/django/cvat/apps/engine/task.py", line 372, in _create_thread
_copy_images_to_task(upload_dir, db_task)
File "/home/django/cvat/apps/engine/task.py", line 244, in _copy_images_to_task
raise ValueError("Image files were not found")
ValueError: Image files were not found

This was when I started to suspect there's a problem with the way I upload the images 😄
But not sure what it is the correct format in which they should be sent.
Attaching the code for the upload (I give a directory path in which all the images I want to upload are in, at the moment it's one image that I'm able to upload using the UI).
Any help will be much appreciated.

def add_files_to_task(self, task_id, file_keys, file_source=CLIENT_FILE_UPLOAD_METHOD_NAME):
        update_task_data_request = self._create_url(server_url=SERVER,
                                                    path_params=[API_VERSION, TASK_PATH, str(task_id), TASK_DATA_PATH])
        print("update task data request: {}".format(update_task_data_request))
        get_files_method = self._resolve_get_files_method_by_file_source(file_upload_method_name=file_source)
        data = get_files_method(file_keys=file_keys)
        update_task_data_response = requests.post(update_task_data_request, data=data, auth=self._auth)
        print("update task data response: {}".format(update_task_data_response.json()))
        return update_task_data_response

    def _resolve_get_files_method_by_file_source(self, file_upload_method_name):
        if file_upload_method_name == CLIENT_FILE_UPLOAD_METHOD_NAME:
            return self._get_data_with_files_from_client
        elif file_upload_method_name == SERVER_FILE_UPLOAD_METHOD_NAME:
            return self._get_data_with_file_names_in_server
        raise Exception("No upload method for files was selected")

    def _get_data_with_files_from_client(self, file_keys):
        client_files = []
        print("File paths are:{}".format(file_keys))
        for file_path in file_keys:
            client_file = open(file_path, encoding="utf8", errors='ignore').read()
            client_files.append(client_file)
        return  {
            "client_files": client_files
        }

   def _create_url(self, server_url, path_params):
        path = "/".join(path_params)
        return "{}/{}".format(server_url, path)

@nmanovic
Copy link
Contributor

Hi @roik7 , answered in gitter. You need to add indexes. If you want to send only one file. Don't hesitate to contribute your script. We will be happy to have a simple script or an example. Thus other people can use the same functionality easily.

"client_files" --> "client_files[0]"

@nmanovic nmanovic added the question Further information is requested label Jun 12, 2019
@roik7
Copy link
Author

roik7 commented Jun 13, 2019

@nmanovic - thanks for the help!

for whoever using the requests lib the updated functions are:

    def add_files_to_task(self, task_id, file_keys, file_source=CLIENT_FILE_UPLOAD_METHOD_NAME):
        update_task_data_request = self._create_url(server_url=SERVER,
                                                    path_params=[API_VERSION, TASK_PATH, str(task_id), TASK_DATA_PATH])

        get_files_method = self._resolve_get_files_method_by_file_source(file_upload_method_name=file_source)
        files = get_files_method(file_keys=file_keys)
        return requests.post(update_task_data_request, files=files, auth=self._auth)


   def _get_data_with_files_from_local(self, file_keys):
        files = {}
        file_index = 0
        print("File paths are:{}".format(file_keys))
        for file_path in file_keys:
            local_file = self._get_file_from_path(file_path)
            file_key = "client_files[{}]".format(file_index)
            files[file_key] = local_file
            file_index += 1
        return files


    def _get_file_from_path(self, file_path):
        file_name = os.path.basename(file_path)
        return (file_name, open(file_path, "rb"))

@lmessinger
Copy link

lmessinger commented Aug 22, 2019

for anyone arriving here, to explain further the code - TASK_DATA_PATH is data
so to add remote files, all is needed is:

remote_files = {'remote_files':['https://images.livemint.com/rf/Image-621x414/LiveMint/Period2/2018/03/17/Photos/Processed/[email protected]']}
dataApiEp = API_ENDPOINT + '/14/data' #14 is task id
`

requests.post(url = dataApiEp,json=remote_files,auth=('user','pwd')) #the auth means u added the rest_framework.authentication.BasicAuthentication at the django setting
`

@KuldeepPCodes
Copy link

for anyone arriving here, to explain further the code - TASK_DATA_PATH is data
so to add remote files, all is needed is:

remote_files = {'remote_files':['https://images.livemint.com/rf/Image-621x414/LiveMint/Period2/2018/03/17/Photos/Processed/[email protected]']}
dataApiEp = API_ENDPOINT + '/14/data' #14 is task id
`

requests.post(url = dataApiEp,json=remote_files,auth=('user','pwd')) #the auth means u added the rest_framework.authentication.BasicAuthentication at the django setting
`

hi i am also trying to add a image to task created by Rest end points but get an Value-Error saying :=#raise ValueError('No media data found')
cvat | ValueError: No media data found

@Qalababbass
Copy link

Hi, I had a similar task where i needed to create tasks in a pre-defined project in CVAT using CVAT REST API. I am also uploading images to these tasks. There are some other methods too for debug.
Maybe it will be helpful for somebody new at CVAT.
CVAT_REST_API

Happy for my contribution.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

5 participants