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

update UI

Browse files
Files changed (1) hide show
  1. app.py +70 -21
app.py CHANGED
@@ -41,7 +41,11 @@ SUGGESTION_DICT = {
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,6 +85,9 @@ def show_diag_input(img_name):
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,28 +107,34 @@ def diagnose_demo(img_name):
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
- /* 主标题居中 且更大 */
125
  #main-title {
126
  text-align: center;
127
  font-size: 44px;
@@ -160,7 +173,7 @@ css = """
160
  text-align: center;
161
  }
162
 
163
- /* 按钮样式(宽度更小, 橙色无边框, 灰色黑色边框) */
164
  .orange-btn {
165
  background: #FF7F1F !important;
166
  color: white !important;
@@ -200,6 +213,19 @@ css = """
200
  box-shadow: none;
201
  border: none;
202
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
203
  """
204
 
205
  def reset_enhance():
@@ -221,14 +247,24 @@ with gr.Blocks(title="消化道疾病智能分析系统") as demo:
221
  with gr.Column():
222
  with gr.Group(elem_classes="blue-dash-border"):
223
  with gr.Group(elem_classes="big-group"):
224
- gr.HTML(f"<div class='big-title'>图像增强模块</div>")
225
  enhance_select = gr.Dropdown(choices=ENHANCE_CHOICES, value="请选择样例图片或者上传", label="选择样例图片", filterable=True)
226
  with gr.Row():
227
- enhance_input_img = gr.Image(label="原始图片", interactive=False, width=300, height=300)
228
- enhance_output_img = gr.Image(label="增强结果图片", interactive=False, width=300, height=300)
 
 
 
 
 
 
 
229
  with gr.Row(elem_classes="button-row"):
230
- enhance_reset_btn = gr.Button("Reset", elem_classes="gray-btn")
231
- enhance_btn = gr.Button("点击执行图片增强", elem_classes="orange-btn")
 
 
 
232
  enhance_select.change(fn=show_enhance_input, inputs=enhance_select, outputs=enhance_input_img)
233
  enhance_btn.click(fn=enhance_demo, inputs=enhance_select, outputs=enhance_output_img)
234
  enhance_reset_btn.click(fn=reset_enhance, inputs=None, outputs=[enhance_select, enhance_input_img, enhance_output_img])
@@ -237,14 +273,20 @@ with gr.Blocks(title="消化道疾病智能分析系统") as demo:
237
  with gr.Column():
238
  with gr.Group(elem_classes="blue-dash-border"):
239
  with gr.Group(elem_classes="big-group"):
240
- gr.HTML(f"<div class='big-title'>图像分割模块</div>")
241
  seg_select = gr.Dropdown(choices=SEG_CHOICES, value="请选择样例图片或者上传", label="选择样例图片", filterable=True)
242
  with gr.Row():
243
- seg_input_img = gr.Image(label="输入图片", interactive=False, width=300, height=300)
244
- seg_output_img = gr.Image(label="分割结果图片", interactive=False, width=300, height=300)
 
 
 
 
245
  with gr.Row(elem_classes="button-row"):
246
- seg_reset_btn = gr.Button("Reset", elem_classes="gray-btn")
247
- seg_btn = gr.Button("点击执行分割", elem_classes="orange-btn")
 
 
248
  seg_select.change(fn=show_seg_input, inputs=seg_select, outputs=seg_input_img)
249
  seg_btn.click(fn=segment_demo, inputs=seg_select, outputs=seg_output_img)
250
  seg_reset_btn.click(fn=reset_seg, inputs=None, outputs=[seg_select, seg_input_img, seg_output_img])
@@ -253,23 +295,30 @@ with gr.Blocks(title="消化道疾病智能分析系统") as demo:
253
  with gr.Row():
254
  with gr.Group(elem_classes="blue-dash-border"):
255
  with gr.Group(elem_classes="big-group"):
256
- gr.HTML(f"<div class='big-title'>疾病分类模块</div>")
257
  with gr.Row():
258
- # 1. 输入图片+按钮
259
  with gr.Column():
260
  diag_select = gr.Dropdown(choices=DIAG_CHOICES, value="请选择样例图片或者上传", label="选择样例图片", filterable=True)
261
- diag_input_img = gr.Image(label="输入图片", interactive=False, width=300, height=300)
 
 
262
  with gr.Row(elem_classes="button-row"):
263
- diag_reset_btn = gr.Button("Reset", elem_classes="gray-btn")
264
- diag_btn = gr.Button("点击执行诊断", elem_classes="orange-btn")
 
 
265
  diag_select.change(fn=show_diag_input, inputs=diag_select, outputs=diag_input_img)
266
- # 2. 结果表格
 
267
  with gr.Column():
268
  diag_table = gr.Dataframe(headers=["类别", "概率"])
269
- # 3. 预测与建议
 
270
  with gr.Column():
271
  diag_result = gr.Textbox(label="诊断结果")
272
  diag_suggestion = gr.Textbox(label="建议的治疗方案")
 
273
  diag_btn.click(fn=diagnose_demo, inputs=diag_select, outputs=[diag_table, diag_result, diag_suggestion])
274
  diag_reset_btn.click(fn=reset_diag, inputs=None, outputs=[diag_select, diag_input_img, diag_table, diag_result, diag_suggestion])
275
 
 
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
  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
  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;
 
173
  text-align: center;
174
  }
175
 
176
+ /* 按钮样式(橙色无边框灰色黑色边框) */
177
  .orange-btn {
178
  background: #FF7F1F !important;
179
  color: white !important;
 
213
  box-shadow: none;
214
  border: none;
215
  }
216
+
217
+ /* 下方彩条,用于显示文字说明 */
218
+ .img-label-bar {
219
+ background: rgb(76, 180, 184);
220
+ color: #fff;
221
+ text-align: center;
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
+ }
229
  """
230
 
231
  def reset_enhance():
 
247
  with gr.Column():
248
  with gr.Group(elem_classes="blue-dash-border"):
249
  with gr.Group(elem_classes="big-group"):
250
+ gr.HTML("<div class='big-title'>图像增强模块</div>")
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])
 
