zxcvb6958 commited on
Commit
53bdda3
·
1 Parent(s): 1983a93

update UI

Browse files
Files changed (1) hide show
  1. app.py +0 -242
app.py CHANGED
@@ -1,242 +0,0 @@
1
- import gradio as gr
2
- from PIL import Image
3
- import os
4
- import random
5
-
6
- # ========== 配置图片路径 ==========
7
- ENHANCE_INPUT_DIR = "sr_input"
8
- ENHANCE_OUTPUT_DIR = "sr_output"
9
- ENHANCE_IMG_NAMES = [f"img_{i}.jpg" for i in range(1, 30)]
10
- ENHANCE_CHOICES = ["请选择样例图片或者上传"] + ENHANCE_IMG_NAMES
11
-
12
- SEG_INPUT_DIR = "seg_input"
13
- SEG_OUTPUT_DIR = "seg_output"
14
- SEG_IMG_NAMES = [f"img_{i}.png" for i in range(1, 30)]
15
- SEG_CHOICES = ["请选择样例图片或者上传"] + SEG_IMG_NAMES
16
-
17
- DIAG_INPUT_DIR = "diag_input"
18
- DIAG_IMG_NAMES = [f"img_{i}.jpg" for i in range(1, 32)]
19
- DIAG_CHOICES = ["请选择样例图片或者上传"] + DIAG_IMG_NAMES
20
-
21
- KVASIR_CLASSES = [
22
- "正常粘膜 (normal-z-line)",
23
- "食管静脉曲张 (esophagitis)",
24
- "正常盲肠 (normal-cecum)",
25
- "息肉 (polyps)",
26
- "溃疡 (ulcerative-colitis)",
27
- "糜烂 (dyed-lifted-polyps)",
28
- "出血 (dyed-resection-margins)",
29
- "正常幽门 (normal-pylorus)"
30
- ]
31
-
32
- SUGGESTION_DICT = {
33
- "正常粘膜 (normal-z-line)": "建议:无需特殊治疗,常规随访。",
34
- "食管静脉曲张 (esophagitis)": "建议:根据分级考虑内镜下治疗或药物治疗。",
35
- "正常盲肠 (normal-cecum)": "建议:无需特殊治疗,常规随访。",
36
- "息肉 (polyps)": "建议:考虑内镜下息肉切除,并随访。",
37
- "溃疡 (ulcerative-colitis)": "建议:药物治疗,必要时内镜下活检。",
38
- "糜烂 (dyed-lifted-polyps)": "建议:药物保护胃黏膜,可考虑内镜下进一步处理。",
39
- "出血 (dyed-resection-margins)": "建议:内镜下止血处理,密切观察。",
40
- "正常幽门 (normal-pylorus)": "建议:无需特殊治疗,常规随访。"
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)
48
- w, h = img.size
49
- if max(w, h) != 300:
50
- if w >= h:
51
- new_w = 300
52
- new_h = int(h * 300 / w)
53
- else:
54
- new_h = 300
55
- new_w = int(w * 300 / h)
56
- img = img.resize((new_w, new_h), Image.LANCZOS)
57
- return img
58
-
59
- def show_enhance_input(img_name):
60
- if img_name in (None, "", "请选择样例图片或者上传"):
61
- return None
62
- path = os.path.join(ENHANCE_INPUT_DIR, img_name)
63
- return open_img(path)
64
-
65
- def enhance_demo(img_name):
66
- if img_name in (None, "", "请选择样例图片或者上传"):
67
- return None
68
- path = os.path.join(ENHANCE_OUTPUT_DIR, img_name)
69
- return open_img(path)
70
-
71
- def show_seg_input(img_name):
72
- if img_name in (None, "", "请选择样例图片或者上传"):
73
- return None
74
- path = os.path.join(SEG_INPUT_DIR, img_name)
75
- return open_img(path)
76
-
77
- def segment_demo(img_name):
78
- if img_name in (None, "", "请选择样例图片或者上传"):
79
- return None
80
- path = os.path.join(SEG_OUTPUT_DIR, img_name)
81
- return open_img(path)
82
-
83
- def show_diag_input(img_name):
84
- if img_name in (None, "", "请选择样例图片或者上传"):
85
- return None
86
- path = os.path.join(DIAG_INPUT_DIR, img_name)
87
- return open_img(path)
88
-
89
- def diagnose_demo(img_name):
90
- if img_name in (None, "", "请选择样例图片或者上传"):
91
- return [], "", ""
92
- idx = int(os.path.splitext(img_name)[0].replace("img_", ""))
93
- idx2cat = [
94
- (range(1, 5), "糜烂 (dyed-lifted-polyps)"),
95
- (range(5, 9), "出血 (dyed-resection-margins)"),
96
- (range(9, 13), "食管静脉曲张 (esophagitis)"),
97
- (range(13, 17), "正常盲肠 (normal-cecum)"),
98
- (range(17, 21), "正常幽门 (normal-pylorus)"),
99
- (range(21, 25), "正常粘膜 (normal-z-line)"),
100
- (range(25, 29), "息肉 (polyps)"),
101
- (range(29, 33), "溃疡 (ulcerative-colitis)")
102
- ]
103
- main_cat = None
104
- for r, cat in idx2cat:
105
- if idx in r:
106
- main_cat = cat
107
- break
108
- if main_cat is None:
109
- main_cat = random.choice(KVASIR_CLASSES)
110
- n_cat = len(KVASIR_CLASSES)
111
- probs = [0.0 for _ in range(n_cat)]
112
- main_idx = KVASIR_CLASSES.index(main_cat)
113
- main_prob = random.uniform(0.85, 0.99)
114
- rest = 1 - main_prob
115
- other_probs_raw = [random.uniform(0.01, 1) for _ in range(n_cat-1)]
116
- other_probs_norm = [x / sum(other_probs_raw) * rest for x in other_probs_raw]
117
- pidx = 0
118
- for i in range(n_cat):
119
- if i == main_idx:
120
- probs[i] = main_prob
121
- else:
122
- probs[i] = other_probs_norm[pidx]
123
- pidx += 1
124
- result_table = [[KVASIR_CLASSES[i], f"{probs[i]:.4f}"] for i in range(n_cat)]
125
- result_text = f"诊断类别:{main_cat}(概率最大)"
126
- suggestion = SUGGESTION_DICT.get(main_cat, "建议:请咨询医生。")
127
- return result_table, result_text, suggestion
128
-
129
- # --- CSS ---
130
- css = """
131
- .orange-btn {
132
- background: #FF7F1F !important;
133
- color: white !important;
134
- font-weight: bold;
135
- border: 2px solid #ff6100 !important;
136
- border-radius: 12px !important;
137
- font-size: 16px !important;
138
- padding: 10px 18px !important;
139
- margin-left: 10px !important;
140
- box-shadow: none !important;
141
- }
142
- .gray-btn {
143
- background: #EBEBEE !important;
144
- color: black !important;
145
- font-weight: bold;
146
- border: 2px solid #bfbfbf !important;
147
- border-radius: 12px !important;
148
- font-size: 16px !important;
149
- padding: 10px 18px !important;
150
- margin-right: 10px !important;
151
- box-shadow: none !important;
152
- }
153
- .button-row {
154
- margin-top: 4px;
155
- }
156
- .big-group {
157
- border: 2px solid #dadce0 !important;
158
- border-radius: 15px !important;
159
- padding: 28px 12px 24px 12px !important;
160
- margin-top: 16px !important;
161
- margin-bottom: 16px !important;
162
- background: #FAFAFA !important;
163
- box-shadow: 0 2px 6px 0 rgba(60,64,67,.11),0 1.5px 5px 0 rgba(60,64,67,.11);
164
- }
165
- .big-title {
166
- font-size: 22px;
167
- font-weight: bold;
168
- margin-bottom: 14px;
169
- margin-left: 6px;
170
- color: #ff7f1f;
171
- }
172
- """
173
-
174
- def reset_enhance():
175
- return "请选择样例图片或者上传", None, None
176
-
177
- def reset_seg():
178
- return "请选择样例图片或者上传", None, None
179
-
180
- def reset_diag():
181
- return "请选择样例图片或者上传", None, [], "", ""
182
-
183
- with gr.Blocks(title="AI内窥镜影像辅助分析系统") as demo:
184
- gr.HTML(f"<style>{css}</style>")
185
- gr.Markdown("<span style='font-size: 32px; font-weight: bold;'>AI内窥镜影像辅助分析系统</span>")
186
-
187
- # 第一行:增强&分割(大框包裹)
188
- with gr.Row():
189
- # 图像增强
190
- with gr.Column():
191
- with gr.Group(elem_classes="big-group"):
192
- gr.HTML(f"<div class='big-title'>图像增强模块</div>")
193
- enhance_select = gr.Dropdown(choices=ENHANCE_CHOICES, value="请选择样例图片或者上传", label="选择样例图片", filterable=True)
194
- with gr.Row():
195
- enhance_input_img = gr.Image(label="原始图片", interactive=False, width=300, height=300)
196
- enhance_output_img = gr.Image(label="增强结果图片", interactive=False, width=300, height=300)
197
- with gr.Row(elem_classes="button-row"):
198
- enhance_reset_btn = gr.Button("Reset", elem_classes="gray-btn")
199
- enhance_btn = gr.Button("点击执行图片增强", elem_classes="orange-btn")
200
- enhance_select.change(fn=show_enhance_input, inputs=enhance_select, outputs=enhance_input_img)
201
- enhance_btn.click(fn=enhance_demo, inputs=enhance_select, outputs=enhance_output_img)
202
- enhance_reset_btn.click(fn=reset_enhance, inputs=None, outputs=[enhance_select, enhance_input_img, enhance_output_img])
203
-
204
- # 图像分割
205
- with gr.Column():
206
- with gr.Group(elem_classes="big-group"):
207
- gr.HTML(f"<div class='big-title'>图像分割模块</div>")
208
- seg_select = gr.Dropdown(choices=SEG_CHOICES, value="请选择样例图片或者上传", label="选择样例图片", filterable=True)
209
- with gr.Row():
210
- seg_input_img = gr.Image(label="输入图片", interactive=False, width=300, height=300)
211
- seg_output_img = gr.Image(label="分割结果图片", interactive=False, width=300, height=300)
212
- with gr.Row(elem_classes="button-row"):
213
- seg_reset_btn = gr.Button("Reset", elem_classes="gray-btn")
214
- seg_btn = gr.Button("点击执行分割", elem_classes="orange-btn")
215
- seg_select.change(fn=show_seg_input, inputs=seg_select, outputs=seg_input_img)
216
- seg_btn.click(fn=segment_demo, inputs=seg_select, outputs=seg_output_img)
217
- seg_reset_btn.click(fn=reset_seg, inputs=None, outputs=[seg_select, seg_input_img, seg_output_img])
218
-
219
- # 第二行:疾病诊断(大框包裹)
220
- with gr.Row():
221
- with gr.Group(elem_classes="big-group"):
222
- gr.HTML(f"<div class='big-title'>疾病分类模块</div>")
223
- with gr.Row():
224
- # 1. 输入图片+按钮
225
- with gr.Column():
226
- diag_select = gr.Dropdown(choices=DIAG_CHOICES, value="请选择样例图片或者上传", label="选择样例图片", filterable=True)
227
- diag_input_img = gr.Image(label="输入图片", interactive=False, width=300, height=300)
228
- with gr.Row(elem_classes="button-row"):
229
- diag_reset_btn = gr.Button("Reset", elem_classes="gray-btn")
230
- diag_btn = gr.Button("点击执行诊断", elem_classes="orange-btn")
231
- diag_select.change(fn=show_diag_input, inputs=diag_select, outputs=diag_input_img)
232
- # 2. 结果表格
233
- with gr.Column():
234
- diag_table = gr.Dataframe(headers=["类别", "概率"])
235
- # 3. 预测与建议
236
- with gr.Column():
237
- diag_result = gr.Textbox(label="诊断结果")
238
- diag_suggestion = gr.Textbox(label="建议的治疗��案")
239
- diag_btn.click(fn=diagnose_demo, inputs=diag_select, outputs=[diag_table, diag_result, diag_suggestion])
240
- diag_reset_btn.click(fn=reset_diag, inputs=None, outputs=[diag_select, diag_input_img, diag_table, diag_result, diag_suggestion])
241
-
242
- demo.launch()