zxcvb6958 commited on
Commit
4c9051a
·
1 Parent(s): fafdc60

update UI

Browse files
Files changed (1) hide show
  1. app.py +10 -45
app.py CHANGED
@@ -41,11 +41,7 @@ SUGGESTION_DICT = {
41
  }
42
 
43
  def open_img(img_path):
44
- """
45
- 将图等比缩放成 300x300,并返回 PIL.Image.Image
46
- """
47
  if not os.path.exists(img_path):
48
- # 若路径不存在,用灰色方块占位
49
  img = Image.new("RGB", (300, 300), (200, 200, 200))
50
  else:
51
  img = Image.open(img_path)
@@ -85,9 +81,6 @@ def show_diag_input(img_name):
85
  return open_img(path)
86
 
87
  def diagnose_demo(img_name):
88
- """
89
- 随机模拟诊断结果与概率分布
90
- """
91
  if img_name in (None, "", "请选择样例图片或者上传"):
92
  return [], "", ""
93
  idx = int(os.path.splitext(img_name)[0].replace("img_", ""))
@@ -107,34 +100,27 @@ def diagnose_demo(img_name):
107
  main_cat = cat
108
  break
109
  if main_cat is None:
110
- import random
111
  main_cat = random.choice(KVASIR_CLASSES)
112
-
113
- import random
114
  n_cat = len(KVASIR_CLASSES)
115
  probs = [0.0 for _ in range(n_cat)]
116
  main_idx = KVASIR_CLASSES.index(main_cat)
117
  main_prob = random.uniform(0.85, 0.99)
118
  rest = 1 - main_prob
119
  other_probs_raw = [random.uniform(0.01, 1) for _ in range(n_cat-1)]
120
- other_probs_sum = sum(other_probs_raw)
121
- other_probs = [x / other_probs_sum * rest for x in other_probs_raw]
122
  pidx = 0
123
  for i in range(n_cat):
124
  if i == main_idx:
125
  probs[i] = main_prob
126
  else:
127
- probs[i] = other_probs[pidx]
128
  pidx += 1
129
-
130
  result_table = [[KVASIR_CLASSES[i], f"{probs[i]:.4f}"] for i in range(n_cat)]
131
  result_text = f"诊断类别:{main_cat}(概率最大)"
132
  suggestion = SUGGESTION_DICT.get(main_cat, "建议:请咨询医生。")
133
  return result_table, result_text, suggestion
134
 
135
- # 自定义CSS,特别是 img-label-bar 用于在图片下方添加彩条
136
  css = """
137
- /* 主标题居中加大 */
138
  #main-title {
139
  text-align: center;
140
  font-size: 44px;
@@ -143,7 +129,6 @@ css = """
143
  margin-top: 18px;
144
  letter-spacing: 2px;
145
  }
146
-
147
  /* 模块外层深蓝虚线圆角 */
148
  .blue-dash-border {
149
  border: 2.5px dashed #164fa0 !important;
@@ -152,8 +137,6 @@ css = """
152
  margin-bottom: 32px !important;
153
  box-sizing: border-box;
154
  }
155
-
156
- /* 模块内卡片背景、阴影、圆角等 */
157
  .big-group {
158
  background: #F5F6F7 !important;
159
  border: 2px solid #e0e2e5 !important;
@@ -162,8 +145,6 @@ css = """
162
  margin: 16px !important;
163
  box-shadow: 0 3px 12px 2px rgba(60,64,67,.14);
164
  }
