BhumikaMak commited on
Commit
8978982
·
verified ·
1 Parent(s): 60b0ce7

fix: function args

Browse files
Files changed (1) hide show
  1. app.py +33 -43
app.py CHANGED
@@ -7,8 +7,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 = {
@@ -25,30 +23,34 @@ def load_sample_image(sample_name):
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
30
  else:
31
- image = load_sample_image(sample_choice) # Use selected sample image
32
 
 
33
  image = np.array(image)
34
  image = cv2.resize(image, (640, 640))
35
  result_images = []
36
 
 
37
  for yolo_version in yolo_versions:
38
  if yolo_version == "yolov5":
39
  result_images.append(xai_yolov5(image))
40
  elif yolo_version == "yolov8s":
41
  result_images.append(xai_yolov8s(image))
42
  else:
43
- result_images.append((Image.fromarray(image), f"{yolo_version} not yet implemented."))
44
 
45
  return result_images
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":
51
- iframe_html = f"""
52
  <iframe
53
  src="https://netron.app/?url=https://huggingface.co/FFusion/FFusionXL-BASE/blob/main/vae_encoder/model.onnx"
54
  width="100%"
@@ -56,8 +58,7 @@ def view_model(selected_models):
56
  frameborder="0">
57
  </iframe>
58
  """
59
- return iframe_html
60
- return "<p>Please select a valid model for Netron visualization.</p>"
61
 
62
  # Custom CSS for styling (optional)
63
  custom_css = """
@@ -73,15 +74,15 @@ custom_css = """
73
  with gr.Blocks(css=custom_css) as interface:
74
  gr.Markdown("# NeuralVista: Visualize Object Detection of Your Models")
75
 
 
76
  default_sample = "Sample 1"
77
 
78
  with gr.Row():
79
- # Left side: Sample selection and upload image
80
  with gr.Column():
81
  sample_selection = gr.Radio(
82
  choices=list(sample_images.keys()),
83
  label="Select a Sample Image",
84
- type="value",
85
  value=default_sample,
86
  )
87
 
@@ -104,66 +105,55 @@ with gr.Blocks(css=custom_css) as interface:
104
  label="Selected Sample Image",
105
  )
106
 
107
- # Below the sample image, display results and architecture side by side
108
  with gr.Row():
109
  result_gallery = gr.Gallery(
110
  label="Results",
111
- elem_id="gallery",
112
  rows=1,
113
  height=500,
114
  )
115
 
116
  netron_display = gr.HTML(label="Netron Visualization")
117
 
118
- # Update the sample image when the sample is changed
119
  sample_selection.change(
120
  fn=load_sample_image,
121
  inputs=sample_selection,
122
  outputs=sample_display,
123
  )
 
 
124
  def run_both(sample_choice, uploaded_image, selected_models):
125
  results = []
126
  netron_html = ""
127
 
128
- def update_results(res):
 
129
  nonlocal results
130
- results = res
131
- result_gallery.update(res)
132
 
133
- def update_netron(html):
 
134
  nonlocal netron_html
135
- netron_html = html
136
- netron_display.update(html)
137
-
138
- # Run both functions in parallel
139
- thread1 = threading.Thread(target=process_image, args=(sample_choice, uploaded_image, selected_models, update_results))
140
- thread2 = threading.Thread(target=view_model, args=(selected_models, update_netron))
141
- thread1.start()
142
- thread2.start()
143
- thread1.join()
144
- thread2.join()
145
- return results, netron_html
146
 
 
 
 
 
 
 
 
147
 
 
148
 
 
149
  run_button.click(
150
  fn=run_both,
151
  inputs=[sample_selection, upload_image, selected_models],
152
- outputs=[result_gallery,netron_display],
153
  )
154
- # run_button.click(
155
- # fn=lambda sample_choice, uploaded_image, yolo_versions: [
156
- # process_image(sample_choice, uploaded_image, yolo_versions), # Process image
157
- # view_model(yolo_versions) # Display model visualization
158
- # ],
159
- # inputs=[sample_selection, upload_image, selected_models],
160
- # outputs=[result_gallery, netron_display],
161
- # )
162
-
163
- # Laun
164
-
165
-
166
 
