Upload API error

i’ve been trying to use roboflow’s api to upload a lot to images and their annotation and i walked through the provided colab notebook to use it

this is the error

{'error': {'message': 'Unsupported get request. Dataset with ID `XXX` does not exist or cannot be loaded due to missing permissions.', 'type': 'GraphMethodException', 'hint': 'You can see your available datasets by issuing a GET request to /datasets'}}
---------------------------------------------------------------------------
KeyError                                  Traceback (most recent call last)
<ipython-input-10-a855405627e4> in <module>()
     38 
     39 
---> 40       img_id = r.json()['id']
     41 
     42       annotation_filename = os.path.splitext(filename)[0]+'.xml'

KeyError: 'id'

and I’ve used this section

import requests
import base64
import io
from PIL import Image
import os
for filename in os.listdir("."):
    if filename.endswith(".jpg") or filename.endswith(".png"):
      print(filename)

      # Load Image with PIL
      image = Image.open(filename).convert("RGB")

      # Convert to JPEG Buffer
      buffered = io.BytesIO()
      image.save(buffered, quality=90, format="JPEG")

      # Base 64 Encode
      img_str = base64.b64encode(buffered.getvalue())
      img_str = img_str.decode("ascii")



      # Construct the URL
      upload_url = "".join([
          "https://api.roboflow.com/dataset/XXX/upload",
          "?api_key=" + MY_KEY,
          "&name=IMG_0159_JPG.rf.15c0119df282796d2a9c90d18f615672.jpg",
          "&split=train"
      ])

      # POST to the API
      r = requests.post(upload_url, data=img_str, headers={
          "Content-Type": "application/x-www-form-urlencoded"
      })

      # Output result
      print(r.json())


      img_id = r.json()['id']

      annotation_filename = os.path.splitext(filename)[0]+'.xml'
      print(annotation_filename)

      # Read Annotation as String
      annotation_str = open(annotation_filename, "r").read()

      # Construct the URL
      upload_url = "".join([
          "https://api.roboflow.com/dataset/XXX/annotate/" + img_id,
          "?api_key=" + MY_KEY,
          "&name=", annotation_filename
      ])

      # POST to the API
      r = requests.post(upload_url, data=annotation_str, headers={
          "Content-Type": "text/plain"
      })

      # Output result
      print(r.json())

Hello @Ruba0 - thank you for the detailed code and error. In order to upload an image to Roboflow via the upload API you need to specify the dataset name where you have XXX and be sure to include you API key in the variable “MY_KEY”, which you may already have done.

Alternatively, you might prefer to use our PIP package to upload your data - where we have wrapped some of these API nuances

thank you @Jacobsolawetz for the reply!
I tried uploading images and it works but how can I upload their annotations?

---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-26-e41f30109986> in <module>()
      1 project.upload("1007_test.jpg")
----> 2 project.upload("1007_test.xml")

2 frames
/usr/local/lib/python3.7/dist-packages/roboflow/core/project.py in __image_upload(self, image_path, hosted_image, split)
    110             # Convert to PIL Image
    111             img = cv2.imread(image_path)
--> 112             image = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    113             pilImage = Image.fromarray(image)
    114 

error: OpenCV(4.1.2) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed) !_src.empty() in function 'cvtColor'

That looks like an error reading your image, can you display it successfully with your current installation of cv2?

Hi I have used the roboflow package and tried to upload image by using following code snippet

rf = Roboflow(api_key=API_KEY)
project = rf.workspace("caitesting").project("pdf_images")
project.upload(image_path=IMG_PATH)

Here i have a workspace called “caitesting” and project called “pdf_images”.but when i executed above command it will give error as following.

Hi Nipun, this would be the correct code:

rf = Roboflow(api_key=API_KEY)
workspace = rf.workspace()
project = workspace.project("pdf_images")
project.upload(image_path=IMG_PATH)
1 Like

Cool Thanks,I could be able to manage above problem by using same code with different image dimensions.is there any restriction on image dimensions ?

There’s no requirement to pass the image dimensions with the inference query.

There is a 10MB payload limit. However, you can pass a larger image if you pass it via URL from where you may already have uploaded it image={url}

1 Like

Thanks for your quick reply.but when i tried to upload following image from my dataset.It won’t upload properly to the Roboflow project.Then i resized following image into (640,640) and then it worked


Roboflow Package has been used for uploading.so can you take a look at this problem? because in my end Roboflow api will work if images are resized to smaller size.

I also ran into this bug, it looks like the API documentation might be out of date.
My code looks similar to that of @Ruba0

rp2=rf.workspace("Familiar Robotics").project("familiar_robotics/prickly_pear_detect")
[...]
rp.upload(img)

Which leads to the error:

Traceback (most recent call last):
  File "/home/ubuntu/projects/roboflow/cacti_uploader.py", line 31, in <module>
    rp=rf.workspace(wsname).project(projname)
  File "/home/ubuntu/anaconda3/lib/python3.10/site-packages/roboflow/__init__.py", line 283, in workspace
    return Workspace(list_projects, self.api_key, the_workspace, self.model_format)
  File "/home/ubuntu/anaconda3/lib/python3.10/site-packages/roboflow/core/workspace.py", line 37, in __init__
    workspace_info = info["workspace"]
KeyError: 'workspace'
(base) ubuntu@velociraptor:~/projects/roboflow$

I fixed my code following the example from @Mohamed but just wanted to note here that the Python example at:
https://docs.roboflow.com/datasets/adding-data/upload-api

still has the old code that doesn’t work.

1 Like

@espressobot thanks for pointing that out. I just changed that documentation page to reflect the correct usage.

In your particular case, you want to do something like

rp=rf.workspace("familiar_robotics").project("prickly_pear_detect")
rp.upload(img)

Thanks for pointing out that error, please tell us more if you find more errors like that :slight_smile:

1 Like

Thanks Tony!
I’m building an uploader/processor for one of my robots so it can have an image pipeline to push new images up to my project here while its out in the field :slight_smile:

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.