How to use onnxruntime for prediction correctly and predict the bounding boxes for people.
import onnxruntime as ort
import numpy as np
import cv2
onnx_model_path = 'yolov8n.onnx'
session = ort.InferenceSession(onnx_model_path)
image_path = r"C:UsersamarnOneDrivePicturesfather mother.jpg"
image = cv2.imread(image_path,1)
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
image = cv2.resize(image, (640, 640))
img_batch = np.expand_dims(image, axis=0)
img_transposed = np.transpose(img_batch, ( 0,3, 1,2)).astype(np.float32)
for i,input_info in enumerate(session.get_inputs()):
print(i, input_info)
input_name = session.get_inputs()[0].name
output_names = [output.name for output in session.get_outputs()]
outputs = session.run(output_names, {input_name: img_transposed})
detections = outputs[0] # Extract the detections from the output
labels = [
"person", "bicycle", "car", "motorcycle", "airplane", "bus", "train", "truck",
"boat", "traffic light", "fire hydrant", "stop sign", "parking meter", "bench",
"bird", "cat", "dog", "horse", "sheep", "cow", "elephant", "bear", "zebra",
"giraffe", "backpack", "umbrella", "handbag", "tie", "suitcase", "frisbee",
"skis", "snowboard", "sports ball", "kite", "baseball bat", "baseball glove",
"skateboard", "surfboard", "tennis racket", "bottle", "wine glass", "cup", "fork",
"knife", "spoon", "bowl", "banana", "apple", "sandwich", "orange", "broccoli",
"carrot", "hot dog", "pizza", "donut", "cake", "chair", "couch", "potted plant",
"bed", "dining table", "toilet", "tv", "laptop", "mouse", "remote", "keyboard",
"cell phone", "microwave", "oven", "toaster", "sink", "refrigerator", "book",
"clock", "vase", "scissors", "teddy bear", "hair drier", "toothbrush"
] # Truncated for brevity; fill in all COCO labels
confidence_threshold = 0.5
nms_threshold = 0.4
human_label = "person" # Label name for humans
for detection in detections:
# Extract coordinates, confidence, and class probabilities
x, y, w, h = detection[:4]
confidence = detection[4]
class_probs = detection[5:]
print(len(class_probs),len(labels))
# Ensure that class_probs only covers the range of actual classes
if len(class_probs) != len(labels):
print("Unexpected number of class probabilities:", len(class_probs))
continue
class_id = np.argmax(class_probs)
# print(class_id)#, y, w, h, confidence, class_probs, class_id)
label = labels[class_id]
print(label)
# Check if the detected object is a human and if confidence is above threshold
if label == human_label:# and confidence > confidence_threshold:
print(x, y, w, h, confidence, class_probs, class_id)
# Convert to corner coordinates
left = (x - w / 2) * image.width
top = (y - h / 2) * image.height
right = (x + w / 2) * image.width
bottom = (y + h / 2) * image.height
# Draw rectangle
# draw.rectangle([left, top, right, bottom], outline="red", width=10)
cv2.rectangle(image, (int(left), int(top)), (int(right), int(bottom)), (0, 0, 255), 2)
# Save or show the image
# image.show()
cv2.imshow('image', np.array(image))
cv2.waitKey(0)
i loaded the run time but how can i predict? length of class_probs i am getting 79 and length of labels is 80. is any mistakes i am doing while processing.
I want draw bounding boxes using onnxruntime.
I written code above. I am try to predict and draw bounding boxes.
I am using python for this.