273
  with gr.Column():
274
  with gr.Group(elem_classes="blue-dash-border"):
275
  with gr.Group(elem_classes="big-group"):
276
+ gr.HTML("<div class='big-title'>图像分割模块</div>")
277
  seg_select = gr.Dropdown(choices=SEG_CHOICES, value="请选择样例图片或者上传", label="选择样例图片", filterable=True)
278
  with gr.Row():
279
+ with gr.Column():
280
+ seg_input_img = gr.Image(show_label=False, interactive=False, width=300, height=300)
281
+ gr.HTML("<div class='img-label-bar'>输入图片</div>")
282
+ with gr.Column():
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])
 
295
  with gr.Row():
296
  with gr.Group(elem_classes="blue-dash-border"):
297
  with gr.Group(elem_classes="big-group"):
298
+ gr.HTML("<div class='big-title'>疾病分类模块</div>")
299
  with gr.Row():
300
+ # 左列:输入图片+按钮
301
  with gr.Column():
302
  diag_select = gr.Dropdown(choices=DIAG_CHOICES, value="请选择样例图片或者上传", label="选择样例图片", filterable=True)
303
+ with gr.Column():
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
+ # 中列:结果表格
314
  with gr.Column():
315
  diag_table = gr.Dataframe(headers=["类别", "概率"])
316
+
317
+ # 右列:诊断结果 & 建议
318
  with gr.Column():
319
  diag_result = gr.Textbox(label="诊断结果")
320
  diag_suggestion = gr.Textbox(label="建议的治疗方案")
321
+
322
  diag_btn.click(fn=diagnose_demo, inputs=diag_select, outputs=[diag_table, diag_result, diag_suggestion])
323
  diag_reset_btn.click(fn=reset_diag, inputs=None, outputs=[diag_select, diag_input_img, diag_table, diag_result, diag_suggestion])
324