Upload API question

Hello! I have been trying to upload a annotation via API lately, but it always return that the annotation format is unsupported, even if i upload a exported annotation from existing images.

Are there more guidelines then what is written in the guide page?

Welcome to the forum! Sorry to hear you’re having issues with the annotate API.

We don’t yet have more documentation for this beyond the annotate via api docs but I’m happy to help. Could you share more info about the API request (ideally a code snippet) and the annotation format you’re uploading?

If you could send me an example image & annotation I’d be happy to test on my side; it’s possible you’re encountering a bug but without more info it’s hard to say.

Once we figure things out we’ll be sure to update the docs to help future readers avoid this problem!

Yes thanks!

Below is my code snippet and file which is used to upload the image and annotations.

Image&annotation:

Blockquote
def _uploadannotation(annotation_str,pic_id,picname,datasetname=‘mydataset’):

upload_url = "".join([
    f"https://api.roboflow.com/dataset/{datasetname}/annotate/{pic_id}",
    "?api_key=mykey",
    "&name=", f"6.xml"
])

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

# Output result
return r.json()

def _uploadimg(img,picname, datasetname=‘mydb’):
image = img
if not isinstance(img,Image.Image):
# Load Image with PIL
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
image = Image.fromarray(img)

# 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([
    f"https://api.roboflow.com/dataset/{datasetname}/upload",
    "?api_key=mykey",
    f"&name={picname}",
    "&split=train"
])

# POST to the API
r = requests.post(upload_url, data=img_str, headers={
    "Content-Type": "application/x-www-form-urlencoded"
})
return json.loads(r.content).get('id',None)

def test():
im = PIL.Image.open(“images_png.rf.7c0985eb122fe55a1b9b030f551eb211.jpg”)
picid = _uploadimg(im,“test.jpg”,“mydb”)
ano = open(“images_png.rf.7c0985eb122fe55a1b9b030f551eb211.xml”,“r”).read()
annotation = _uploadannotation(ano,picid,“images_png.rf.7c0985eb122fe55a1b9b030f551eb211.jpg”,“mydb”)
pass

image:
images_png.rf.7c0985eb122fe55a1b9b030f551eb211

xml:


test_jpg.rf.009934dc7aa1e9fa60d44ce11d25dc76.jpg
test_jpg.rf.009934dc7aa1e9fa60d44ce11d25dc76.jpg

roboflow.ai


128
128
3

0

up
Unspecified
0
0
0

35
96
19
111


Sure!

here are the code snippets:

def _uploadannotation(annotation_str,pic_id,picname,datasetname='db'):

    upload_url = "".join([
        f"https://api.roboflow.com/dataset/{datasetname}/annotate/{pic_id}",
        "?api_key=key",
        "&name=", f"6.xml"
    ])

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

    # Output result
    return r.json()

def _uploadimg(img,picname, datasetname='mb'):
    image = img
    if not isinstance(img,Image.Image):
        # Load Image with PIL
        img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
        image = Image.fromarray(img)

    # 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([
        f"https://api.roboflow.com/dataset/{datasetname}/upload",
        "?api_key=key",
        f"&name={picname}",
        "&split=train"
    ])

    # POST to the API
    r = requests.post(upload_url, data=img_str, headers={
        "Content-Type": "application/x-www-form-urlencoded"
    })
    return json.loads(r.content).get('id',None)


def test():
    im = PIL.Image.open("images_png.rf.7c0985eb122fe55a1b9b030f551eb211.jpg")
    picid = _uploadimg(im,"test.jpg","mb")
    ano = open("images_png.rf.7c0985eb122fe55a1b9b030f551eb211.xml","r").read()
    annotation = _uploadannotation(ano,picid,"images_png.rf.7c0985eb122fe55a1b9b030f551eb211.jpg","mb")
    pass

and here is the xml

xml:


<annotation>
	<folder></folder>
	<filename>test_jpg.rf.009934dc7aa1e9fa60d44ce11d25dc76.jpg</filename>
	<path>test_jpg.rf.009934dc7aa1e9fa60d44ce11d25dc76.jpg</path>
	<source>
		<database>roboflow.ai</database>
	</source>
	<size>
		<width>128</width>
		<height>128</height>
		<depth>3</depth>
	</size>
	<segmented>0</segmented>
	<object>
		<name>up</name>
		<pose>Unspecified</pose>
		<truncated>0</truncated>
		<difficult>0</difficult>
		<occluded>0</occluded>
		<bndbox>
			<xmin>35</xmin>
			<xmax>96</xmax>
			<ymin>19</ymin>
			<ymax>111</ymax>
		</bndbox>
	</object>
</annotation>

images_png.rf.7c0985eb122fe55a1b9b030f551eb211

Could you retry with the Content-Type header as text/plain instead of text/xml? I believe we use the name= query param to infer the type vs the Content-Type; it might be confusing our body parser.

Thanks for the reply! I changed to text/plain and text/plaintext both but none gave a good responds, thats the reason i tried xml as well.

Ok, I’ll loop in an engineer on our team to dive in a little deeper & get back in touch.

Many thanks! :smiley:, I hope this issue can be reproduced easily, if any additional information is necessary or the result cannot be reproduced, I will write a full standalone code to ensure the reproducibility of the issue

1 Like

Hello!

I tested some more variation on the uploading process and it turns out, if i use the filename “6.xml” or “6.txt”. Then the upload of anotation will fail, but with something such as “test.xml” or “test.txt” it works correctly, for now.

I’m unsure if I just missed it completely or it was working a few days ago, but by modifying the code snippet above from ‘6.xml’ to ‘test.xml’ solved my issue, the ‘text/plaintext’ or ‘text/xml’ doesn’t affect the calls as for now.

Thanks for the effort on helping me!

What error does the API produce when this is failing for you?

After some more testing, I found out it is due to the mismatch between the uploaded images name and the annotation name.

In the test script i posted earlier, I uploaded the image under ‘test.jpg’ and annotation under ‘test.txt’, which is why it doesn’t produce an error.

The error I’m receiving under mismatched name is as follows: