Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -9,12 +9,11 @@ from pathlib import Path
|
|
| 9 |
from ultralytics import YOLO
|
| 10 |
|
| 11 |
# Load YOLOv5 model for ONNX export
|
| 12 |
-
model = YOLO("yolov5n.pt")
|
| 13 |
|
| 14 |
# Export to ONNX format
|
| 15 |
model.export(format="onnx", dynamic=True)
|
| 16 |
|
| 17 |
-
|
| 18 |
os.makedirs("models", exist_ok=True)
|
| 19 |
|
| 20 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
@@ -44,49 +43,61 @@ inference_count = 0
|
|
| 44 |
|
| 45 |
def detect_objects(image):
|
| 46 |
global total_inference_time, inference_count
|
| 47 |
-
|
| 48 |
if image is None:
|
| 49 |
return None
|
| 50 |
-
|
| 51 |
start_time = time.time()
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 63 |
inference_time = time.time() - start_time
|
| 64 |
total_inference_time += inference_time
|
| 65 |
inference_count += 1
|
| 66 |
avg_inference_time = total_inference_time / inference_count
|
| 67 |
fps = 1 / inference_time
|
| 68 |
-
|
| 69 |
-
#
|
| 70 |
-
output_image = image
|
| 71 |
-
|
| 72 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 73 |
for det in detections:
|
| 74 |
-
x1, y1, x2, y2, conf, class_id =
|
| 75 |
if conf < 0.3: # Confidence threshold
|
| 76 |
continue
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 80 |
label = f"Class {class_id} {conf:.2f}"
|
| 81 |
-
cv2.putText(output_image, label, (x1, y1 - 10),
|
| 82 |
-
|
| 83 |
-
|
| 84 |
# Display FPS
|
| 85 |
-
cv2.putText(output_image, f"FPS: {fps:.2f}", (20, 40),
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 89 |
-
|
| 90 |
return output_image
|
| 91 |
|
| 92 |
# Gradio Interface
|
|
|
|
| 9 |
from ultralytics import YOLO
|
| 10 |
|
| 11 |
# Load YOLOv5 model for ONNX export
|
| 12 |
+
model = YOLO("yolov5n.pt")
|
| 13 |
|
| 14 |
# Export to ONNX format
|
| 15 |
model.export(format="onnx", dynamic=True)
|
| 16 |
|
|
|
|
| 17 |
os.makedirs("models", exist_ok=True)
|
| 18 |
|
| 19 |
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
|
|
|
|
| 43 |
|
| 44 |
def detect_objects(image):
|
| 45 |
global total_inference_time, inference_count
|
|
|
|
| 46 |
if image is None:
|
| 47 |
return None
|
| 48 |
+
|
| 49 |
start_time = time.time()
|
| 50 |
+
|
| 51 |
+
# Preprocess image
|
| 52 |
+
original_shape = image.shape
|
| 53 |
+
input_shape = (416, 416)
|
| 54 |
+
image_resized = cv2.resize(image, input_shape)
|
| 55 |
+
image_norm = image_resized.astype(np.float32) / 255.0
|
| 56 |
+
image_transposed = np.transpose(image_norm, (2, 0, 1))
|
| 57 |
+
image_batch = np.expand_dims(image_transposed, axis=0)
|
| 58 |
+
|
| 59 |
+
# Get input name and run inference
|
| 60 |
+
input_name = session.get_inputs()[0].name
|
| 61 |
+
outputs = session.run(None, {input_name: image_batch})
|
| 62 |
+
|
| 63 |
+
# Process detections
|
| 64 |
+
detections = outputs[0][0] # First batch, all detections
|
| 65 |
+
|
| 66 |
+
# Calculate timing
|
| 67 |
inference_time = time.time() - start_time
|
| 68 |
total_inference_time += inference_time
|
| 69 |
inference_count += 1
|
| 70 |
avg_inference_time = total_inference_time / inference_count
|
| 71 |
fps = 1 / inference_time
|
| 72 |
+
|
| 73 |
+
# Create a copy of the original image for visualization
|
| 74 |
+
output_image = image.copy()
|
| 75 |
+
|
| 76 |
+
# Scale factor for bounding box coordinates
|
| 77 |
+
scale_x = original_shape[1] / input_shape[0]
|
| 78 |
+
scale_y = original_shape[0] / input_shape[1]
|
| 79 |
+
|
| 80 |
+
# Draw bounding boxes and labels
|
| 81 |
for det in detections:
|
| 82 |
+
x1, y1, x2, y2, conf, class_id = det[:6]
|
| 83 |
if conf < 0.3: # Confidence threshold
|
| 84 |
continue
|
| 85 |
+
|
| 86 |
+
# Convert to original image coordinates
|
| 87 |
+
x1, x2 = int(x1 * scale_x), int(x2 * scale_x)
|
| 88 |
+
y1, y2 = int(y1 * scale_y), int(y2 * scale_y)
|
| 89 |
+
class_id = int(class_id)
|
| 90 |
+
|
| 91 |
+
# Draw rectangle and label
|
| 92 |
+
color = tuple(map(int, colors[class_id]))
|
| 93 |
+
cv2.rectangle(output_image, (x1, y1), (x2, y2), color, 2)
|
| 94 |
label = f"Class {class_id} {conf:.2f}"
|
| 95 |
+
cv2.putText(output_image, label, (x1, y1 - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 96 |
+
|
|
|
|
| 97 |
# Display FPS
|
| 98 |
+
cv2.putText(output_image, f"FPS: {fps:.2f}", (20, 40), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 99 |
+
cv2.putText(output_image, f"Avg FPS: {1/avg_inference_time:.2f}", (20, 70), cv2.FONT_HERSHEY_SIMPLEX, 1, (0, 255, 0), 2)
|
| 100 |
+
|
|
|
|
|
|
|
| 101 |
return output_image
|
| 102 |
|
| 103 |
# Gradio Interface
|