Spaces:
Sleeping
Sleeping
integrating public url
Browse files
app.py
CHANGED
@@ -1,4 +1,5 @@
|
|
1 |
-
|
|
|
2 |
import gradio as gr
|
3 |
import os
|
4 |
from PIL import Image
|
@@ -6,6 +7,10 @@ import cv2
|
|
6 |
import numpy as np
|
7 |
from yolov5 import xai_yolov5
|
8 |
from yolov8 import xai_yolov8s
|
|
|
|
|
|
|
|
|
9 |
|
10 |
# Sample images directory
|
11 |
sample_images = {
|
@@ -13,56 +18,73 @@ sample_images = {
|
|
13 |
"Sample 2": os.path.join(os.getcwd(), "data/xai/sample2.jpg"),
|
14 |
}
|
15 |
|
16 |
-
# Function to load sample image
|
17 |
def load_sample_image(sample_name):
|
|
|
18 |
image_path = sample_images.get(sample_name)
|
19 |
if image_path and os.path.exists(image_path):
|
20 |
return Image.open(image_path)
|
21 |
return None
|
22 |
|
23 |
-
# Function to process the image
|
24 |
def process_image(sample_choice, uploaded_image, yolo_versions):
|
25 |
-
|
26 |
if uploaded_image is not None:
|
27 |
-
image = uploaded_image
|
28 |
else:
|
29 |
-
image = load_sample_image(sample_choice)
|
30 |
|
31 |
-
# Resize and process the image
|
32 |
image = np.array(image)
|
33 |
image = cv2.resize(image, (640, 640))
|
34 |
result_images = []
|
35 |
|
36 |
for yolo_version in yolo_versions:
|
37 |
if yolo_version == "yolov5":
|
38 |
-
result_images.append(xai_yolov5(image))
|
39 |
elif yolo_version == "yolov8s":
|
40 |
result_images.append(xai_yolov8s(image))
|
41 |
else:
|
42 |
-
result_images.append((Image.fromarray(image), f"{yolo_version} not implemented."))
|
43 |
|
44 |
return result_images
|
45 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
46 |
# Custom CSS for styling (optional)
|
47 |
custom_css = """
|
48 |
-
|
49 |
-
|
50 |
-
|
51 |
-
|
52 |
-
|
53 |
-
|
54 |
-
|
55 |
"""
|
56 |
|
57 |
-
# Gradio UI
|
58 |
with gr.Blocks(css=custom_css) as interface:
|
59 |
gr.Markdown("# XAI: Visualize Object Detection of Your Models")
|
60 |
-
|
61 |
-
# Default sample
|
62 |
default_sample = "Sample 1"
|
63 |
|
64 |
with gr.Row():
|
65 |
-
# Left:
|
66 |
with gr.Column():
|
67 |
sample_selection = gr.Radio(
|
68 |
choices=list(sample_images.keys()),
|
@@ -71,7 +93,10 @@ with gr.Blocks(css=custom_css) as interface:
|
|
71 |
value=default_sample,
|
72 |
)
|
73 |
|
74 |
-
upload_image = gr.Image(
|
|
|
|
|
|
|
75 |
|
76 |
selected_models = gr.CheckboxGroup(
|
77 |
choices=["yolov5", "yolov8s"],
|
@@ -81,14 +106,13 @@ with gr.Blocks(css=custom_css) as interface:
|
|
81 |
|
82 |
run_button = gr.Button("Run", elem_id="run_button")
|
83 |
|
84 |
-
# Right: Display sample image
|
85 |
with gr.Column():
|
86 |
sample_display = gr.Image(
|
87 |
-
value=load_sample_image(default_sample),
|
88 |
label="Selected Sample Image",
|
89 |
)
|
90 |
|
91 |
-
#
|
92 |
with gr.Row():
|
93 |
result_gallery = gr.Gallery(
|
94 |
label="Results",
|
@@ -97,21 +121,28 @@ with gr.Blocks(css=custom_css) as interface:
|
|
97 |
height=500,
|
98 |
)
|
99 |
|
100 |
-
|
|
|
|
|
101 |
sample_selection.change(
|
102 |
fn=load_sample_image,
|
103 |
inputs=sample_selection,
|
104 |
outputs=sample_display,
|
105 |
)
|
106 |
|
107 |
-
# Process image
|
108 |
run_button.click(
|
109 |
fn=process_image,
|
110 |
inputs=[sample_selection, upload_image, selected_models],
|
111 |
outputs=[result_gallery],
|
112 |
)
|
113 |
|
114 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
115 |
if __name__ == "__main__":
|
116 |
interface.launch(share=True)
|
117 |
"""
|
@@ -157,12 +188,12 @@ def visualize_model(file):
|
|
157 |
if True:
|
158 |
# Embed the Netron viewer using an iframe with the generated URL
|
159 |
iframe_html = f"""
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
-
|
165 |
-
|
166 |
"""
|
167 |
return iframe_html
|
168 |
return "<p>Error: Unable to generate Netron visualization.</p>"
|
@@ -178,3 +209,4 @@ with gr.Blocks() as demo:
|
|
178 |
|
179 |
# Launch the Gradio app
|
180 |
demo.launch()
|
|
|
|
1 |
+
import netron
|
2 |
+
import threading
|
3 |
import gradio as gr
|
4 |
import os
|
5 |
from PIL import Image
|
|
|
7 |
import numpy as np
|
8 |
from yolov5 import xai_yolov5
|
9 |
from yolov8 import xai_yolov8s
|
10 |
+
import time
|
11 |
+
import tempfile
|
12 |
+
|
13 |
+
|
14 |
|
15 |
# Sample images directory
|
16 |
sample_images = {
|
|
|
18 |
"Sample 2": os.path.join(os.getcwd(), "data/xai/sample2.jpg"),
|
19 |
}
|
20 |
|
|
|
21 |
def load_sample_image(sample_name):
|
22 |
+
"""Load a sample image based on user selection."""
|
23 |
image_path = sample_images.get(sample_name)
|
24 |
if image_path and os.path.exists(image_path):
|
25 |
return Image.open(image_path)
|
26 |
return None
|
27 |
|
|
|
28 |
def process_image(sample_choice, uploaded_image, yolo_versions):
|
29 |
+
"""Process the image using selected YOLO models."""
|
30 |
if uploaded_image is not None:
|
31 |
+
image = uploaded_image # Use the uploaded image
|
32 |
else:
|
33 |
+
image = load_sample_image(sample_choice) # Use selected sample image
|
34 |
|
|
|
35 |
image = np.array(image)
|
36 |
image = cv2.resize(image, (640, 640))
|
37 |
result_images = []
|
38 |
|
39 |
for yolo_version in yolo_versions:
|
40 |
if yolo_version == "yolov5":
|
41 |
+
result_images.append(xai_yolov5(image))
|
42 |
elif yolo_version == "yolov8s":
|
43 |
result_images.append(xai_yolov8s(image))
|
44 |
else:
|
45 |
+
result_images.append((Image.fromarray(image), f"{yolo_version} not yet implemented."))
|
46 |
|
47 |
return result_images
|
48 |
|
49 |
+
|
50 |
+
def view_model(file):
|
51 |
+
if file:
|
52 |
+
file_path = file.name
|
53 |
+
#netron_url = start_netron_server(file_path)
|
54 |
+
if True:
|
55 |
+
# Embed the Netron viewer using an iframe with the generated URL
|
56 |
+
iframe_html = f"""
|
57 |
+
<iframe
|
58 |
+
src="https://netron.app/?url=https://huggingface.co/FFusion/FFusionXL-BASE/blob/main/vae_encoder/model.onnx"
|
59 |
+
width="100%"
|
60 |
+
height="800"
|
61 |
+
frameborder="0">
|
62 |
+
</iframe>
|
63 |
+
"""
|
64 |
+
return iframe_html
|
65 |
+
return "<p>Error: Unable to generate Netron visualization.</p>"
|
66 |
+
return "<p>Please upload a valid model file.</p>"
|
67 |
+
|
68 |
+
|
69 |
+
|
70 |
# Custom CSS for styling (optional)
|
71 |
custom_css = """
|
72 |
+
#run_button {
|
73 |
+
background-color: purple;
|
74 |
+
color: white;
|
75 |
+
width: 120px;
|
76 |
+
border-radius: 5px;
|
77 |
+
font-size: 14px;
|
78 |
+
}
|
79 |
"""
|
80 |
|
|
|
81 |
with gr.Blocks(css=custom_css) as interface:
|
82 |
gr.Markdown("# XAI: Visualize Object Detection of Your Models")
|
83 |
+
|
|
|
84 |
default_sample = "Sample 1"
|
85 |
|
86 |
with gr.Row():
|
87 |
+
# Left side: Sample selection and upload image
|
88 |
with gr.Column():
|
89 |
sample_selection = gr.Radio(
|
90 |
choices=list(sample_images.keys()),
|
|
|
93 |
value=default_sample,
|
94 |
)
|
95 |
|
96 |
+
upload_image = gr.Image(
|
97 |
+
label="Upload an Image",
|
98 |
+
type="pil",
|
99 |
+
)
|
100 |
|
101 |
selected_models = gr.CheckboxGroup(
|
102 |
choices=["yolov5", "yolov8s"],
|
|
|
106 |
|
107 |
run_button = gr.Button("Run", elem_id="run_button")
|
108 |
|
|
|
109 |
with gr.Column():
|
110 |
sample_display = gr.Image(
|
111 |
+
value=load_sample_image(default_sample),
|
112 |
label="Selected Sample Image",
|
113 |
)
|
114 |
|
115 |
+
# Below the sample image, display results and architecture side by side
|
116 |
with gr.Row():
|
117 |
result_gallery = gr.Gallery(
|
118 |
label="Results",
|
|
|
121 |
height=500,
|
122 |
)
|
123 |
|
124 |
+
model_file_input = gr.File(label="Upload Model File", type="filepath") # Fixed type here
|
125 |
+
netron_display = gr.HTML(label="Netron Visualization")
|
126 |
+
|
127 |
sample_selection.change(
|
128 |
fn=load_sample_image,
|
129 |
inputs=sample_selection,
|
130 |
outputs=sample_display,
|
131 |
)
|
132 |
|
|
|
133 |
run_button.click(
|
134 |
fn=process_image,
|
135 |
inputs=[sample_selection, upload_image, selected_models],
|
136 |
outputs=[result_gallery],
|
137 |
)
|
138 |
|
139 |
+
model_file_input.change(
|
140 |
+
fn=view_model,
|
141 |
+
inputs=model_file_input,
|
142 |
+
outputs=netron_display,
|
143 |
+
)
|
144 |
+
|
145 |
+
# Launching Gradio app and handling Netron visualization separately.
|
146 |
if __name__ == "__main__":
|
147 |
interface.launch(share=True)
|
148 |
"""
|
|
|
188 |
if True:
|
189 |
# Embed the Netron viewer using an iframe with the generated URL
|
190 |
iframe_html = f"""
|
191 |
+
# <iframe
|
192 |
+
# src="https://netron.app/?url=https://huggingface.co/FFusion/FFusionXL-BASE/blob/main/vae_encoder/model.onnx"
|
193 |
+
# width="100%"
|
194 |
+
# height="800"
|
195 |
+
# # frameborder="0">
|
196 |
+
# </iframe>
|
197 |
"""
|
198 |
return iframe_html
|
199 |
return "<p>Error: Unable to generate Netron visualization.</p>"
|
|
|
209 |
|
210 |
# Launch the Gradio app
|
211 |
demo.launch()
|
212 |
+
"""
|