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