Nekshay commited on
Commit
cd66def
·
verified ·
1 Parent(s): d5c46bf

Update onnx_runtime_draw_bboxes.py

Browse files
Files changed (1) hide show
  1. onnx_runtime_draw_bboxes.py +25 -3
onnx_runtime_draw_bboxes.py CHANGED
@@ -18,6 +18,17 @@ MODEL_INPUT_SIZE = (320, 320) # Change based on your model input size
18
  # Define class labels (update based on your model)
19
  CLASS_NAMES = ["person", "car", "truck", "bicycle", "dog", "cat"] # Update accordingly
20
 
 
 
 
 
 
 
 
 
 
 
 
21
  def preprocess_image(image_path):
22
  """ Preprocess image for ONNX model input """
23
  image = cv2.imread(image_path)
@@ -44,15 +55,26 @@ def postprocess_output(output, orig_image):
44
  x1, y1, x2, y2 = boxes[i] # Get box coordinates
45
  x1, y1, x2, y2 = int(x1 * width), int(y1 * height), int(x2 * width), int(y2 * height) # Scale box
46
 
47
- label = CLASS_NAMES[int(class_indices[i])]
 
 
48
  confidence = scores[i]
49
 
 
 
 
50
  # Draw bounding box
51
- cv2.rectangle(orig_image, (x1, y1), (x2, y2), (0, 255, 0), 2)
52
 
53
  # Put label text
54
  label_text = f"{label}: {confidence:.2f}"
55
- cv2.putText(orig_image, label_text, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 255, 0), 2)
 
 
 
 
 
 
56
 
57
  return orig_image
58
 
 
18
  # Define class labels (update based on your model)
19
  CLASS_NAMES = ["person", "car", "truck", "bicycle", "dog", "cat"] # Update accordingly
20
 
21
+ # Define unique colors for each class (BGR format for OpenCV)
22
+ CLASS_COLORS = {
23
+ "person": (255, 0, 0), # Blue
24
+ "car": (0, 255, 0), # Green
25
+ "truck": (0, 0, 255), # Red
26
+ "bicycle": (255, 255, 0), # Cyan
27
+ "dog": (255, 0, 255), # Magenta
28
+ "cat": (0, 255, 255), # Yellow
29
+ }
30
+ DEFAULT_COLOR = (200, 200, 200) # Gray for unknown classes
31
+
32
  def preprocess_image(image_path):
33
  """ Preprocess image for ONNX model input """
34
  image = cv2.imread(image_path)
 
55
  x1, y1, x2, y2 = boxes[i] # Get box coordinates
56
  x1, y1, x2, y2 = int(x1 * width), int(y1 * height), int(x2 * width), int(y2 * height) # Scale box
57
 
58
+ # Get class label
59
+ class_id = int(class_indices[i])
60
+ label = CLASS_NAMES[class_id] if class_id < len(CLASS_NAMES) else "Unknown"
61
  confidence = scores[i]
62
 
63
+ # Get class-specific color
64
+ color = CLASS_COLORS.get(label, DEFAULT_COLOR)
65
+
66
  # Draw bounding box
67
+ cv2.rectangle(orig_image, (x1, y1), (x2, y2), color, 2)
68
 
69
  # Put label text
70
  label_text = f"{label}: {confidence:.2f}"
71
+ text_size = cv2.getTextSize(label_text, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 2)[0]
72
+ text_x, text_y = x1, y1 - 10
73
+
74
+ # Draw text background
75
+ cv2.rectangle(orig_image, (text_x, text_y - text_size[1] - 5), (text_x + text_size[0] + 5, text_y + 5), color, -1)
76
+ # Put label text
77
+ cv2.putText(orig_image, label_text, (text_x, text_y), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 2)
78
 
79
  return orig_image
80