Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,7 +1,7 @@
|
|
| 1 |
import os
|
| 2 |
import urllib.request
|
| 3 |
-
import cv2
|
| 4 |
import numpy as np
|
|
|
|
| 5 |
from ultralytics import YOLO
|
| 6 |
import gradio as gr
|
| 7 |
|
|
@@ -24,42 +24,31 @@ model = YOLO(MODEL_PATH)
|
|
| 24 |
# --- Detection function ---
|
| 25 |
def detect_objects(image):
|
| 26 |
"""
|
| 27 |
-
Input: image (
|
| 28 |
Output: annotated image, detected object names
|
| 29 |
"""
|
| 30 |
-
# Convert BGR
|
| 31 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 32 |
|
| 33 |
-
#
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
# Run YOLO inference with confidence threshold 0.2
|
| 37 |
-
results = model(image_resized, conf=0.2)
|
| 38 |
|
| 39 |
-
# Annotated image
|
| 40 |
annotated_image = results[0].plot()
|
| 41 |
|
| 42 |
-
#
|
|
|
|
|
|
|
|
|
|
| 43 |
if results[0].boxes is not None:
|
| 44 |
detected_classes = [model.names[int(c)] for c in results[0].boxes.cls]
|
| 45 |
detected_text = ", ".join(detected_classes) if detected_classes else "No objects detected"
|
| 46 |
else:
|
| 47 |
detected_text = "No objects detected"
|
| 48 |
|
| 49 |
-
return
|
| 50 |
|
| 51 |
# --- Gradio Interface ---
|
| 52 |
demo = gr.Interface(
|
| 53 |
fn=detect_objects,
|
| 54 |
-
inputs=
|
| 55 |
-
outputs=[
|
| 56 |
-
gr.Image(type="numpy", label="Detected Objects"),
|
| 57 |
-
gr.Textbox(label="Objects Detected")
|
| 58 |
-
],
|
| 59 |
-
title="🧠 Object Detection App",
|
| 60 |
-
description="Upload an image — YOLOv8s detects all objects and lists their names!"
|
| 61 |
-
)
|
| 62 |
-
|
| 63 |
-
# --- Launch the app ---
|
| 64 |
-
if __name__ == "__main__":
|
| 65 |
-
demo.launch(server_name="0.0.0.0", server_port=7860)
|
|
|
|
| 1 |
import os
|
| 2 |
import urllib.request
|
|
|
|
| 3 |
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
from ultralytics import YOLO
|
| 6 |
import gradio as gr
|
| 7 |
|
|
|
|
| 24 |
# --- Detection function ---
|
| 25 |
def detect_objects(image):
|
| 26 |
"""
|
| 27 |
+
Input: image in BGR format (from Gradio/OpenCV)
|
| 28 |
Output: annotated image, detected object names
|
| 29 |
"""
|
| 30 |
+
# Convert BGR to RGB for YOLO
|
| 31 |
image_rgb = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 32 |
|
| 33 |
+
# Run YOLO inference directly on RGB image
|
| 34 |
+
results = model(image_rgb, conf=0.25) # confidence threshold 25%
|
|
|
|
|
|
|
|
|
|
| 35 |
|
| 36 |
+
# Annotated image (YOLO expects RGB, but plot returns RGB)
|
| 37 |
annotated_image = results[0].plot()
|
| 38 |
|
| 39 |
+
# Convert annotated image back to BGR for OpenCV/Gradio display
|
| 40 |
+
annotated_bgr = cv2.cvtColor(annotated_image, cv2.COLOR_RGB2BGR)
|
| 41 |
+
|
| 42 |
+
# Extract detected object names
|
| 43 |
if results[0].boxes is not None:
|
| 44 |
detected_classes = [model.names[int(c)] for c in results[0].boxes.cls]
|
| 45 |
detected_text = ", ".join(detected_classes) if detected_classes else "No objects detected"
|
| 46 |
else:
|
| 47 |
detected_text = "No objects detected"
|
| 48 |
|
| 49 |
+
return annotated_bgr, detected_text
|
| 50 |
|
| 51 |
# --- Gradio Interface ---
|
| 52 |
demo = gr.Interface(
|
| 53 |
fn=detect_objects,
|
| 54 |
+
inputs=g
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|