Roboflow API Inference Error

Please share the following so we may better assist you:
When trying to do API inference with a yolov8 model deployed on Roboflow, I get the following error(see screenshot): 413: Payload too large for URL

I was wondering how I could solve this?

  1. Project type: Object Detection
  2. The operating system & browser you are using and their versions: Google Colab
  3. The screenshot of the error being triggered in your browser’s developer tools/console. Please copy/paste the url below to watch how to pull up your devtools

Hi @Alper - what is the pixel size, both width and height, on the image(s) you’ve tried with?

Additionally, what Resize parameter did you resize the images to when you generated the version used for training your model?

Here’s an example of the Resize parameter on one of my datasets:

Hi, thank you for your response! The resize size should be 640 x 640, do I need to resize before I send it to the API? I originally thought it does the resize preprocess automatically. Thank you in advance!

I believe the image is 1080p

I just tried inference with a dataset version of mine that was resized to 640x640 prior to training.

The image I ran inference with was 1080pixels X 820pixels - it did give me a result without error:

When did you upload the YOLOv8 model weights that you’re using for inference? Was it sometime in the last day or 2, or right after the Upload Weights feature was released?

Can you email me the image to test? I’ll send you an email

My test was successful.

I just tried it with both version 3 (YOLOv8n) and 4 (YOLOv8s) of your project (infer credits were added back to make up for the tests I ran).

It seems that what may have happened is that Colab automatically timed out the request? I noticed that the image was 4k resolution, but the file size was only about 800KB when I downloaded it.

Higher image resolution is great for capturing images and labeling data, but as you’ve already seen, “smaller” image sizes speeds up model training and inference.

The best course of action will be to resize images prior to inference. Additionally, I’m including the sample script I used for inference, in case you’d also like to try it on Colab again, or on your host machine.

from roboflow import Roboflow

rf = Roboflow(api_key="INSERT_PRIVATE_API_KEY")

# List all projects for your workspace
workspace = rf.workspace("mohamed-traore-2ekkp")

# Load a specific project
project = workspace.project("face-detection-mik1i")

# Load a specific version of a project
version = project.version(18)

# Retrieve the model of a specific project
model = version.model

# predict on a local image
img_file = "park_faces-1080.jpg"

prediction = model.predict(img_file, confidence=40, overlap=30)

# Save the prediction as an image
prediction.save(output_path='predictions.jpg')

# Convert predictions to JSON and print the result
print(prediction.json())

# Plot the prediction
prediction.plot()

Here’s another example that utilizes the Hosted API, directly:

import cv2
import base64
import numpy as np
import requests


# Construct the Roboflow Infer URL
# (if running locally replace https://detect.roboflow.com/ with eg http://127.0.0.1:9001/)
ROBOFLOW_API_KEY = "INSERT_PRIVATE_API_KEY"
#for my face detection project, the model id is: "face-detection-mik1i"
ROBOFLOW_MODEL_ID = "INSERT_MODEL_ID"
#for my face detection project, the version number is: "18" for this inference example
MODEL_VERSION = "INSERT_VERSION#"
#resize value - for my face detection project, v18, it is 640
ROBOFLOW_SIZE = 640
#path to image for inference, insert path between the empty quotes
img_path = ""

upload_url = "".join([
    "https://detect.roboflow.com/",
    ROBOFLOW_MODEL_ID, "/",
    MODEL_VERSION,
    "?api_key=",
    ROBOFLOW_API_KEY,
    "&format=image",
    "&stroke=2"
])

# Infer via the Roboflow Hosted Inference API and return the result
def infer(img, ROBOFLOW_SIZE, upload_url):
    # Resize (while maintaining the aspect ratio) to improve speed and save bandwidth
    height, width, channels = img.shape
    scale = ROBOFLOW_SIZE / max(height, width)
    img = cv2.resize(img, (round(scale * width), round(scale * height)))

    # Encode image to base64 string
    retval, buffer = cv2.imencode('.jpg', img)
    img_str = base64.b64encode(buffer)

    # Get prediction from Roboflow Infer API
    resp = requests.post(upload_url, data=img_str, headers={
        "Content-Type": "application/x-www-form-urlencoded"
    }, stream=True).raw

    # Parse result image
    image = np.asarray(bytearray(resp.read()), dtype="uint8")
    image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    return image


img_file = cv2.imread(img_path)
result = infer(img_file, ROBOFLOW_SIZE, upload_url)

cv2.imwrite("prediction.jpg", result)

I keep getting this
{‘detail’: ‘Method Not Allowed’}

import cv2
import base64
import numpy as np
import requests

ROBOFLOW_API_KEY = “*************”
ROBOFLOW_MODEL_ID = “drive-drowsiness-detection/2”
MODEL_VERSION = “2”
ROBOFLOW_SIZE = 640 # Adjust this value if necessary for your model
img_path = “Yolo\Images\IMG_1397.jpeg” # Provide the path to your image

upload_url = “”.join([
https://detect.roboflow.com/”,
ROBOFLOW_MODEL_ID, “/”,
MODEL_VERSION,
“?api_key=”,
ROBOFLOW_API_KEY,
“&format=json”,
“&stroke=2”
])

