Spaces:
Sleeping
Sleeping
Eric P. Nusbaum
commited on
Commit
·
cb58d33
1
Parent(s):
076eeb1
Benchmarking
Browse files
app.py
CHANGED
@@ -4,6 +4,7 @@ import onnx
|
|
4 |
import onnxruntime
|
5 |
from PIL import Image, ImageDraw, ImageFont
|
6 |
import gradio as gr
|
|
|
7 |
|
8 |
# Constants
|
9 |
PROB_THRESHOLD = 0.5 # Minimum probability to show results
|
@@ -44,9 +45,15 @@ class Model:
|
|
44 |
if not self.is_range255:
|
45 |
input_array = input_array / 255.0 # Normalize to [0,1]
|
46 |
|
47 |
-
# Run inference
|
|
|
48 |
outputs = self.session.run(self.output_names, {self.input_name: input_array.astype(self.input_type)})
|
49 |
-
|
|
|
|
|
|
|
|
|
|
|
50 |
|
51 |
def draw_boxes(image: Image.Image, outputs: dict):
|
52 |
draw = ImageDraw.Draw(image, "RGBA") # Use RGBA for transparency
|
@@ -115,7 +122,7 @@ def draw_boxes(image: Image.Image, outputs: dict):
|
|
115 |
model = Model(MODEL_PATH)
|
116 |
|
117 |
def detect_objects(image):
|
118 |
-
outputs = model.predict(image)
|
119 |
annotated_image = draw_boxes(image.copy(), outputs)
|
120 |
|
121 |
# Prepare detection summary
|
@@ -131,6 +138,7 @@ def detect_objects(image):
|
|
131 |
detections.append(f"{label}: {score:.2f}")
|
132 |
|
133 |
detection_summary = "\n".join(detections) if detections else "No objects detected."
|
|
|
134 |
|
135 |
return annotated_image, detection_summary
|
136 |
|
@@ -153,4 +161,4 @@ iface = gr.Interface(
|
|
153 |
)
|
154 |
|
155 |
if __name__ == "__main__":
|
156 |
-
iface.launch()
|
|
|
4 |
import onnxruntime
|
5 |
from PIL import Image, ImageDraw, ImageFont
|
6 |
import gradio as gr
|
7 |
+
import time # Import time for benchmarking
|
8 |
|
9 |
# Constants
|
10 |
PROB_THRESHOLD = 0.5 # Minimum probability to show results
|
|
|
45 |
if not self.is_range255:
|
46 |
input_array = input_array / 255.0 # Normalize to [0,1]
|
47 |
|
48 |
+
# Run inference with benchmarking
|
49 |
+
start_time = time.time() # Start timing
|
50 |
outputs = self.session.run(self.output_names, {self.input_name: input_array.astype(self.input_type)})
|
51 |
+
end_time = time.time() # End timing
|
52 |
+
|
53 |
+
execution_time = (end_time - start_time) * 1000 # Convert to milliseconds
|
54 |
+
print(f"Inference time: {execution_time:.2f} ms")
|
55 |
+
|
56 |
+
return {name: outputs[i] for i, name in enumerate(self.output_names)}, execution_time
|
57 |
|
58 |
def draw_boxes(image: Image.Image, outputs: dict):
|
59 |
draw = ImageDraw.Draw(image, "RGBA") # Use RGBA for transparency
|
|
|
122 |
model = Model(MODEL_PATH)
|
123 |
|
124 |
def detect_objects(image):
|
125 |
+
outputs, execution_time = model.predict(image)
|
126 |
annotated_image = draw_boxes(image.copy(), outputs)
|
127 |
|
128 |
# Prepare detection summary
|
|
|
138 |
detections.append(f"{label}: {score:.2f}")
|
139 |
|
140 |
detection_summary = "\n".join(detections) if detections else "No objects detected."
|
141 |
+
detection_summary += f"\n\nInference Time: {execution_time:.2f} ms"
|
142 |
|
143 |
return annotated_image, detection_summary
|
144 |
|
|
|
161 |
)
|
162 |
|
163 |
if __name__ == "__main__":
|
164 |
+
iface.launch()
|