|
|
import onnxruntime as ort |
|
|
import numpy as np |
|
|
import cv2 |
|
|
import os |
|
|
|
|
|
|
|
|
model_path = "model.onnx" |
|
|
session = ort.InferenceSession(model_path, providers=["CPUExecutionProvider"]) |
|
|
|
|
|
|
|
|
input_folder = "input_images" |
|
|
output_folder = "output_images" |
|
|
os.makedirs(output_folder, exist_ok=True) |
|
|
|
|
|
|
|
|
MODEL_INPUT_SIZE = (320, 320) |
|
|
|
|
|
|
|
|
CLASS_NAMES = ["person", "car", "truck", "bicycle", "dog", "cat"] |
|
|
|
|
|
|
|
|
CLASS_COLORS = { |
|
|
"person": (255, 0, 0), |
|
|
"car": (0, 255, 0), |
|
|
"truck": (0, 0, 255), |
|
|
"bicycle": (255, 255, 0), |
|
|
"dog": (255, 0, 255), |
|
|
"cat": (0, 255, 255), |
|
|
} |
|
|
DEFAULT_COLOR = (200, 200, 200) |
|
|
|
|
|
def preprocess_image(image_path): |
|
|
""" Preprocess image for ONNX model input """ |
|
|
image = cv2.imread(image_path) |
|
|
original_image = image.copy() |
|
|
image = cv2.resize(image, MODEL_INPUT_SIZE) |
|
|
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB) |
|
|
image = image.astype(np.float32) / 255.0 |
|
|
image = np.transpose(image, (2, 0, 1)) |
|
|
image = np.expand_dims(image, axis=0) |
|
|
return image, original_image |
|
|
|
|
|
def postprocess_output(output, orig_image): |
|
|
""" Post-process ONNX model output """ |
|
|
height, width, _ = orig_image.shape |
|
|
|
|
|
|
|
|
boxes = output[0] |
|
|
scores = output[1] |
|
|
class_indices = output[2] |
|
|
|
|
|
|
|
|
for i in range(len(scores)): |
|
|
if scores[i] > 0.5: |
|
|
x1, y1, x2, y2 = boxes[i] |
|
|
x1, y1, x2, y2 = int(x1 * width), int(y1 * height), int(x2 * width), int(y2 * height) |
|
|
|
|
|
|
|
|
class_id = int(class_indices[i]) |
|
|
label = CLASS_NAMES[class_id] if class_id < len(CLASS_NAMES) else "Unknown" |
|
|
confidence = scores[i] |
|
|
|
|
|
|
|
|
color = CLASS_COLORS.get(label, DEFAULT_COLOR) |
|
|
|
|
|
|
|
|
cv2.rectangle(orig_image, (x1, y1), (x2, y2), color, 2) |
|
|
|
|
|
|
|
|
label_text = f"{label}: {confidence:.2f}" |
|
|
text_size = cv2.getTextSize(label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)[0] |
|
|
text_x, text_y = x1, y1 - 10 |
|
|
|
|
|
|
|
|
cv2.rectangle(orig_image, (text_x, text_y - text_size[1] - 5), (text_x + text_size[0] + 5, text_y + 5), color, -1) |
|
|
|
|
|
cv2.putText(orig_image, label_text, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2) |
|
|
|
|
|
return orig_image |
|
|
|
|
|
|
|
|
for image_name in os.listdir(input_folder): |
|
|
image_path = os.path.join(input_folder, image_name) |
|
|
output_path = os.path.join(output_folder, image_name) |
|
|
|
|
|
|
|
|
image_tensor, orig_image = preprocess_image(image_path) |
|
|
|
|
|
|
|
|
inputs = {session.get_inputs()[0].name: image_tensor} |
|
|
outputs = session.run(None, inputs) |
|
|
|
|
|
|
|
|
processed_image = postprocess_output(outputs, orig_image) |
|
|
cv2.imwrite(output_path, processed_image) |
|
|
|
|
|
print(f"Processed: {image_name}") |
|
|
|
|
|
print("Processing complete! Results saved in", output_folder) |
|
|
|