BhumikaMak commited on
Commit
b30ea65
·
1 Parent(s): 4613dc1

Update: layout refinement

Browse files
Files changed (1) hide show
  1. app.py +16 -92
app.py CHANGED
@@ -6,21 +6,7 @@ import torchvision.transforms as transforms
6
  import gradio as gr
7
  from yolov5 import xai_yolov5
8
  from yolov8 import xai_yolov8s
9
- """
10
- def process_image(image, yolo_versions=["yolov5"]):
11
- image = np.array(image)
12
- image = cv2.resize(image, (640, 640))
13
- result_images = []
14
- for yolo_version in yolo_versions:
15
- if yolo_version == "yolov5":
16
- result_images.append(xai_yolov5(image))
17
- elif yolo_version == "yolov8s":
18
- result_images.append(xai_yolov8s(image))
19
- else:
20
- result_images.append((Image.fromarray(image), f"{yolo_version} not yet implemented."))
21
- return result_images
22
 
23
- """
24
  sample_images = {
25
  "Sample 1": os.path.join(os.getcwd(), "data/xai/sample1.jpeg"),
26
  "Sample 2": os.path.join(os.getcwd(), "data/xai/sample2.jpg"),
@@ -32,33 +18,7 @@ def load_sample_image(sample_name):
32
  return None
33
 
34
  default_sample_image = load_sample_image("Sample 1")
35
- """
36
- with gr.Blocks() as interface:
37
- gr.Markdown("# XAI: Upload an image to visualize object detection of your models..")
38
- gr.Markdown("Upload an image or select a sample image to visualize object detection.")
39
-
40
- with gr.Row():
41
- uploaded_image = gr.Image(type="pil", label="Upload an Image")
42
- sample_selection = gr.Dropdown(
43
- choices=list(sample_images.keys()),
44
- label="Select a Sample Image",
45
- type="value",
46
- )
47
- sample_display = gr.Image(label="Sample Image Preview", value=default_sample_image)
48
- sample_selection.change(fn=load_sample_image, inputs=sample_selection, outputs=sample_display)
49
 
50
- selected_models = gr.CheckboxGroup(
51
- choices=["yolov5", "yolov8s"],
52
- value=["yolov5"],
53
- label="Select Model(s)",
54
- )
55
- result_gallery = gr.Gallery(label="Results", elem_id="gallery", rows=2, height=500)
56
- gr.Button("Run").click(
57
- fn=process_image,
58
- inputs=[uploaded_image, selected_models],
59
- outputs=result_gallery,
60
- )
61
- """
62
  def load_sample_image(choice):
63
  if choice in sample_images:
64
  image_path = sample_images[choice]
@@ -86,24 +46,31 @@ def process_image(sample_choice, uploaded_image, yolo_versions=["yolov5"]):
86
  result_images.append((Image.fromarray(image), f"{yolo_version} not yet implemented."))
87
  return result_images
88
 
89
-
90
- """
91
- import gradio as gr
92
  with gr.Blocks() as interface:
93
  gr.Markdown("# XAI: Visualize Object Detection of Your Models")
94
  gr.Markdown("Select a sample image to visualize object detection.")
95
  default_sample = "Sample 1"
96
  with gr.Row():
97
- sample_selection = gr.Radio(
98
- choices=list(sample_images.keys()),
99
- label="Select a Sample Image",
100
- type="value",
101
- value=default_sample, # Set default selection
102
- )
 
 
 
 
 
 
 
 
 
103
  sample_display = gr.Image(
104
  value=load_sample_image(default_sample),
105
  label="Selected Sample Image",
106
  )
 
107
  sample_selection.change(
108
  fn=load_sample_image,
109
  inputs=sample_selection,
@@ -117,49 +84,6 @@ with gr.Blocks() as interface:
117
  )
118
  result_gallery = gr.Gallery(label="Results", elem_id="gallery", rows=2, height=500)
119
 
120
- gr.Button("Run").click(
121
- fn=process_image,
122
- inputs=[sample_selection, selected_models],
123
- outputs=result_gallery,
124
- )
125
-
126
- interface.launch()
127
- """
128
- with gr.Blocks() as interface:
129
- gr.Markdown("# XAI: Visualize Object Detection of Your Models")
130
- gr.Markdown("Select a sample image to visualize object detection.")
131
- default_sample = "Sample 1"
132
- with gr.Row():
133
- sample_selection = gr.Radio(
134
- choices=list(sample_images.keys()),
135
- label="Select a Sample Image",
136
- type="value",
137
- value=default_sample, # Set default selection
138
- )
139
- sample_display = gr.Image(
140
- value=load_sample_image(default_sample),
141
- label="Selected Sample Image",
142
- )
143
- sample_selection.change(
144
- fn=load_sample_image,
145
- inputs=sample_selection,
146
- outputs=sample_display,
147
- )
148
-
149
- # Adding an upload placeholder
150
- gr.Markdown("**Or upload your own image:**")
151
- upload_image = gr.Image(
152
- label="Upload an Image",
153
- type="pil", # Corrected type for file path compatibility
154
- )
155
-
156
- selected_models = gr.CheckboxGroup(
157
- choices=["yolov5", "yolov8s"],
158
- value=["yolov5"],
159
- label="Select Model(s)",
160
- )
161
- result_gallery = gr.Gallery(label="Results", elem_id="gallery", rows=2, height=500)
162
-
163
  gr.Button("Run").click(
164
  fn=process_image,
165
  inputs=[sample_selection, upload_image, selected_models], # Include both options
 
6
  import gradio as gr
7
  from yolov5 import xai_yolov5
8
  from yolov8 import xai_yolov8s
 
 
 
 
 
 
 
 
 
 
 
 
 
9
 
 
10
  sample_images = {
11
  "Sample 1": os.path.join(os.getcwd(), "data/xai/sample1.jpeg"),
12
  "Sample 2": os.path.join(os.getcwd(), "data/xai/sample2.jpg"),
 
18
  return None
19
 
20
  default_sample_image = load_sample_image("Sample 1")
 
 
 
 
 
 
 
 
 
 
 
 
 
 
21
 
 
 
 
 
 
 
 
 
 
 
 
 
22
  def load_sample_image(choice):
23
  if choice in sample_images:
24
  image_path = sample_images[choice]
 
46
  result_images.append((Image.fromarray(image), f"{yolo_version} not yet implemented."))
47
  return result_images
48
 
 
 
 
49
  with gr.Blocks() as interface:
50
  gr.Markdown("# XAI: Visualize Object Detection of Your Models")
51
  gr.Markdown("Select a sample image to visualize object detection.")
52
  default_sample = "Sample 1"
53
  with gr.Row():
54
+ # Left side: Sample selection and upload image
55
+ with gr.Column():
56
+ sample_selection = gr.Radio(
57
+ choices=list(sample_images.keys()),
58
+ label="Select a Sample Image",
59
+ type="value",
60
+ value=default_sample, # Set default selection
61
+ )
62
+ # Upload image below sample selection
63
+ gr.Markdown("**Or upload your own image:**")
64
+ upload_image = gr.Image(
65
+ label="Upload an Image",
66
+ type="filepath", # Correct type for file path compatibility
67
+ )
68
+ # Right side: Selected sample image display
69
  sample_display = gr.Image(
70
  value=load_sample_image(default_sample),
71
  label="Selected Sample Image",
72
  )
73
+
74
  sample_selection.change(
75
  fn=load_sample_image,
76
  inputs=sample_selection,
 
84
  )
85
  result_gallery = gr.Gallery(label="Results", elem_id="gallery", rows=2, height=500)
86
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
87
  gr.Button("Run").click(
88
  fn=process_image,
89
  inputs=[sample_selection, upload_image, selected_models], # Include both options