Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,54 +1,45 @@
|
|
| 1 |
-
import cv2
|
| 2 |
-
import torch
|
| 3 |
-
from ultralytics import YOLO
|
| 4 |
import gradio as gr
|
| 5 |
-
|
| 6 |
-
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
#
|
| 10 |
-
|
| 11 |
-
|
| 12 |
-
|
| 13 |
-
|
| 14 |
-
|
| 15 |
-
|
| 16 |
-
|
| 17 |
-
#
|
| 18 |
-
|
| 19 |
-
|
| 20 |
-
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 27 |
else:
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
|
| 33 |
-
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
|
| 38 |
-
|
| 39 |
-
|
| 40 |
-
|
| 41 |
-
|
| 42 |
-
|
| 43 |
-
cap.release()
|
| 44 |
-
return stock_status
|
| 45 |
-
|
| 46 |
-
# Gradio interface to upload a video and classify stock
|
| 47 |
-
def main(video_input):
|
| 48 |
-
return classify_video(video_input)
|
| 49 |
-
|
| 50 |
-
# Creating the Gradio interface
|
| 51 |
-
iface = gr.Interface(fn=main, inputs=gr.Video(), outputs="text")
|
| 52 |
|
| 53 |
if __name__ == "__main__":
|
| 54 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
+
from ultralytics import YOLO
|
| 3 |
+
from PIL import Image
|
| 4 |
+
|
| 5 |
+
# -----------------------------
|
| 6 |
+
# Load YOLO model
|
| 7 |
+
# -----------------------------
|
| 8 |
+
model = YOLO("./data/best.pt") # make sure this path matches your folder structure
|
| 9 |
+
|
| 10 |
+
# -----------------------------
|
| 11 |
+
# Prediction function
|
| 12 |
+
# -----------------------------
|
| 13 |
+
def predict(image):
|
| 14 |
+
# Run prediction
|
| 15 |
+
results = model.predict(image, conf=0.5)
|
| 16 |
+
|
| 17 |
+
# Annotated image with bounding boxes
|
| 18 |
+
result_img = results[0].plot()
|
| 19 |
+
|
| 20 |
+
# Extract detected labels
|
| 21 |
+
detected_labels = results[0].boxes.cls.tolist()
|
| 22 |
+
names = results[0].names
|
| 23 |
+
detected_objects = [names[int(cls_id)] for cls_id in detected_labels]
|
| 24 |
+
|
| 25 |
+
# Text output
|
| 26 |
+
if detected_objects:
|
| 27 |
+
label_text = f"✅ Detected objects: {', '.join(detected_objects)}"
|
| 28 |
else:
|
| 29 |
+
label_text = "❌ No objects detected."
|
| 30 |
+
|
| 31 |
+
return Image.fromarray(result_img), label_text
|
| 32 |
+
|
| 33 |
+
# -----------------------------
|
| 34 |
+
# Gradio Interface
|
| 35 |
+
# -----------------------------
|
| 36 |
+
demo = gr.Interface(
|
| 37 |
+
fn=predict,
|
| 38 |
+
inputs=gr.Image(type="pil"),
|
| 39 |
+
outputs=[gr.Image(type="pil", label="Detection Result"), gr.Textbox(label="Detected Objects")],
|
| 40 |
+
title="🥤 Bottle Detection with YOLOv11",
|
| 41 |
+
description="Upload an image to check if a **bottle** is detected using your trained YOLOv11 model."
|
| 42 |
+
)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 43 |
|
| 44 |
if __name__ == "__main__":
|
| 45 |
+
demo.launch()
|