Spaces:
Sleeping
Sleeping
concurrent execution
Browse files
app.py
CHANGED
|
@@ -1,14 +1,12 @@
|
|
| 1 |
import netron
|
| 2 |
-
import threading
|
| 3 |
import gradio as gr
|
| 4 |
import os
|
|
|
|
| 5 |
from PIL import Image
|
| 6 |
import cv2
|
| 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 |
# Sample images directory
|
| 14 |
sample_images = {
|
|
@@ -23,7 +21,7 @@ def load_sample_image(sample_name):
|
|
| 23 |
return Image.open(image_path)
|
| 24 |
return None
|
| 25 |
|
| 26 |
-
def process_image(sample_choice, uploaded_image, yolo_versions):
|
| 27 |
"""Process the image using selected YOLO models."""
|
| 28 |
if uploaded_image is not None:
|
| 29 |
image = uploaded_image # Use the uploaded image
|
|
@@ -42,9 +40,9 @@ def process_image(sample_choice, uploaded_image, yolo_versions):
|
|
| 42 |
else:
|
| 43 |
result_images.append((Image.fromarray(image), f"{yolo_version} not yet implemented."))
|
| 44 |
|
| 45 |
-
|
| 46 |
|
| 47 |
-
def view_model(selected_models):
|
| 48 |
"""Generate Netron visualization for the selected models."""
|
| 49 |
for model in selected_models:
|
| 50 |
if model == "yolov5":
|
|
@@ -56,8 +54,9 @@ def view_model(selected_models):
|
|
| 56 |
frameborder="0">
|
| 57 |
</iframe>
|
| 58 |
"""
|
| 59 |
-
|
| 60 |
-
|
|
|
|
| 61 |
|
| 62 |
# Custom CSS for styling (optional)
|
| 63 |
custom_css = """
|
|
@@ -122,16 +121,35 @@ with gr.Blocks(css=custom_css) as interface:
|
|
| 122 |
outputs=sample_display,
|
| 123 |
)
|
| 124 |
|
| 125 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 126 |
run_button.click(
|
| 127 |
-
fn=
|
| 128 |
-
process_image(sample_choice, uploaded_image, yolo_versions), # Process image
|
| 129 |
-
view_model(yolo_versions) # Display model visualization
|
| 130 |
-
],
|
| 131 |
inputs=[sample_selection, upload_image, selected_models],
|
| 132 |
-
outputs=[
|
| 133 |
)
|
| 134 |
|
| 135 |
# Launching Gradio app
|
| 136 |
if __name__ == "__main__":
|
| 137 |
-
interface.launch(share=True)
|
|
|
|
| 1 |
import netron
|
|
|
|
| 2 |
import gradio as gr
|
| 3 |
import os
|
| 4 |
+
import threading
|
| 5 |
from PIL import Image
|
| 6 |
import cv2
|
| 7 |
import numpy as np
|
| 8 |
from yolov5 import xai_yolov5
|
| 9 |
from yolov8 import xai_yolov8s
|
|
|
|
|
|
|
| 10 |
|
| 11 |
# Sample images directory
|
| 12 |
sample_images = {
|
|
|
|
| 21 |
return Image.open(image_path)
|
| 22 |
return None
|
| 23 |
|
| 24 |
+
def process_image(sample_choice, uploaded_image, yolo_versions, result_callback):
|
| 25 |
"""Process the image using selected YOLO models."""
|
| 26 |
if uploaded_image is not None:
|
| 27 |
image = uploaded_image # Use the uploaded image
|
|
|
|
| 40 |
else:
|
| 41 |
result_images.append((Image.fromarray(image), f"{yolo_version} not yet implemented."))
|
| 42 |
|
| 43 |
+
result_callback(result_images)
|
| 44 |
|
| 45 |
+
def view_model(selected_models, netron_callback):
|
| 46 |
"""Generate Netron visualization for the selected models."""
|
| 47 |
for model in selected_models:
|
| 48 |
if model == "yolov5":
|
|
|
|
| 54 |
frameborder="0">
|
| 55 |
</iframe>
|
| 56 |
"""
|
| 57 |
+
netron_callback(iframe_html)
|
| 58 |
+
return
|
| 59 |
+
netron_callback("<p>Please select a valid model for Netron visualization.</p>")
|
| 60 |
|
| 61 |
# Custom CSS for styling (optional)
|
| 62 |
custom_css = """
|
|
|
|
| 121 |
outputs=sample_display,
|
| 122 |
)
|
| 123 |
|
| 124 |
+
# Parallel execution with threading
|
| 125 |
+
def run_both(sample_choice, uploaded_image, selected_models):
|
| 126 |
+
results = []
|
| 127 |
+
netron_html = ""
|
| 128 |
+
|
| 129 |
+
def update_results(res):
|
| 130 |
+
nonlocal results
|
| 131 |
+
results = res
|
| 132 |
+
result_gallery.update(res)
|
| 133 |
+
|
| 134 |
+
def update_netron(html):
|
| 135 |
+
nonlocal netron_html
|
| 136 |
+
netron_html = html
|
| 137 |
+
netron_display.update(html)
|
| 138 |
+
|
| 139 |
+
# Run both functions in parallel
|
| 140 |
+
thread1 = threading.Thread(target=process_image, args=(sample_choice, uploaded_image, selected_models, update_results))
|
| 141 |
+
thread2 = threading.Thread(target=view_model, args=(selected_models, update_netron))
|
| 142 |
+
thread1.start()
|
| 143 |
+
thread2.start()
|
| 144 |
+
thread1.join()
|
| 145 |
+
thread2.join()
|
| 146 |
+
|
| 147 |
run_button.click(
|
| 148 |
+
fn=run_both,
|
|
|
|
|
|
|
|
|
|
| 149 |
inputs=[sample_selection, upload_image, selected_models],
|
| 150 |
+
outputs=[],
|
| 151 |
)
|
| 152 |
|
| 153 |
# Launching Gradio app
|
| 154 |
if __name__ == "__main__":
|
| 155 |
+
interface.launch(share=True)
|