romiioooz's picture
Update app.py
8f03b1e verified
Raw History Blame Contribute Delete
1.46 kB
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()