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