Spaces:
Sleeping
Sleeping
resolved server issue
Browse files
app.py
CHANGED
|
@@ -8,6 +8,7 @@ import cv2
|
|
| 8 |
import numpy as np
|
| 9 |
from yolov5 import xai_yolov5
|
| 10 |
from yolov8 import xai_yolov8s
|
|
|
|
| 11 |
|
| 12 |
# Sample images directory
|
| 13 |
sample_images = {
|
|
@@ -18,12 +19,14 @@ sample_images = {
|
|
| 18 |
# Preloaded model file path
|
| 19 |
preloaded_model_file = os.path.join(os.getcwd(), "weight_files/yolov5.onnx") # Example path
|
| 20 |
|
|
|
|
| 21 |
def load_sample_image(sample_name):
|
| 22 |
image_path = sample_images.get(sample_name)
|
| 23 |
if image_path and os.path.exists(image_path):
|
| 24 |
return Image.open(image_path)
|
| 25 |
return None
|
| 26 |
|
|
|
|
| 27 |
def process_image(sample_choice, uploaded_image, yolo_versions):
|
| 28 |
# Use uploaded or sample image
|
| 29 |
if uploaded_image is not None:
|
|
@@ -46,14 +49,30 @@ def process_image(sample_choice, uploaded_image, yolo_versions):
|
|
| 46 |
|
| 47 |
return result_images
|
| 48 |
|
|
|
|
| 49 |
def start_netron_backend(model_file):
|
| 50 |
def serve_netron():
|
| 51 |
netron.start(model_file, host='0.0.0.0', port=8080) # Start Netron on port 8080
|
| 52 |
-
|
| 53 |
# Launch Netron in a separate thread
|
| 54 |
threading.Thread(target=serve_netron, daemon=True).start()
|
| 55 |
-
time.sleep(2) # Allow server to initialize
|
| 56 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 57 |
def view_netron_model():
|
| 58 |
# Ensure model exists
|
| 59 |
if not os.path.exists(preloaded_model_file):
|
|
@@ -61,7 +80,9 @@ def view_netron_model():
|
|
| 61 |
|
| 62 |
# Start Netron backend
|
| 63 |
start_netron_backend(preloaded_model_file)
|
| 64 |
-
return gr.
|
|
|
|
|
|
|
| 65 |
|
| 66 |
# Custom CSS for styling (optional)
|
| 67 |
custom_css = """
|
|
@@ -117,7 +138,10 @@ with gr.Blocks(css=custom_css) as interface:
|
|
| 117 |
height=500,
|
| 118 |
)
|
| 119 |
|
| 120 |
-
|
|
|
|
|
|
|
|
|
|
| 121 |
|
| 122 |
# Sample selection update
|
| 123 |
sample_selection.change(
|
|
@@ -133,10 +157,6 @@ with gr.Blocks(css=custom_css) as interface:
|
|
| 133 |
outputs=[result_gallery],
|
| 134 |
)
|
| 135 |
|
| 136 |
-
# Netron model visualization
|
| 137 |
-
# interface.add_component(gr.HTML(view_netron_model))
|
| 138 |
-
# netron_display.value = view_netron_model()
|
| 139 |
-
|
| 140 |
# Launch Gradio app
|
| 141 |
if __name__ == "__main__":
|
| 142 |
interface.launch(share=True)
|
|
|
|
| 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 = {
|
|
|
|
| 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)
|
| 25 |
if image_path and os.path.exists(image_path):
|
| 26 |
return Image.open(image_path)
|
| 27 |
return None
|
| 28 |
|
| 29 |
+
# Function to process the image
|
| 30 |
def process_image(sample_choice, uploaded_image, yolo_versions):
|
| 31 |
# Use uploaded or sample image
|
| 32 |
if uploaded_image is not None:
|
|
|
|
| 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, host='0.0.0.0', port=8080) # Start Netron on port 8080
|
| 56 |
+
|
| 57 |
# Launch Netron in a separate thread
|
| 58 |
threading.Thread(target=serve_netron, daemon=True).start()
|
|
|
|
| 59 |
|
| 60 |
+
# Wait until Netron server is ready
|
| 61 |
+
def wait_for_netron(url, timeout=10):
|
| 62 |
+
start_time = time.time()
|
| 63 |
+
while time.time() - start_time < timeout:
|
| 64 |
+
try:
|
| 65 |
+
response = requests.get(url)
|
| 66 |
+
if response.status_code == 200:
|
| 67 |
+
return True
|
| 68 |
+
except requests.ConnectionError:
|
| 69 |
+
time.sleep(0.5)
|
| 70 |
+
return False
|
| 71 |
+
|
| 72 |
+
# Check server readiness
|
| 73 |
+
wait_for_netron("http://localhost:8080/")
|
| 74 |
+
|
| 75 |
+
# View Netron model
|
| 76 |
def view_netron_model():
|
| 77 |
# Ensure model exists
|
| 78 |
if not os.path.exists(preloaded_model_file):
|
|
|
|
| 80 |
|
| 81 |
# Start Netron backend
|
| 82 |
start_netron_backend(preloaded_model_file)
|
| 83 |
+
return gr.Markdown(
|
| 84 |
+
"### [View Model in Netron](http://localhost:8080/) - Click the link if Netron does not load properly."
|
| 85 |
+
)
|
| 86 |
|
| 87 |
# Custom CSS for styling (optional)
|
| 88 |
custom_css = """
|
|
|
|
| 138 |
height=500,
|
| 139 |
)
|
| 140 |
|
| 141 |
+
# Display Netron link
|
| 142 |
+
netron_display = gr.Markdown(
|
| 143 |
+
"### [View Model in Netron](http://localhost:8080/) - Click the link if Netron does not load properly."
|
| 144 |
+
)
|
| 145 |
|
| 146 |
# Sample selection update
|
| 147 |
sample_selection.change(
|
|
|
|
| 157 |
outputs=[result_gallery],
|
| 158 |
)
|
| 159 |
|
|
|
|
|
|
|
|
|
|
|
|
|
| 160 |
# Launch Gradio app
|
| 161 |
if __name__ == "__main__":
|
| 162 |
interface.launch(share=True)
|