Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
@@ -1,14 +1,10 @@
|
|
1 |
import gradio as gr
|
2 |
-
import netron
|
3 |
import os
|
4 |
-
import threading
|
5 |
-
import time
|
6 |
from PIL import Image
|
7 |
import cv2
|
8 |
import numpy as np
|
9 |
from yolov5 import xai_yolov5
|
10 |
from yolov8 import xai_yolov8s
|
11 |
-
import requests
|
12 |
|
13 |
# Sample images directory
|
14 |
sample_images = {
|
@@ -16,9 +12,6 @@ sample_images = {
|
|
16 |
"Sample 2": os.path.join(os.getcwd(), "data/xai/sample2.jpg"),
|
17 |
}
|
18 |
|
19 |
-
# Preloaded model file path
|
20 |
-
preloaded_model_file = os.path.join(os.getcwd(), "weight_files/yolov5.onnx") # Example path
|
21 |
-
|
22 |
# Function to load sample image
|
23 |
def load_sample_image(sample_name):
|
24 |
image_path = sample_images.get(sample_name)
|
@@ -49,40 +42,6 @@ def process_image(sample_choice, uploaded_image, yolo_versions):
|
|
49 |
|
50 |
return result_images
|
51 |
|
52 |
-
# Start Netron backend
|
53 |
-
def start_netron_backend(model_file):
|
54 |
-
def serve_netron():
|
55 |
-
netron.start(model_file, address=("0.0.0.0", 8080), browse=False)
|
56 |
-
#netron.start(model_file, address="0.0.0.0:8080", browse=False) # Updated Netron arguments
|
57 |
-
|
58 |
-
# Launch Netron in a separate thread
|
59 |
-
threading.Thread(target=serve_netron, daemon=True).start()
|
60 |
-
|
61 |
-
# Wait until Netron server is ready
|
62 |
-
def wait_for_netron(url, timeout=10):
|
63 |
-
start_time = time.time()
|
64 |
-
while time.time() - start_time < timeout:
|
65 |
-
try:
|
66 |
-
response = requests.get(url)
|
67 |
-
if response.status_code == 200:
|
68 |
-
return True
|
69 |
-
except requests.ConnectionError:
|
70 |
-
time.sleep(0.5)
|
71 |
-
return False
|
72 |
-
|
73 |
-
# Check server readiness
|
74 |
-
wait_for_netron("http://localhost:8080/", timeout=15)
|
75 |
-
|
76 |
-
# View Netron model
|
77 |
-
def view_netron_model():
|
78 |
-
# Ensure model exists
|
79 |
-
if not os.path.exists(preloaded_model_file):
|
80 |
-
return "Model file not found."
|
81 |
-
|
82 |
-
# Start Netron backend
|
83 |
-
start_netron_backend(preloaded_model_file)
|
84 |
-
return gr.HTML('<iframe src="http://localhost:8080/" width="100%" height="600px"></iframe>')
|
85 |
-
|
86 |
# Custom CSS for styling (optional)
|
87 |
custom_css = """
|
88 |
run_button {
|
@@ -128,7 +87,7 @@ with gr.Blocks(css=custom_css) as interface:
|
|
128 |
label="Selected Sample Image",
|
129 |
)
|
130 |
|
131 |
-
# Results
|
132 |
with gr.Row():
|
133 |
result_gallery = gr.Gallery(
|
134 |
label="Results",
|
@@ -137,9 +96,6 @@ with gr.Blocks(css=custom_css) as interface:
|
|
137 |
height=500,
|
138 |
)
|
139 |
|
140 |
-
# Display Netron iframe
|
141 |
-
netron_display = gr.HTML(view_netron_model())
|
142 |
-
|
143 |
# Sample selection update
|
144 |
sample_selection.change(
|
145 |
fn=load_sample_image,
|
|
|
1 |
import gradio as gr
|
|
|
2 |
import os
|
|
|
|
|
3 |
from PIL import Image
|
4 |
import cv2
|
5 |
import numpy as np
|
6 |
from yolov5 import xai_yolov5
|
7 |
from yolov8 import xai_yolov8s
|
|
|
8 |
|
9 |
# Sample images directory
|
10 |
sample_images = {
|
|
|
12 |
"Sample 2": os.path.join(os.getcwd(), "data/xai/sample2.jpg"),
|
13 |
}
|
14 |
|
|
|
|
|
|
|
15 |
# Function to load sample image
|
16 |
def load_sample_image(sample_name):
|
17 |
image_path = sample_images.get(sample_name)
|
|
|
42 |
|
43 |
return result_images
|
44 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
45 |
# Custom CSS for styling (optional)
|
46 |
custom_css = """
|
47 |
run_button {
|
|
|
87 |
label="Selected Sample Image",
|
88 |
)
|
89 |
|
90 |
+
# Results
|
91 |
with gr.Row():
|
92 |
result_gallery = gr.Gallery(
|
93 |
label="Results",
|
|
|
96 |
height=500,
|
97 |
)
|
98 |
|
|
|
|
|
|
|
99 |
# Sample selection update
|
100 |
sample_selection.change(
|
101 |
fn=load_sample_image,
|