import os import sys import cv2 import gradio as gr import numpy as np from git import Repo from huggingface_hub import hf_hub_download # ---------------------------------------------------- # 1) تحميل مشروع YOLO11_EfficientNet من GitHub # (نفس الشيء الذي فعلته في كولاب) # ---------------------------------------------------- REPO_DIR = "YOLO11_EfficientNet" REPO_URL = "https://github.com/JYe9/YOLO11_EfficientNet.git" if not os.path.exists(REPO_DIR): Repo.clone_from(REPO_URL, REPO_DIR) # إضافة المسار في sys.path حتى نستورد ultralytics من داخل المشروع sys.path.insert(0, os.path.abspath(REPO_DIR)) from ultralytics import YOLO # الآن هذه من داخل الريبو، وليس من pip # ---------------------------------------------------- # 2) تحميل وزن الموديل best.pt من Hugging Face Hub # azizasyd/yolov11-el-fault-detector # ---------------------------------------------------- weights_path = hf_hub_download( repo_id="azizasyd/yolov11-el-fault-detector", filename="best.pt" ) model = YOLO(weights_path) # نفس ما عملت في الكولاب تماما # ---------------------------------------------------- # 3) دالة تحليل صورة واحدة (مثل analyze_single_image في كولاب) # Gradio سيستدعي هذه الدالة # ---------------------------------------------------- def analyze_image(image, conf_threshold): """ image: صورة EL-PV من المستخدم (numpy, RGB) conf_threshold: عتبة الثقة """ if image is None: return None, "No image uploaded." # Gradio يعطي الصورة RGB، نحول إلى BGR لاستخدام OpenCV/YOLO img_bgr = cv2.cvtColor(image, cv2.COLOR_RGB2BGR) h, w = img_bgr.shape[:2] image_area = w * h if w > 0 and h > 0 else 1 # تشغيل الموديل بنفس منطق الكولاب results = model(img_bgr, conf=float(conf_threshold)) res = results[0] # لو ما في أي بوكسات (لا يوجد عيوب) if len(res.boxes) == 0: text = "No defects detected ✅" return image, text total_defect_area = 0.0 defects_lines = [] # حساب مساحة كل عيب وتجميع المعلومات for box in res.boxes: x1, y1, x2, y2 = box.xyxy[0].tolist() cls_id = int(box.cls[0]) cls_name = model.names.get(cls_id, str(cls_id)) conf = float(box.conf[0]) defect_area = max(0.0, (x2 - x1)) * max(0.0, (y2 - y1)) total_defect_area += defect_area defects_lines.append( f"{cls_name} | conf={conf:.2f} | bbox=({x1:.1f}, {y1:.1f}, {x2:.1f}, {y2:.1f})" ) defect_ratio = total_defect_area / image_area defect_percent = defect_ratio * 100.0 summary_lines = [ f"Total defects: {len(res.boxes)}", f"Defect area ratio: {defect_ratio:.4f}", f"Defect area percent: {defect_percent:.2f} %", "", "Defects detail:", *defects_lines ] text_result = "\n".join(summary_lines) # الحصول على الصورة المعلّمة (annotated) من YOLO annotated_bgr = res.plot() # BGR annotated_rgb = cv2.cvtColor(annotated_bgr, cv2.COLOR_BGR2RGB) return annotated_rgb, text_result # ---------------------------------------------------- # 4) واجهة Gradio (الـ UI في الـ Space) # ---------------------------------------------------- with gr.Blocks() as demo: gr.Markdown( """ # 🔍 PV-EL Defect Detection – Fouad ارفع صورة EL لخلية أو لوح شمسي، وسيقوم الموديل (YOLO11_EfficientNet + best.pt من Hugging Face) بـ: - اكتشاف العيوب (black_core, crack, finger, horizontal_dislocation, short_circuit, star_crack, thick_line) - حساب نسبة مساحة الخلل من مساحة الصورة - إظهار عدد العيوب وأنواعها ومربعات التحديد على الصورة """ ) with gr.Row(): with gr.Column(): input_image = gr.Image( label="Upload EL-PV Image", type="numpy" ) conf_slider = gr.Slider( minimum=0.0001, maximum=0.90, value=0.25, step=0.01, label="Confidence Threshold" ) analyze_btn = gr.Button("Analyze") with gr.Column(): output_image = gr.Image( label="Annotated Image (with defects)", type="numpy" ) output_text = gr.Textbox( label="Defect Information", lines=12 ) analyze_btn.click( fn=analyze_image, inputs=[input_image, conf_slider], outputs=[output_image, output_text] ) demo.launch()