def infer(img, ROBOFLOW_SIZE, upload_url):
# Resize (while maintaining the aspect ratio) to improve speed and save bandwidth
height, width, channels = img.shape
scale = ROBOFLOW_SIZE / max(height, width)
img = cv2.resize(img, (round(scale * width), round(scale * height)))

# Encode image to base64 string
retval, buffer = cv2.imencode('.jpg', img)
img_str = base64.b64encode(buffer).decode('utf-8')

# Get prediction from Roboflow Infer API
response = requests.get(upload_url, data=img_str, headers={
    "Content-Type": "application/x-www-form-urlencoded"
})

# Parse the JSON response
predictions = response.json()

return predictions

Read the image

img_file = cv2.imread(img_path)

if img_file is None:
print(f"Could not open or find the image: {img_path}")
else:
# Run inference
predictions = infer(img_file, ROBOFLOW_SIZE, upload_url)

# Print the predictions
print(predictions)

# for prediction in predictions['predictions']:
#     x, y, width, height = prediction['x'], prediction['y'], prediction['width'], prediction['height']
#     label = prediction['class']
#     confidence = prediction['confidence']

#     start_point = (int(x - width / 2), int(y - height / 2))
#     end_point = (int(x + width / 2), int(y + height / 2))

#     
#     color = (0, 255, 0)  # Green color for bounding box
#     thickness = 2
#     img_file = cv2.rectangle(img_file, start_point, end_point, color, thickness)
    
#     # Add label and confidence
#     label_text = f"{label} ({confidence:.2f})"
#     img_file = cv2.putText(img_file, label_text, (start_point[0], start_point[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness)

# # Save the result
# output_path = "prediction.jpg"
# cv2.imwrite(output_path, img_file)
# print(f"Prediction saved to {output_path}")

Your request was rejected because the server expects a POST request instead of a GET. Replace your requests.get with a requests.post.

Hi, Iurisilvio, I used post request and get the same error
Response Status Code: 405
Response Text: {“detail”:“Method Not Allowed”}
{‘detail’: ‘Method Not Allowed’}
Traceback (most recent call last):
File “d:\GITHUB\PLD\Yolo\roboflow.py”, line 64, in
for prediction in predictions[‘predictions’]:
~~~~~~~~~~~^^^^^^^^^^^^^^^
KeyError: ‘predictions’

import cv2
import base64
import numpy as np
import requests

ROBOFLOW_API_KEY = “eLbSC5htGr3R03suMkL8”
ROBOFLOW_MODEL_ID = “drive-drowsiness-detection/2”
MODEL_VERSION = “2”
ROBOFLOW_SIZE = 640
img_path = “Yolo\Images\image - Brenden Asare(1).jpg”
upload_url = “”.join([
https://detect.roboflow.com/”,
ROBOFLOW_MODEL_ID, “/”,
MODEL_VERSION,
“?api_key=”,
ROBOFLOW_API_KEY,
“&format=json”,
“&stroke=2”
])

def infer(img, ROBOFLOW_SIZE, upload_url):
# Resize (while maintaining the aspect ratio) to improve speed and save bandwidth
height, width, channels = img.shape
scale = ROBOFLOW_SIZE / max(height, width)
img = cv2.resize(img, (round(scale * width), round(scale * height)))

# Encode image to base64 string
retval, buffer = cv2.imencode('.jpg', img)
img_str = base64.b64encode(buffer).decode('utf-8')

# Get prediction from Roboflow Infer API
response = requests.post(upload_url, data=img_str, headers={
    "Content-Type": "application/x-www-form-urlencoded"
})

# Print the raw response text for debugging
print("Response Status Code:", response.status_code)
print("Response Text:", response.text)

# Check if the response is valid JSON
try:
    predictions = response.json()
except ValueError as e:
    print("Error decoding JSON:", e)
    return None

return predictions

Read the image

img_file = cv2.imread(img_path)

if img_file is None:
print(f"Could not open or find the image: {img_path}")
else:
# Run inference
predictions = infer(img_file, ROBOFLOW_SIZE, upload_url)

if predictions:
    # Print the predictions
    print(predictions)

    for prediction in predictions['predictions']:
        x, y, width, height = prediction['x'], prediction['y'], prediction['width'], prediction['height']
        label = prediction['class']
        confidence = prediction['confidence']

        # Calculate the top-left corner of the bounding box
        start_point = (int(x - width / 2), int(y - height / 2))
        end_point = (int(x + width / 2), int(y + height / 2))

        # Draw the bounding box
        color = (0, 255, 0)  # Green color for bounding box
        thickness = 2
        img_file = cv2.rectangle(img_file, start_point, end_point, color, thickness)
        
        # Add label and confidence
        label_text = f"{label} ({confidence:.2f})"
        img_file = cv2.putText(img_file, label_text, (start_point[0], start_point[1] - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, thickness)

    # Save the result
    output_path = "prediction.jpg"
    cv2.imwrite(output_path, img_file)
    print(f"Prediction saved to {output_path}")

Also,aside installing inference or inference_sdk, is there a way i reference my model for predictions. I am using an arduino Nicla vision board and OpenmV IDE and it doesn’t allow for installing the inference or inference_sdk package