165
-
166
- /* 模块标题居中 */
167
  .big-title {
168
  font-size: 25px;
169
  font-weight: bold;
@@ -172,8 +153,6 @@ css = """
172
  letter-spacing: 1px;
173
  text-align: center;
174
  }
175
-
176
- /* 按钮样式(橙色无边框,灰色带黑色边框) */
177
  .orange-btn {
178
  background: #FF7F1F !important;
179
  color: white !important;
@@ -201,20 +180,15 @@ css = """
201
  .button-row {
202
  margin-top: 8px;
203
  }
204
-
205
- /* 图片圆角阴影 */
206
  .gr-image, .gr-image-preview {
207
  border-radius: 10px !important;
208
  background: #fff !important;
209
  box-shadow: 0 2px 12px 0 rgba(60,64,67,.08);
210
  }
211
- /* 禁止卡片内嵌套group的边框和阴影 */
212
  .gr-box .gr-block.gr-group {
213
  box-shadow: none;
214
  border: none;
215
  }
216
-
217
- /* 下方彩条,用于显示文字说明 */
218
  .img-label-bar {
219
  background: rgb(76, 180, 184);
220
  color: #fff;
@@ -222,7 +196,7 @@ css = """
222
  padding: 6px 0;
223
  border-bottom-left-radius: 10px;
224
  border-bottom-right-radius: 10px;
225
- margin-top: -5px; /* 让bar紧贴图片下边缘 */
226
  font-size: 14px;
227
  font-weight: normal;
228
  }
@@ -251,20 +225,15 @@ with gr.Blocks(title="消化道疾病智能分析系统") as demo:
251
  enhance_select = gr.Dropdown(choices=ENHANCE_CHOICES, value="请选择样例图片或者上传", label="选择样例图片", filterable=True)
252
  with gr.Row():
253
  with gr.Column():
254
- # 1) 不在 gr.Image 里加 label
255
  enhance_input_img = gr.Image(show_label=False, interactive=False, width=300, height=300)
256
- # 2) 自定义bar来显示 "原始图片"
257
  gr.HTML("<div class='img-label-bar'>原始图片</div>")
258
  with gr.Column():
259
  enhance_output_img = gr.Image(show_label=False, interactive=False, width=300, height=300)
260
  gr.HTML("<div class='img-label-bar'>增强结果图片</div>")
261
- # 按钮区域避免横向拉伸
262
  with gr.Row(elem_classes="button-row"):
263
- with gr.Column(scale=0):
264
- enhance_reset_btn = gr.Button("Reset", elem_classes="gray-btn")
265
- with gr.Column(scale=0):
266
- enhance_btn = gr.Button("点击执行图片增强", elem_classes="orange-btn")
267
- # 交互
268
  enhance_select.change(fn=show_enhance_input, inputs=enhance_select, outputs=enhance_input_img)
269
  enhance_btn.click(fn=enhance_demo, inputs=enhance_select, outputs=enhance_output_img)
270
  enhance_reset_btn.click(fn=reset_enhance, inputs=None, outputs=[enhance_select, enhance_input_img, enhance_output_img])
@@ -283,10 +252,8 @@ with gr.Blocks(title="消化道疾病智能分析系统") as demo:
283
  seg_output_img = gr.Image(show_label=False, interactive=False, width=300, height=300)
284
  gr.HTML("<div class='img-label-bar'>分割结果图片</div>")
285
  with gr.Row(elem_classes="button-row"):
286
- with gr.Column(scale=0):
287
- seg_reset_btn = gr.Button("Reset", elem_classes="gray-btn")
288
- with gr.Column(scale=0):
289
- seg_btn = gr.Button("点击执行分割", elem_classes="orange-btn")
290
  seg_select.change(fn=show_seg_input, inputs=seg_select, outputs=seg_input_img)
291
  seg_btn.click(fn=segment_demo, inputs=seg_select, outputs=seg_output_img)
292
  seg_reset_btn.click(fn=reset_seg, inputs=None, outputs=[seg_select, seg_input_img, seg_output_img])
@@ -304,10 +271,8 @@ with gr.Blocks(title="消化道疾病智能分析系统") as demo:
304
  diag_input_img = gr.Image(show_label=False, interactive=False, width=300, height=300)
305
  gr.HTML("<div class='img-label-bar'>输入图片</div>")
306
  with gr.Row(elem_classes="button-row"):
307
- with gr.Column(scale=0):
308
- diag_reset_btn = gr.Button("Reset", elem_classes="gray-btn")
309
- with gr.Column(scale=0):
310
- diag_btn = gr.Button("点击执行诊断", elem_classes="orange-btn")
311
  diag_select.change(fn=show_diag_input, inputs=diag_select, outputs=diag_input_img)
312
 
313
  # 中列:结果表格
 
41
  }
42
 
43
  def open_img(img_path):
 
 
 
44
  if not os.path.exists(img_path):
 
45
  img = Image.new("RGB", (300, 300), (200, 200, 200))
46
  else:
47
  img = Image.open(img_path)
 
81
  return open_img(path)
82
 
83
  def diagnose_demo(img_name):
 
 
 
84
  if img_name in (None, "", "请选择样例图片或者上传"):
85
  return [], "", ""
86
  idx = int(os.path.splitext(img_name)[0].replace("img_", ""))
 
100
  main_cat = cat
101
  break
102
  if main_cat is None:
 
103
  main_cat = random.choice(KVASIR_CLASSES)
 
 
104
  n_cat = len(KVASIR_CLASSES)
105
  probs = [0.0 for _ in range(n_cat)]
106
  main_idx = KVASIR_CLASSES.index(main_cat)
107
  main_prob = random.uniform(0.85, 0.99)
108
  rest = 1 - main_prob
109
  other_probs_raw = [random.uniform(0.01, 1) for _ in range(n_cat-1)]
110
+ other_probs_norm = [x / sum(other_probs_raw) * rest for x in other_probs_raw]
 
111
  pidx = 0
112
  for i in range(n_cat):
113
  if i == main_idx:
114
  probs[i] = main_prob
115
  else:
116
+ probs[i] = other_probs_norm[pidx]
117
  pidx += 1
 
118
  result_table = [[KVASIR_CLASSES[i], f"{probs[i]:.4f}"] for i in range(n_cat)]
119
  result_text = f"诊断类别:{main_cat}(概率最大)"
120
  suggestion = SUGGESTION_DICT.get(main_cat, "建议:请咨询医生。")
121
  return result_table, result_text, suggestion
122
 
 
123
  css = """
 
124
  #main-title {
125
  text-align: center;
126
  font-size: 44px;
 
129
  margin-top: 18px;
130
  letter-spacing: 2px;
131
  }
 
132
  /* 模块外层深蓝虚线圆角 */
133
  .blue-dash-border {
134
  border: 2.5px dashed #164fa0 !important;
 
137
  margin-bottom: 32px !important;
138
  box-sizing: border-box;
139
  }
 
 
140
  .big-group {
141
  background: #F5F6F7 !important;
142
  border: 2px solid #e0e2e5 !important;
 
145
  margin: 16px !important;
146
  box-shadow: 0 3px 12px 2px rgba(60,64,67,.14);
147
  }
 
 
148
  .big-title {
149
  font-size: 25px;
150
  font-weight: bold;
 
153
  letter-spacing: 1px;
154
  text-align: center;
155
  }
 
 
156
  .orange-btn {
157
  background: #FF7F1F !important;
158
  color: white !important;
 
180
  .button-row {
181
  margin-top: 8px;
182
  }
 
 
183
  .gr-image, .gr-image-preview {
184
  border-radius: 10px !important;
185
  background: #fff !important;
186
  box-shadow: 0 2px 12px 0 rgba(60,64,67,.08);
187
  }
 
188
  .gr-box .gr-block.gr-group {
189
  box-shadow: none;
190
  border: none;
191
  }
 
 
192
  .img-label-bar {
193
  background: rgb(76, 180, 184);
194
  color: #fff;
 
196
  padding: 6px 0;
197
  border-bottom-left-radius: 10px;
198
  border-bottom-right-radius: 10px;
199
+ margin-top: -5px;
200
  font-size: 14px;
201
  font-weight: normal;
202
  }
 
225
  enhance_select = gr.Dropdown(choices=ENHANCE_CHOICES, value="请选择样例图片或者上传", label="选择样例图片", filterable=True)
226
  with gr.Row():
227
  with gr.Column():
 
228
  enhance_input_img = gr.Image(show_label=False, interactive=False, width=300, height=300)
 
229
  gr.HTML("<div class='img-label-bar'>原始图片</div>")
230
  with gr.Column():
231
  enhance_output_img = gr.Image(show_label=False, interactive=False, width=300, height=300)
232
  gr.HTML("<div class='img-label-bar'>增强结果图片</div>")
233
+ # 按钮直接并排
234
  with gr.Row(elem_classes="button-row"):
235
+ enhance_reset_btn = gr.Button("清空", elem_classes="gray-btn")
236
+ enhance_btn = gr.Button("点击执行图片增强", elem_classes="orange-btn")
 
 
 
237
  enhance_select.change(fn=show_enhance_input, inputs=enhance_select, outputs=enhance_input_img)
238
  enhance_btn.click(fn=enhance_demo, inputs=enhance_select, outputs=enhance_output_img)
239
  enhance_reset_btn.click(fn=reset_enhance, inputs=None, outputs=[enhance_select, enhance_input_img, enhance_output_img])
 
252
  seg_output_img = gr.Image(show_label=False, interactive=False, width=300, height=300)
253
  gr.HTML("<div class='img-label-bar'>分割结果图片</div>")
254
  with gr.Row(elem_classes="button-row"):
255
+ seg_reset_btn = gr.Button("清空", elem_classes="gray-btn")
256
+ seg_btn = gr.Button("点击执行分割", elem_classes="orange-btn")
 
 
257
  seg_select.change(fn=show_seg_input, inputs=seg_select, outputs=seg_input_img)
258
  seg_btn.click(fn=segment_demo, inputs=seg_select, outputs=seg_output_img)
259
  seg_reset_btn.click(fn=reset_seg, inputs=None, outputs=[seg_select, seg_input_img, seg_output_img])
 
271
  diag_input_img = gr.Image(show_label=False, interactive=False, width=300, height=300)
272
  gr.HTML("<div class='img-label-bar'>输入图片</div>")
273
  with gr.Row(elem_classes="button-row"):
274
+ diag_reset_btn = gr.Button("清空", elem_classes="gray-btn")
275
+ diag_btn = gr.Button("点击执行诊断", elem_classes="orange-btn")
 
 
276
  diag_select.change(fn=show_diag_input, inputs=diag_select, outputs=diag_input_img)
277
 
278
  # 中列:结果表格