Search code examples
pythonpython-requestsazure-cognitive-servicesmicrosoft-custom-vision

Error while posting data in CustomVision.ai API


I have trained the model on customvision.ai and obtained the prediction key and endpoint.

Here what I have written

import requests
import json
url="https://centralindia.api.cognitive.microsoft.com/customvision/v3.0/Prediction/xxxxxxxxxx/classify/iterations/Iteration1/url"
headers={'content-type':'application/json','Prediction-Key':'24xxxxxxxxxxxxxxxxxxxxxx'}
body={"Url": "https://i.imgur.com/cYzaOkV.jpg"}
r =requests.post(url,data=body,headers=headers)
print(r.content)

But when I tried to run this I get this error: b'{"code":"BadRequestImageUrl","message":"Invalid image url"}'

However, I tried to call my model using a local image and in this case, it works fine here code for written for local img:

import requests
import json
url="https://centralindia.api.cognitive.microsoft.com/customvision/v3.0/Prediction/82xxxxxxxxxxxxxxxxxxxx/classify/iterations/Iteration1/image"
headers={'content-type':'application/octet-stream','Prediction-Key':'24xxxxxxxxxxxxxxxxxx'}
r =requests.post(url,data=open("lentigo-adults-1.jpg","rb"),headers=headers)

print(r.content)

I get my desired output:

b'{"id":"43a1d33b-0ea9-490a-9ae4-d24f8395931a","project":"82462bcc-5616-4f82-98ea-8d4fda55a9e6","iteration":"d0e48a13-e906-4af2-9f4c-15d79e3b4576","created":"2020-10-14T13:48:41.671Z","predictions":[{"probability":0.999876738,"tagId":"b5bd1d3a-dbcd-4b0d-ad0b-3f8d7b12fbea","tagName":"Lentigo"},{"probability":0.0001100349,"tagId":"c69433ef-4630-4e0f-8077-2de485483323","tagName":"Acne Cystic"},{"probability":1.29300688E-05,"tagId":"2102157b-b250-4c3d-bf2a-588cda8e93eb","tagName":"Rosacea"},{"probability":1.10303489E-07,"tagId":"ea68f8e7-b50f-4ca0-88dc-c2c8f3bb6823","tagName":"Herpes"},{"probability":9.26677259E-08,"tagId":"29c5c735-90f7-48ff-b23c-545be3325e9e","tagName":"Acne Pustular"},{"probability":7.811148E-08,"tagId":"579b080c-4ab1-405f-a8bd-5bf32efd9b70","tagName":"Melasma"},{"probability":3.90258634E-08,"tagId":"8340afdd-b758-46b4-9aee-754480437f0e","tagName":"Acne Comedo"},{"probability":1.49268364E-09,"tagId":"b89f964a-1a72-41ec-b8b7-8e0d7e9ef8ff","tagName":"Eczema"}]}'

can anyone please help me?? I want to call this endpoint using img url

Thank you for giving me your important time towards my problem.


Solution

  • According to my test, when we call the image URL endpoint, the body should be json string. Please update code r =requests.post(url,data=body,headers=headers) to response = requests.post( url, headers=headers, data=json.dumps(body))

    for example

    import requests
    import json
    url = "https://testvision03.cognitiveservices.azure.com/customvision/v3.0/Prediction/<>/detect/iterations/Iteration1/url"
    
    body = {"Url": "https://i.imgur.com/cYzaOkV.jpg"}
    headers = {
        'Prediction-Key': '',
        'Content-Type': 'application/json'
    }
    
    response = requests.post(url, headers=headers, data=json.dumps(body))
    
    print(response.text.encode('utf8'))
    

    enter image description here