import netron import threading import gradio as gr import os from PIL import Image import cv2 import numpy as np from yolov5 import xai_yolov5 from yolov8 import xai_yolov8s # Sample images directory sample_images = { "Sample 1": os.path.join(os.getcwd(), "data/xai/sample1.jpeg"), "Sample 2": os.path.join(os.getcwd(), "data/xai/sample2.jpg"), } def load_sample_image(sample_name): """Load a sample image based on user selection.""" image_path = sample_images.get(sample_name) if image_path and os.path.exists(image_path): return Image.open(image_path) return None def process_image(sample_choice, uploaded_image, yolo_versions, target_lyr = -5, n_components = 8): """Process the image using selected YOLO models.""" # Load sample or uploaded image if uploaded_image is not None: image = uploaded_image else: image = load_sample_image(sample_choice) # Preprocess image image = np.array(image) image = cv2.resize(image, (640, 640)) result_images = [] # Apply selected models for yolo_version in yolo_versions: if yolo_version == "yolov5": result_images.append(xai_yolov5(image, target_lyr = -5, n_components = 8)) elif yolo_version == "yolov8s": result_images.append(xai_yolov8s(image)) else: result_images.append((Image.fromarray(image), f"{yolo_version} not implemented.")) return result_images def view_model(selected_models): """Generate Netron visualization for the selected models.""" netron_html = "" for model in selected_models: if model=="yolov8s": netron_html = f""" """ if model == "yolov5": netron_html = f""" """ return netron_html if netron_html else "
No valid models selected for visualization.
" # CSS to style the Gradio components and HTML content custom_css = """ body { background-color: #FFFAFO; /* Navy blue background */ background-image: linear-gradient(to right, transparent 39px, #a05252 1px, transparent 40px), /* Vertical dashed lines */ background-size: 1800px 1800px; /* Grid cell size */ height: 100%; /* Ensure body height is 100% of the viewport */ margin: 0; /* Remove default margin */ overflow-y: auto; /* Allow vertical scrolling if needed */ } .custom-row { display: flex; justify-content: center; /* Align horizontally */ align-items: center; /* Align vertically */ padding: 10px; /* Adjust as needed for spacing */ } .custom-button { background-color: #800000; color: white; font-size: 12px; /* Small font size */ width: 100px !important; /* Fixed width */ height: 35px !important; /* Fixed height */ border-radius: 6px; /* Slightly rounded corners */ padding: 0 !important; /* Remove extra padding */ cursor: pointer; text-align: center; margin: 0 auto; /* Center within its container */ box-sizing: border-box; /* Ensure consistent sizing */ } #run-button { background-color: #800000 !important; color: white !important; font-size: 12px !important; /* Small font size */ width: 100px !important; /* Fixed width */ height: 35px !important; /* Fixed height */ border-radius: 6px !important; padding: 0 !important; text-align: center !important; display: block !important; /* Ensure block-level alignment */ margin: 0 auto !important; /* Center horizontally */ box-sizing: border-box !important; } /* Custom border styles for all Gradio components */ .gradio-container, .gradio-row, .gradio-column, .gradio-input, .gradio-image, .gradio-checkgroup, .gradio-button, .gradio-markdown { border: 3px #800000 !important; /* Border width and color */ border-radius: 8px !important; /* Rounded corners */ } /* Additional customizations for images to enhance visibility of the border */ .gradio-image img { border-radius: 8px !important; border: 3px solid black !important; /* Border for image */ } /* Custom Row for images and buttons */ .custom-row img { border-radius: 10px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1); } #highlighted-text { font-weight: bold; color: #1976d2; } .gradio-block { max-height: 100vh; /* Allow scrolling within the Gradio blocks */ overflow-y: auto; /* Enable scrolling for the content if it overflows */ } #neural-vista-title { color: #800000 !important; /* Purple color for the title */ font-size: 32px; /* Adjust font size as needed */ font-weight: bold; text-align: center; } #neural-vista-text { color: #800000 !important; /* Purple color for the title */ font-size: 18px; /* Adjust font size as needed */ font-weight: bold; text-align: center; } """ # Then in the Gradio interface: with gr.Blocks(css=custom_css) as interface: gr.HTML("""