Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,70 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import cv2
|
| 2 |
+
import numpy as np
|
| 3 |
+
import gradio as gr
|
| 4 |
+
import os
|
| 5 |
+
|
| 6 |
+
# ✅ Load YOLO model files (Ensure these files are uploaded to the Space)
|
| 7 |
+
yolo_config = "yolov3.cfg"
|
| 8 |
+
yolo_weights = "yolov3.weights"
|
| 9 |
+
yolo_classes = "coco.names"
|
| 10 |
+
|
| 11 |
+
# ✅ Load class labels
|
| 12 |
+
with open(yolo_classes, "r") as f:
|
| 13 |
+
classes = [line.strip() for line in f.readlines()]
|
| 14 |
+
|
| 15 |
+
# ✅ Load YOLO model
|
| 16 |
+
net = cv2.dnn.readNet(yolo_weights, yolo_config)
|
| 17 |
+
layer_names = net.getLayerNames()
|
| 18 |
+
output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()]
|
| 19 |
+
|
| 20 |
+
# ✅ Object Detection Function
|
| 21 |
+
def detect_objects(image):
|
| 22 |
+
img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert from RGB to BGR
|
| 23 |
+
height, width, _ = img.shape
|
| 24 |
+
|
| 25 |
+
# Convert image to blob
|
| 26 |
+
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
|
| 27 |
+
net.setInput(blob)
|
| 28 |
+
outs = net.forward(output_layers)
|
| 29 |
+
|
| 30 |
+
# Process detected objects
|
| 31 |
+
class_ids, confidences, boxes = [], [], []
|
| 32 |
+
for out in outs:
|
| 33 |
+
for detection in out:
|
| 34 |
+
scores = detection[5:]
|
| 35 |
+
class_id = np.argmax(scores)
|
| 36 |
+
confidence = scores[class_id]
|
| 37 |
+
if confidence > 0.5:
|
| 38 |
+
center_x, center_y, w, h = (detection[0:4] * [width, height, width, height]).astype("int")
|
| 39 |
+
x = int(center_x - w / 2)
|
| 40 |
+
y = int(center_y - h / 2)
|
| 41 |
+
boxes.append([x, y, w, h])
|
| 42 |
+
confidences.append(float(confidence))
|
| 43 |
+
class_ids.append(class_id)
|
| 44 |
+
|
| 45 |
+
# Non-maximum suppression
|
| 46 |
+
indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
|
| 47 |
+
colors = np.random.uniform(0, 255, size=(len(classes), 3))
|
| 48 |
+
|
| 49 |
+
# Draw bounding boxes
|
| 50 |
+
for i in indexes.flatten():
|
| 51 |
+
x, y, w, h = boxes[i]
|
| 52 |
+
label = f"{classes[class_ids[i]]}: {confidences[i]:.2f}"
|
| 53 |
+
color = colors[class_ids[i]]
|
| 54 |
+
cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
|
| 55 |
+
cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2)
|
| 56 |
+
|
| 57 |
+
img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert back to RGB
|
| 58 |
+
return img_rgb
|
| 59 |
+
|
| 60 |
+
# ✅ Gradio Interface
|
| 61 |
+
demo = gr.Interface(
|
| 62 |
+
fn=detect_objects,
|
| 63 |
+
inputs=gr.Image(type="numpy"),
|
| 64 |
+
outputs=gr.Image(type="numpy"),
|
| 65 |
+
title="YOLOv3 Object Detection",
|
| 66 |
+
description="Upload an image to detect objects using YOLOv3.",
|
| 67 |
+
)
|
| 68 |
+
|
| 69 |
+
# ✅ Launch Gradio App
|
| 70 |
+
demo.launch()
|