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

Update to use ONNX

Browse files
Files changed (2) hide show
  1. app.py +28 -11
  2. requirements.txt +1 -1
app.py CHANGED
@@ -50,8 +50,12 @@ class Model:
50
 
51
  def draw_boxes(image: Image.Image, outputs: dict):
52
  draw = ImageDraw.Draw(image)
 
 
 
 
53
  try:
54
- font = ImageFont.truetype("arial.ttf", size=16)
55
  except IOError:
56
  font = ImageFont.load_default()
57
 
@@ -63,18 +67,31 @@ def draw_boxes(image: Image.Image, outputs: dict):
63
  if score < PROB_THRESHOLD:
64
  continue
65
  label = LABELS[int(cls)]
 
66
  # Assuming box format: [ymin, xmin, ymax, xmax] normalized [0,1]
67
  ymin, xmin, ymax, xmax = box
68
- width, height = image.size
69
- left = xmin * width
70
- right = xmax * width
71
- top = ymin * height
72
- bottom = ymax * height
 
73
  draw.rectangle([left, top, right, bottom], outline="red", width=2)
 
 
74
  text = f"{label}: {score:.2f}"
75
- text_size = draw.textsize(text, font=font)
76
- draw.rectangle([left, top - text_size[1], left + text_size[0], top], fill="red")
77
- draw.text((left, top - text_size[1]), text, fill="white", font=font)
 
 
 
 
 
 
 
 
 
78
  return image
79
 
80
  # Initialize model
@@ -107,9 +124,9 @@ iface = gr.Interface(
107
  outputs=[gr.Image(type="pil", label="Detected Objects"), gr.Textbox(label="Detections")],
108
  title="Object Detection with ONNX Model",
109
  description="Upload an image to detect objects using the ONNX model.",
110
- examples=["examples/card1.jpg", "examples/card2.jpg", "examples/card3.jpg"]
 
111
  )
112
 
113
  if __name__ == "__main__":
114
  iface.launch()
115
-
 
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
 
 
67
  if score < PROB_THRESHOLD:
68
  continue
69
  label = LABELS[int(cls)]
70
+
71
  # Assuming box format: [ymin, xmin, ymax, xmax] normalized [0,1]
72
  ymin, xmin, ymax, xmax = box
73
+ left = xmin * image_width
74
+ right = xmax * image_width
75
+ top = ymin * image_height
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}"
83
+
84
+ # Calculate text size using textbbox
85
+ text_bbox = draw.textbbox((0, 0), text, font=font)
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
 
97
  # Initialize model
 
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__":
132
  iface.launch()
 
requirements.txt CHANGED
@@ -1,5 +1,5 @@
1
  gradio==3.32.0
2
  onnx==1.14.0
3
  onnxruntime==1.15.1
4
- Pillow==9.5.0
5
  numpy==1.25.0
 
1
  gradio==3.32.0
2
  onnx==1.14.0
3
  onnxruntime==1.15.1
4
+ Pillow>=10.0.0
5
  numpy==1.25.0