Eric P. Nusbaum commited on
Commit
2839c47
·
1 Parent(s): 633e7c4

Update to use ONNX

Browse files
Files changed (1) hide show
  1. app.py +29 -9
app.py CHANGED
@@ -49,14 +49,16 @@ class Model:
49
  return {name: outputs[i] for i, name in enumerate(self.output_names)}
50
 
51
  def draw_boxes(image: Image.Image, outputs: dict):
52
- draw = ImageDraw.Draw(image)
53
 
54
- # Dynamic font size based on image width
55
  image_width, image_height = image.size
56
- font_size = max(15, image_width // 100) # Adjust as needed
57
  try:
 
58
  font = ImageFont.truetype("arial.ttf", size=font_size)
59
  except IOError:
 
60
  font = ImageFont.load_default()
61
 
62
  boxes = outputs.get('detected_boxes', [])
@@ -76,7 +78,7 @@ def draw_boxes(image: Image.Image, outputs: dict):
76
  bottom = ymax * image_height
77
 
78
  # Draw bounding box
79
- draw.rectangle([left, top, right, bottom], outline="red", width=2)
80
 
81
  # Prepare label text
82
  text = f"{label}: {score:.2f}"
@@ -86,11 +88,24 @@ def draw_boxes(image: Image.Image, outputs: dict):
86
  text_width = text_bbox[2] - text_bbox[0]
87
  text_height = text_bbox[3] - text_bbox[1]
88
 
89
- # Draw rectangle behind text for better visibility
90
- draw.rectangle([left, top - text_height - 4, left + text_width + 4, top], fill="red")
 
 
 
 
 
 
 
 
91
 
92
  # Draw text
93
- draw.text((left + 2, top - text_height - 2), text, fill="white", font=font)
 
 
 
 
 
94
 
95
  return image
96
 
@@ -121,11 +136,16 @@ def detect_objects(image):
121
  iface = gr.Interface(
122
  fn=detect_objects,
123
  inputs=gr.Image(type="pil"),
124
- outputs=[gr.Image(type="pil", label="Detected Objects"), gr.Textbox(label="Detections")],
 
 
 
125
  title="Object Detection with ONNX Model",
126
  description="Upload an image to detect objects using the ONNX model.",
127
  examples=["examples/card1.jpg", "examples/card2.jpg", "examples/card3.jpg"],
128
- theme="default" # You can choose other themes if desired
 
 
129
  )
130
 
131
  if __name__ == "__main__":
 
49
  return {name: outputs[i] for i, name in enumerate(self.output_names)}
50
 
51
  def draw_boxes(image: Image.Image, outputs: dict):
52
+ draw = ImageDraw.Draw(image, "RGBA") # Use RGBA for transparency
53
 
54
+ # Dynamic font size based on image dimensions
55
  image_width, image_height = image.size
56
+ font_size = max(20, image_width // 50) # Increased minimum font size
57
  try:
58
+ # Attempt to load a truetype font; adjust the path if necessary
59
  font = ImageFont.truetype("arial.ttf", size=font_size)
60
  except IOError:
61
+ # Fallback to default font if truetype font is not found
62
  font = ImageFont.load_default()
63
 
64
  boxes = outputs.get('detected_boxes', [])
 
78
  bottom = ymax * image_height
79
 
80
  # Draw bounding box
81
+ draw.rectangle([left, top, right, bottom], outline="red", width=3)
82
 
83
  # Prepare label text
84
  text = f"{label}: {score:.2f}"
 
88
  text_width = text_bbox[2] - text_bbox[0]
89
  text_height = text_bbox[3] - text_bbox[1]
90
 
91
+ # Calculate label background position
92
+ # Ensure the label box does not go above the image
93
+ label_top = max(top - text_height - 10, 0)
94
+ label_left = left
95
+
96
+ # Draw semi-transparent rectangle behind text
97
+ draw.rectangle(
98
+ [label_left, label_top, label_left + text_width + 10, label_top + text_height + 10],
99
+ fill=(255, 0, 0, 160) # Semi-transparent red
100
+ )
101
 
102
  # Draw text
103
+ draw.text(
104
+ (label_left + 5, label_top + 5),
105
+ text,
106
+ fill="white",
107
+ font=font
108
+ )
109
 
110
  return image
111
 
 
136
  iface = gr.Interface(
137
  fn=detect_objects,
138
  inputs=gr.Image(type="pil"),
139
+ outputs=[
140
+ gr.Image(type="pil", label="Detected Objects"),
141
+ gr.Textbox(label="Detections")
142
+ ],
143
  title="Object Detection with ONNX Model",
144
  description="Upload an image to detect objects using the ONNX model.",
145
  examples=["examples/card1.jpg", "examples/card2.jpg", "examples/card3.jpg"],
146
+ theme="default", # You can choose other themes if desired
147
+ allow_flagging="never", # Disable flagging if not needed
148
+ layout="horizontal" # Display outputs side by side
149
  )
150
 
151
  if __name__ == "__main__":