Spaces:
Sleeping
Sleeping
File size: 1,455 Bytes
40e05ac 8f03b1e 40e05ac 8f03b1e 40e05ac 8f03b1e 40e05ac 8f03b1e 40e05ac | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 | import gradio as gr
from ultralytics import YOLO
import tempfile
import cv2
import os
# ๐ง ุชุญู
ูู ุงูู
ูุฏูู
model = YOLO("best.pt")
# ๐งฉ ุฏุงูุฉ ู
ุนุงูุฌุฉ ุงูููุฏูู
def detect_video(video_path):
try:
# ๐ ููุชุญ ุงูููุฏูู
cap = cv2.VideoCapture(video_path)
fps = cap.get(cv2.CAP_PROP_FPS)
width = int(cap.get(cv2.CAP_PROP_FRAME_WIDTH))
height = int(cap.get(cv2.CAP_PROP_FRAME_HEIGHT))
# ๐ ูุฌูุฒ ุงูููุฏูู ุงููุงุชุฌ
temp_out = tempfile.NamedTemporaryFile(suffix=".mp4", delete=False).name
out = cv2.VideoWriter(temp_out, cv2.VideoWriter_fourcc(*'mp4v'), fps, (width, height))
while True:
ret, frame = cap.read()
if not ret:
break
# ๐ ูุนู
ู detection ุนูู ุงููุฑูู
results = model(frame, verbose=False)
annotated_frame = results[0].plot()
out.write(annotated_frame)
cap.release()
out.release()
return temp_out
except Exception as e:
return f"Error: {e}"
# ๐จ ูุงุฌูุฉ Gradio
demo = gr.Interface(
fn=detect_video,
inputs=gr.Video(label="๐ฅ Upload a video"),
outputs=gr.Video(label="๐ฆ Detected video"),
title="YOLO Object Detection Demo ๐",
description="Upload a video and see YOLO detect objects frame by frame!"
)
if __name__ == "__main__":
demo.launch()
|