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()