import cv2 import numpy as np import gradio as gr import os # ✅ Load YOLO model files (Ensure these files are uploaded to the Space) yolo_config = "yolov3.cfg" yolo_weights = "yolov3.weights" yolo_classes = "coco.names" # ✅ Load class labels with open(yolo_classes, "r") as f: classes = [line.strip() for line in f.readlines()] # ✅ Load YOLO model net = cv2.dnn.readNet(yolo_weights, yolo_config) layer_names = net.getLayerNames() output_layers = [layer_names[i - 1] for i in net.getUnconnectedOutLayers()] # ✅ Object Detection Function def detect_objects(image): img = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) # Convert from RGB to BGR height, width, _ = img.shape # Convert image to blob blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False) net.setInput(blob) outs = net.forward(output_layers) # Process detected objects class_ids, confidences, boxes = [], [], [] for out in outs: for detection in out: scores = detection[5:] class_id = np.argmax(scores) confidence = scores[class_id] if confidence > 0.5: center_x, center_y, w, h = (detection[0:4] * [width, height, width, height]).astype("int") x = int(center_x - w / 2) y = int(center_y - h / 2) boxes.append([x, y, w, h]) confidences.append(float(confidence)) class_ids.append(class_id) # Non-maximum suppression indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4) colors = np.random.uniform(0, 255, size=(len(classes), 3)) # Draw bounding boxes for i in indexes.flatten(): x, y, w, h = boxes[i] label = f"{classes[class_ids[i]]}: {confidences[i]:.2f}" color = colors[class_ids[i]] cv2.rectangle(img, (x, y), (x + w, y + h), color, 2) cv2.putText(img, label, (x, y - 10), cv2.FONT_HERSHEY_SIMPLEX, 0.5, color, 2) img_rgb = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # Convert back to RGB return img_rgb # ✅ Gradio Interface demo = gr.Interface( fn=detect_objects, inputs=gr.Image(type="numpy"), outputs=gr.Image(type="numpy"), title="YOLOv3 Object Detection", description="Upload an image to detect objects using YOLOv3.", ) # ✅ Launch Gradio App demo.launch()