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

Using the Inference API #291

Closed
1 task done
CemHaligur opened this issue Jun 1, 2023 · 11 comments
Closed
1 task done

Using the Inference API #291

CemHaligur opened this issue Jun 1, 2023 · 11 comments
Assignees
Labels
bug Something isn't working fixed Bug is resolved

Comments

@CemHaligur
Copy link

Search before asking

  • I have searched the HUB issues and found no similar bug report.

HUB Component

No response

Bug

Hey,

I was trying to use the API to inference images in my notebook, but when i try to print my 'response' variable i have this : <Response [400]>.
I guess the API don't work, how can i handle this ?

Kind regards !

Environment

No response

Minimal Reproducible Example

No response

Additional

No response

@CemHaligur CemHaligur added the bug Something isn't working label Jun 1, 2023
@github-actions
Copy link

github-actions bot commented Jun 1, 2023

👋 Hello @CemHaligur, thank you for raising an issue about Ultralytics HUB 🚀! Please visit our HUB Docs to learn more, and see our ⭐️ HUB Guidelines to quickly get started uploading datasets and training YOLO models.

If this is a 🐛 Bug Report, please provide screenshots and steps to recreate your problem to help us get started working on a fix.

If this is a ❓ Question, please provide as much information as possible, including dataset, model, environment details etc. so that we might provide the most helpful response.

We try to respond to all issues as promptly as possible. Thank you for your patience!

@kalenmike
Copy link
Contributor

kalenmike commented Jun 1, 2023

Hi @CemHaligur, the response suggests that your request is invalid. Can you give us an example of a request you are sending?

You can also try printing the json of the response to see if there is any more information:

print(response.json())

@kalenmike kalenmike self-assigned this Jun 1, 2023
@kalenmike kalenmike added question A HUB question that does not involve a bug and removed bug Something isn't working labels Jun 1, 2023
@CemHaligur
Copy link
Author

This is an example of the code :

url = "https://api.ultralytics.com/v1/predict/XXXX"

headers = {

"Content-Type": "multipart/form-data",

"x-api-key": "XXX"

}

files = {

"file": ("image", open("./data/Raw/V-00488_2023_02_03_09-23-51.jpeg", "rb"), "image/jpeg")

}

data = {

"size": 640,

"confidence": 0.20,

"iou": 0.45

}

response = requests.post(url, headers=headers, files=files, data=data)

And i try to print the json file i have this :

{'data': {},
'message': 'An image file or URL must be submitted.',
'success': False}

but i have the right path to my file

@kalenmike
Copy link
Contributor

kalenmike commented Jun 1, 2023

@CemHaligur There is an issue with how you are passing the files, I can see the same issue on our example code on HUB. We will get this updated. For now you can follow the docs.

The change needed is here:

files = {
"image": open("./data/Raw/V-00488_2023_02_03_09-23-51.jpeg", "rb")
}

@ape75
Copy link

ape75 commented Jun 2, 2023

Hi.
I have also tried to use the API with some images from my computer and I keep getting the same error as @CemHaligur. I have tried various ways to send the image and also noticed some differences between HUB example code and documentation.

API returns the 400 - Bad Request response status code :
{'data': {}, 'message': 'An image file or URL must be submitted.', 'success': False}

Here is my code:

`import requests

url = "https://api.ultralytics.com/v1/predict/XXXXXXXXXXXXXXXXX"
headers = {
"Content-Type": "multipart/form-data",
"x-api-key": "*********************************************************"
}
files = {
"image": open("image2.jpg", "rb")
}
data = {
"size": 640,
"confidence": 0.25,
"iou": 0.45
}
response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())`

@CemHaligur
Copy link
Author

Same issue with this doc :

import requests

API URL, use actual MODEL_ID

url = f"https://api.ultralytics.com/v1/predict/XXXXXXXXXX"

Headers, use actual API_KEY

headers = {"x-api-key": "XXXXX"}

Inference arguments (optional)

data = {"size": 640, "confidence": 0.20, "iou": 0.45}

Load image and send request

with open("./data/Raw/V-00476_2023_02_03_09-50-04.jpeg", "rb") as image_file:
files = {"image": image_file}
response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())

-> {'data': {}, 'message': 'Unknown', 'success': False}

@glenn-jocher
Copy link
Member

@CemHaligur and @CemHaligur, there seems to be an issue when using specific versions of requests package, which prevents multipart/form-data from being set up correctly. Please make sure you are using requests version 2.22.0 or newer by running !pip install --upgrade requests in your notebook.

If you still face the same error after upgrading the requests package, please try sending the image as bytes instead of a file, as in the following example:

import requests

url = "https://api.ultralytics.com/v1/predict/XXXXXXXXX"
headers = {
    "Content-Type": "multipart/form-data",
    "x-api-key": "XXXXXXXXXXXXXXXXXXXXX",
}
with open("image.jpg", "rb") as f:
    image_data = f.read()
data = {"size": 640, "confidence": 0.25, "iou": 0.45}
response = requests.post(
    url, headers=headers, data=data, files={"image": ("image.jpg", image_data)}
)
print(response.json())

Please let us know if this helps!

@ape75
Copy link

ape75 commented Jun 2, 2023

Hi.
I updated the requests package and tried also your example code, but the problem still remains the same.

{'data': {}, 'message': 'An image file or URL must be submitted.', 'success': False}

@kalenmike
Copy link
Contributor

kalenmike commented Jun 2, 2023

@CemHaligur We found an issue that was causing the Unknown response when not setting the normalize option. Your last example should work now since the fix.

@ape75 Do not set the content type header but allow requests to set it so that a boundary can be set.

import requests

url = "https://api.ultralytics.com/v1/predict/XXXXXXXXX"

headers = {
	"x-api-key": "XXXXXXXXXXXXXXXXXXXXX",
}

data = {
	"size": 640,
	"confidence": 0.25,
	"iou": 0.45
}

files={
	"image": open("image2.jpg", "rb")
}

response = requests.post(url, headers=headers, files=files, data=data)

print(response.json())

@kalenmike kalenmike added bug Something isn't working fixed Bug is resolved and removed question A HUB question that does not involve a bug labels Jun 2, 2023
@kalenmike kalenmike changed the title Using the API Using the Inference API Jun 2, 2023
@ape75
Copy link

ape75 commented Jun 2, 2023

Now it is working fine. 👍
Thank you for the quick response.

@glenn-jocher
Copy link
Member

@ape75 you're welcome! We're glad to hear that the issue has been resolved. If you have any other questions or concerns, please don't hesitate to reach out to us. We're always here to help. Thank you for using Ultralytics HUB!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working fixed Bug is resolved
Projects
None yet
Development

No branches or pull requests

4 participants