import os os.environ.setdefault("YOLO_CONFIG_DIR", "/tmp/Ultralytics") import gradio as gr from ultralytics import YOLO import tempfile import cv2 import numpy as np model = YOLO("best.pt") def run_image_inference(img, conf=0.25): if img is None: return None if isinstance(img, np.ndarray): img_np = img.copy() else: img_np = np.array(img) # convert RGB -> BGR if img_np.ndim == 3 and img_np.shape[2] == 3: img_bgr = cv2.cvtColor(img_np, cv2.COLOR_RGB2BGR) else: img_bgr = img_np # *** التعديل هنا: إضافة iou=0.4 *** results = model(img_bgr, conf=conf, iou=0.4, verbose=False) annotated = results[0].plot() annotated_rgb = cv2.cvtColor(annotated, cv2.COLOR_BGR2RGB) out_path = tempfile.NamedTemporaryFile(suffix=".png", delete=False).name cv2.imwrite(out_path, cv2.cvtColor(annotated_rgb, cv2.COLOR_RGB2BGR)) return out_path def run_video_inference(video_path, conf=0.25): if video_path is None: return None cap = cv2.VideoCapture(video_path) fps = cap.get(cv2.CAP_PROP_FPS) if fps == 0 or fps is None or np.isnan(fps): fps = 25 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 fourcc = cv2.VideoWriter_fourcc(*'mp4v') out = cv2.VideoWriter(temp_out, fourcc, fps, (width, height)) while True: ret, frame = cap.read() if not ret: break # *** التعديل هنا: إضافة iou=0.4 *** results = model(frame, conf=conf, iou=0.4, verbose=False) annotated_frame = results[0].plot() out.write(annotated_frame) cap.release() out.release() return temp_out dark_css = """ """ with gr.Blocks() as demo: # inject custom CSS (safe fallback) gr.HTML(dark_css) gr.Markdown("# 🎯 YOLO Detection Studio — Image & Video") gr.Markdown("
Upload one item — image or video — then press Detect.
") with gr.Row(): with gr.Column(scale=2): tabs = gr.Tabs() with tabs: with gr.TabItem("Image"): image_input = gr.Image(type="pil", label="Upload image") img_conf = gr.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.01, label="Confidence threshold") img_detect_btn = gr.Button("🔍 Detect Image") image_output = gr.Image(label="Detected Image") image_download = gr.File(label="Download annotated image") with gr.TabItem("Video"): video_input = gr.Video(label="Upload video") vid_conf = gr.Slider(minimum=0.0, maximum=1.0, value=0.25, step=0.01, label="Confidence threshold") vid_detect_btn = gr.Button("🎬 Detect Video") video_output = gr.Video(label="Detected Video") video_download = gr.File(label="Download annotated video") with gr.Column(scale=1): gr.Markdown("### ⚙️ Options & Status") status = gr.Textbox(label="Status", value="Ready", interactive=False) clear_btn = gr.Button("🧹 Clear outputs") def on_detect_image(img, conf): try: status_val = "Processing image..." out = run_image_inference(img, conf=conf) status_val = "Done — image processed" return out, out, status_val except Exception as e: return None, None, f"Error: {e}" def on_detect_video(video, conf): try: status_val = "Processing video..." out = run_video_inference(video, conf=conf) status_val = "Done — video processed" return out, out, status_val except Exception as e: return None, None, f"Error: {e}" img_detect_btn.click(fn=on_detect_image, inputs=[image_input, img_conf], outputs=[image_output, image_download, status]) vid_detect_btn.click(fn=on_detect_video, inputs=[video_input, vid_conf], outputs=[video_output, video_download, status]) def on_clear(): return None, None, "Ready" clear_btn.click(fn=on_clear, inputs=None, outputs=[image_output, video_output, status]) demo.launch(server_name="0.0.0.0", share=False)