167
- # Launching Gradio app
168
  if __name__ == "__main__":
169
- interface.launch(share=True)
 
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 = {
 
23
 
24
  def process_image(sample_choice, uploaded_image, yolo_versions):
25
  """Process the image using selected YOLO models."""
26
+ # Load sample or uploaded image
27
  if uploaded_image is not None:
28
+ image = uploaded_image
29
  else:
30
+ image = load_sample_image(sample_choice)
31
 
32
+ # Preprocess image
33
  image = np.array(image)
34
  image = cv2.resize(image, (640, 640))
35
  result_images = []
36
 
37
+ # Apply selected models
38
  for yolo_version in yolo_versions:
39
  if yolo_version == "yolov5":
40
  result_images.append(xai_yolov5(image))
41
  elif yolo_version == "yolov8s":
42
  result_images.append(xai_yolov8s(image))
43
  else:
44
+ result_images.append((Image.fromarray(image), f"{yolo_version} not implemented."))
45
 
46
  return result_images
47
 
48
  def view_model(selected_models):
49
  """Generate Netron visualization for the selected models."""
50
+ netron_html = ""
51
  for model in selected_models:
52
  if model == "yolov5":
53
+ netron_html = f"""
54
  <iframe
55
  src="https://netron.app/?url=https://huggingface.co/FFusion/FFusionXL-BASE/blob/main/vae_encoder/model.onnx"
56
  width="100%"
 
58
  frameborder="0">
59
  </iframe>
60
  """
61
+ return netron_html if netron_html else "<p>No valid models selected for visualization.</p>"
 
62
 
63
  # Custom CSS for styling (optional)
64
  custom_css = """
 
74
  with gr.Blocks(css=custom_css) as interface:
75
  gr.Markdown("# NeuralVista: Visualize Object Detection of Your Models")
76
 
77
+ # Default sample
78
  default_sample = "Sample 1"
79
 
80
  with gr.Row():
81
+ # Left side: Sample selection and image upload
82
  with gr.Column():
83
  sample_selection = gr.Radio(
84
  choices=list(sample_images.keys()),
85
  label="Select a Sample Image",
 
86
  value=default_sample,
87
  )
88
 
 
105
  label="Selected Sample Image",
106
  )
107
 
108
+ # Results and visualization
109
  with gr.Row():
110
  result_gallery = gr.Gallery(
111
  label="Results",
 
112
  rows=1,
113
  height=500,
114
  )
115
 
116
  netron_display = gr.HTML(label="Netron Visualization")
117
 
118
+ # Update sample image
119
  sample_selection.change(
120
  fn=load_sample_image,
121
  inputs=sample_selection,
122
  outputs=sample_display,
123
  )
124
+
125
+ # Multi-threaded processing
126
  def run_both(sample_choice, uploaded_image, selected_models):
127
  results = []
128
  netron_html = ""
129
 
130
+ # Thread to process the image
131
+ def process_thread():
132
  nonlocal results
133
+ results = process_image(sample_choice, uploaded_image, selected_models)
 
134
 
135
+ # Thread to generate Netron visualization
136
+ def netron_thread():
137
  nonlocal netron_html
138
+ netron_html = view_model(selected_models)
 
 
 
 
 
 
 
 
 
 
139
 
140
+ # Launch threads
141
+ t1 = threading.Thread(target=process_thread)
142
+ t2 = threading.Thread(target=netron_thread)
143
+ t1.start()
144
+ t2.start()
145
+ t1.join()
146
+ t2.join()
147
 
148
+ return results, netron_html
149
 
150
+ # Run button click
151
  run_button.click(
152
  fn=run_both,
153
  inputs=[sample_selection, upload_image, selected_models],
154
+ outputs=[result_gallery, netron_display],
155
  )
 
 
 
 
 
 
 
 
 
 
 
 
156
 
157
+ # Launch Gradio interface
158
  if __name__ == "__main__":
159
+ interface.launch(share=True)