Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -2,59 +2,62 @@ import gradio as gr
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import tempfile
|
| 4 |
import cv2
|
| 5 |
-
import os
|
| 6 |
-
import torch
|
| 7 |
-
|
| 8 |
|
|
|
|
| 9 |
model = YOLO("best.pt")
|
| 10 |
|
| 11 |
-
#
|
| 12 |
def predict_image(image):
|
| 13 |
-
results = model.predict(image, imgsz=
|
| 14 |
return results[0].plot()
|
| 15 |
|
| 16 |
-
#
|
| 17 |
def predict_video(video_path):
|
| 18 |
cap = cv2.VideoCapture(video_path)
|
| 19 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 20 |
-
width
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
| 32 |
-
annotated =
|
| 33 |
out.write(annotated)
|
| 34 |
|
| 35 |
cap.release()
|
| 36 |
out.release()
|
|
|
|
| 37 |
|
| 38 |
-
|
| 39 |
-
if not os.path.exists(output_path) or os.path.getsize(output_path) == 0:
|
| 40 |
-
raise RuntimeError("Output video file is missing or corrupted.")
|
| 41 |
-
|
| 42 |
-
return output_path
|
| 43 |
-
|
| 44 |
-
# Gradio UI
|
| 45 |
with gr.Blocks() as demo:
|
| 46 |
-
gr.Markdown("# YOLOv8 Object Detection
|
|
|
|
| 47 |
|
| 48 |
-
|
|
|
|
| 49 |
img_input = gr.Image(type="pil")
|
| 50 |
-
img_output = gr.Image()
|
| 51 |
img_btn = gr.Button("Run Detection")
|
| 52 |
img_btn.click(predict_image, inputs=img_input, outputs=img_output)
|
| 53 |
|
| 54 |
-
|
|
|
|
| 55 |
vid_input = gr.Video()
|
| 56 |
vid_output = gr.Video()
|
| 57 |
vid_btn = gr.Button("Run Detection on Video")
|
| 58 |
vid_btn.click(predict_video, inputs=vid_input, outputs=vid_output)
|
| 59 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 60 |
demo.launch()
|
|
|
|
| 2 |
from ultralytics import YOLO
|
| 3 |
import tempfile
|
| 4 |
import cv2
|
|
|
|
|
|
|
|
|
|
| 5 |
|
| 6 |
+
# Load YOLOv8 model once (you can use 'yolov8n.pt' for better speed if needed)
|
| 7 |
model = YOLO("best.pt")
|
| 8 |
|
| 9 |
+
# Inference on image
|
| 10 |
def predict_image(image):
|
| 11 |
+
results = model.predict(image, imgsz=480, conf=0.5, verbose=False)
|
| 12 |
return results[0].plot()
|
| 13 |
|
| 14 |
+
# Inference on video file
|
| 15 |
def predict_video(video_path):
|
| 16 |
cap = cv2.VideoCapture(video_path)
|
| 17 |
+
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 18 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)) // 2
|
| 19 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)) // 2
|
| 20 |
+
|
| 21 |
+
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 22 |
+
out = cv2.VideoWriter(temp_output.name, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
|
| 23 |
+
|
| 24 |
+
while True:
|
| 25 |
+
ret, frame = cap.read()
|
| 26 |
+
if not ret:
|
| 27 |
+
break
|
| 28 |
+
frame = cv2.resize(frame, (width, height))
|
| 29 |
+
results = model.predict(frame, imgsz=480, conf=0.5, verbose=False)
|
| 30 |
+
annotated = results[0].plot()
|
| 31 |
out.write(annotated)
|
| 32 |
|
| 33 |
cap.release()
|
| 34 |
out.release()
|
| 35 |
+
return temp_output.name
|
| 36 |
|
| 37 |
+
# Gradio Interface
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 38 |
with gr.Blocks() as demo:
|
| 39 |
+
gr.Markdown("# 🚀 YOLOv8 Object Detection")
|
| 40 |
+
gr.Markdown("Detect objects in images, videos, or live webcam feed using a YOLOv8 model.")
|
| 41 |
|
| 42 |
+
# Image detection tab
|
| 43 |
+
with gr.Tab("🖼️ Image Detection"):
|
| 44 |
img_input = gr.Image(type="pil")
|
| 45 |
+
img_output = gr.Image(label="Detected Objects")
|
| 46 |
img_btn = gr.Button("Run Detection")
|
| 47 |
img_btn.click(predict_image, inputs=img_input, outputs=img_output)
|
| 48 |
|
| 49 |
+
# Video detection tab
|
| 50 |
+
with gr.Tab("🎥 Video Detection"):
|
| 51 |
vid_input = gr.Video()
|
| 52 |
vid_output = gr.Video()
|
| 53 |
vid_btn = gr.Button("Run Detection on Video")
|
| 54 |
vid_btn.click(predict_video, inputs=vid_input, outputs=vid_output)
|
| 55 |
|
| 56 |
+
# Live webcam (browser-based) tab
|
| 57 |
+
with gr.Tab("📷 Live Webcam"):
|
| 58 |
+
gr.Markdown("This uses your browser webcam (works on Hugging Face Spaces).")
|
| 59 |
+
webcam_input = gr.Image(source="webcam", streaming=True, type="pil")
|
| 60 |
+
webcam_output = gr.Image()
|
| 61 |
+
webcam_input.change(fn=predict_image, inputs=webcam_input, outputs=webcam_output)
|
| 62 |
+
|
| 63 |
demo.launch()
|