Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,73 +1,62 @@
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
| 3 |
-
import cv2
|
| 4 |
import tempfile
|
| 5 |
-
import
|
| 6 |
|
| 7 |
-
# Load YOLOv8 model once
|
| 8 |
-
model = YOLO("best.pt")
|
|
|
|
| 9 |
|
| 10 |
-
#
|
| 11 |
def predict_image(image):
|
| 12 |
-
results = model.predict(image, imgsz=
|
| 13 |
return results[0].plot()
|
| 14 |
|
| 15 |
-
#
|
| 16 |
def predict_video(video_path):
|
|
|
|
|
|
|
|
|
|
| 17 |
cap = cv2.VideoCapture(video_path)
|
| 18 |
-
fps = cap.get(cv2.CAP_PROP_FPS)
|
| 19 |
-
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH)
|
| 20 |
-
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT)
|
| 21 |
|
|
|
|
| 22 |
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
|
| 30 |
-
|
| 31 |
-
|
|
|
|
|
|
|
|
|
|
| 32 |
out.write(annotated)
|
| 33 |
|
| 34 |
cap.release()
|
| 35 |
out.release()
|
| 36 |
return temp_output.name
|
| 37 |
|
| 38 |
-
#
|
| 39 |
-
def live_feed():
|
| 40 |
-
cap = cv2.VideoCapture(0)
|
| 41 |
-
while True:
|
| 42 |
-
ret, frame = cap.read()
|
| 43 |
-
if not ret:
|
| 44 |
-
break
|
| 45 |
-
frame = cv2.resize(frame, (640, 480))
|
| 46 |
-
results = model.predict(frame, imgsz=320, conf=0.4, verbose=False)
|
| 47 |
-
annotated = results[0].plot()
|
| 48 |
-
yield annotated
|
| 49 |
-
cap.release()
|
| 50 |
-
|
| 51 |
-
# π Gradio App
|
| 52 |
with gr.Blocks() as demo:
|
| 53 |
-
gr.Markdown("#
|
| 54 |
-
gr.Markdown("Upload an image or video, or use your webcam for real-time detection.")
|
| 55 |
|
| 56 |
-
with gr.Tab("
|
| 57 |
img_input = gr.Image(type="pil")
|
| 58 |
-
img_output = gr.Image()
|
| 59 |
-
img_btn = gr.Button("
|
| 60 |
img_btn.click(predict_image, inputs=img_input, outputs=img_output)
|
| 61 |
|
| 62 |
-
with gr.Tab("
|
| 63 |
vid_input = gr.Video()
|
| 64 |
vid_output = gr.Video()
|
| 65 |
-
vid_btn = gr.Button("
|
| 66 |
vid_btn.click(predict_video, inputs=vid_input, outputs=vid_output)
|
| 67 |
|
| 68 |
-
|
| 69 |
-
gr.Markdown("Note: Webcam streaming may not work on Hugging Face Spaces.")
|
| 70 |
-
live_img = gr.Image(streaming=True)
|
| 71 |
-
live_img.stream(live_feed)
|
| 72 |
-
|
| 73 |
demo.launch()
|
|
|
|
| 1 |
import gradio as gr
|
| 2 |
from ultralytics import YOLO
|
|
|
|
| 3 |
import tempfile
|
| 4 |
+
import cv2
|
| 5 |
|
| 6 |
+
# β
Load YOLOv8 model once on GPU (if available)
|
| 7 |
+
model = YOLO("best.pt")
|
| 8 |
+
model.to("cuda" if model.device.type == "cpu" else model.device)
|
| 9 |
|
| 10 |
+
# β
Inference on image
|
| 11 |
def predict_image(image):
|
| 12 |
+
results = model.predict(image, imgsz=640, conf=0.5, verbose=False)
|
| 13 |
return results[0].plot()
|
| 14 |
|
| 15 |
+
# β
Optimized video detection
|
| 16 |
def predict_video(video_path):
|
| 17 |
+
import os
|
| 18 |
+
|
| 19 |
+
# Open input video
|
| 20 |
cap = cv2.VideoCapture(video_path)
|
| 21 |
+
fps = cap.get(cv2.CAP_PROP_FPS) or 25
|
| 22 |
+
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH) * 0.5)
|
| 23 |
+
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT) * 0.5)
|
| 24 |
|
| 25 |
+
# Create output video file
|
| 26 |
temp_output = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False)
|
| 27 |
+
fourcc = cv2.VideoWriter_fourcc(*'mp4v')
|
| 28 |
+
out = cv2.VideoWriter(temp_output.name, fourcc, fps, (width, height))
|
| 29 |
+
|
| 30 |
+
# Efficient streaming-based inference
|
| 31 |
+
for result in model.track(source=video_path, stream=True, imgsz=480, conf=0.5, verbose=False):
|
| 32 |
+
if result.orig_img is None:
|
| 33 |
+
continue
|
| 34 |
+
# Resize original image
|
| 35 |
+
frame = cv2.resize(result.orig_img, (width, height))
|
| 36 |
+
# Draw annotations
|
| 37 |
+
annotated = result.plot()
|
| 38 |
+
# Write to output video
|
| 39 |
out.write(annotated)
|
| 40 |
|
| 41 |
cap.release()
|
| 42 |
out.release()
|
| 43 |
return temp_output.name
|
| 44 |
|
| 45 |
+
# β
Gradio UI
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 46 |
with gr.Blocks() as demo:
|
| 47 |
+
gr.Markdown("# π YOLOv8 Fast Detection\nOptimized for Speed on Images & Videos")
|
|
|
|
| 48 |
|
| 49 |
+
with gr.Tab("Image"):
|
| 50 |
img_input = gr.Image(type="pil")
|
| 51 |
+
img_output = gr.Image(label="Detected")
|
| 52 |
+
img_btn = gr.Button("Run Detection")
|
| 53 |
img_btn.click(predict_image, inputs=img_input, outputs=img_output)
|
| 54 |
|
| 55 |
+
with gr.Tab("Video"):
|
| 56 |
vid_input = gr.Video()
|
| 57 |
vid_output = gr.Video()
|
| 58 |
+
vid_btn = gr.Button("Run Detection on Video")
|
| 59 |
vid_btn.click(predict_video, inputs=vid_input, outputs=vid_output)
|
| 60 |
|
| 61 |
+
# β
Launch Gradio
|
|
|
|
|
|
|
|
|
|
|
|
|
| 62 |
demo.launch()
|