Update app.py
Browse files
app.py
CHANGED
|
@@ -1,429 +1,431 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
-
from model import (
|
| 3 |
-
cls_predict,
|
| 4 |
-
det_predict,
|
| 5 |
-
seg_predict,
|
| 6 |
-
ALL_SEG_LABELS,
|
| 7 |
-
ALL_DET_LABELS,
|
| 8 |
-
ALL_SEG_COLOR_MAP,
|
| 9 |
-
ALL_CLS_LABELS
|
| 10 |
-
)
|
| 11 |
-
import os
|
| 12 |
-
import requests
|
| 13 |
-
from PIL import Image
|
| 14 |
-
from io import BytesIO
|
| 15 |
-
import time # 用于重试
|
| 16 |
-
|
| 17 |
-
# --- 配置 Logo 和版权信息 ---
|
| 18 |
-
LOGO_PATH = "logo/logo.png" # 请将您的 Logo 图片存放在此路径
|
| 19 |
-
# 🌟 更新:版权信息仅保留年份和权利声明
|
| 20 |
-
COPYRIGHT_TEXT = "© 2025 All Rights Reserved."
|
| 21 |
-
|
| 22 |
-
|
| 23 |
-
|
| 24 |
-
|
| 25 |
-
|
| 26 |
-
|
| 27 |
-
|
| 28 |
-
|
| 29 |
-
#
|
| 30 |
-
"http://images.cocodataset.org/val2017/
|
| 31 |
-
"http://images.cocodataset.org/val2017/
|
| 32 |
-
"http://images.cocodataset.org/val2017/
|
| 33 |
-
"http://images.cocodataset.org/
|
| 34 |
-
|
| 35 |
-
|
| 36 |
-
|
| 37 |
-
#
|
| 38 |
-
"http://images.cocodataset.org/val2017/
|
| 39 |
-
"http://images.cocodataset.org/val2017/
|
| 40 |
-
"http://images.cocodataset.org/
|
| 41 |
-
"http://images.cocodataset.org/train2017/
|
| 42 |
-
|
| 43 |
-
|
| 44 |
-
|
| 45 |
-
#
|
| 46 |
-
"http://images.cocodataset.org/val2017/
|
| 47 |
-
"http://images.cocodataset.org/
|
| 48 |
-
"http://images.cocodataset.org/train2017/
|
| 49 |
-
"http://images.cocodataset.org/train2017/
|
| 50 |
-
|
| 51 |
-
|
| 52 |
-
|
| 53 |
-
|
| 54 |
-
|
| 55 |
-
|
| 56 |
-
|
| 57 |
-
|
| 58 |
-
|
| 59 |
-
|
| 60 |
-
|
| 61 |
-
|
| 62 |
-
|
| 63 |
-
|
| 64 |
-
|
| 65 |
-
|
| 66 |
-
|
| 67 |
-
|
| 68 |
-
|
| 69 |
-
|
| 70 |
-
|
| 71 |
-
|
| 72 |
-
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
response
|
| 80 |
-
|
| 81 |
-
|
| 82 |
-
image
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
|
| 107 |
-
|
| 108 |
-
|
| 109 |
-
|
| 110 |
-
|
| 111 |
-
|
| 112 |
-
|
| 113 |
-
|
| 114 |
-
|
| 115 |
-
|
| 116 |
-
"
|
| 117 |
-
html_content = "<
|
| 118 |
-
|
| 119 |
-
|
| 120 |
-
|
| 121 |
-
|
| 122 |
-
|
| 123 |
-
|
| 124 |
-
|
| 125 |
-
|
| 126 |
-
|
| 127 |
-
<
|
| 128 |
-
|
| 129 |
-
|
| 130 |
-
|
| 131 |
-
|
| 132 |
-
|
| 133 |
-
|
| 134 |
-
|
| 135 |
-
|
| 136 |
-
|
| 137 |
-
|
| 138 |
-
|
| 139 |
-
|
| 140 |
-
|
| 141 |
-
|
| 142 |
-
|
| 143 |
-
|
| 144 |
-
|
| 145 |
-
|
| 146 |
-
|
| 147 |
-
|
| 148 |
-
|
| 149 |
-
|
| 150 |
-
|
| 151 |
-
|
| 152 |
-
|
| 153 |
-
|
| 154 |
-
|
| 155 |
-
|
| 156 |
-
|
| 157 |
-
|
| 158 |
-
|
| 159 |
-
|
| 160 |
-
|
| 161 |
-
|
| 162 |
-
|
| 163 |
-
|
| 164 |
-
|
| 165 |
-
|
| 166 |
-
|
| 167 |
-
|
| 168 |
-
|
| 169 |
-
|
| 170 |
-
|
| 171 |
-
|
| 172 |
-
|
| 173 |
-
|
| 174 |
-
|
| 175 |
-
|
| 176 |
-
|
| 177 |
-
|
| 178 |
-
|
| 179 |
-
|
| 180 |
-
|
| 181 |
-
|
| 182 |
-
|
| 183 |
-
|
| 184 |
-
|
| 185 |
-
|
| 186 |
-
|
| 187 |
-
|
| 188 |
-
|
| 189 |
-
|
| 190 |
-
|
| 191 |
-
|
| 192 |
-
|
| 193 |
-
|
| 194 |
-
|
| 195 |
-
|
| 196 |
-
|
| 197 |
-
|
| 198 |
-
|
| 199 |
-
|
| 200 |
-
|
| 201 |
-
|
| 202 |
-
|
| 203 |
-
|
| 204 |
-
|
| 205 |
-
|
| 206 |
-
|
| 207 |
-
|
| 208 |
-
|
| 209 |
-
|
| 210 |
-
|
| 211 |
-
|
| 212 |
-
|
| 213 |
-
|
| 214 |
-
|
| 215 |
-
|
| 216 |
-
|
| 217 |
-
|
| 218 |
-
|
| 219 |
-
|
| 220 |
-
|
| 221 |
-
|
| 222 |
-
|
| 223 |
-
|
| 224 |
-
|
| 225 |
-
|
| 226 |
-
|
| 227 |
-
|
| 228 |
-
|
| 229 |
-
|
| 230 |
-
|
| 231 |
-
|
| 232 |
-
|
| 233 |
-
|
| 234 |
-
|
| 235 |
-
|
| 236 |
-
|
| 237 |
-
|
| 238 |
-
|
| 239 |
-
|
| 240 |
-
|
| 241 |
-
|
| 242 |
-
|
| 243 |
-
|
| 244 |
-
|
| 245 |
-
|
| 246 |
-
|
| 247 |
-
|
| 248 |
-
|
| 249 |
-
|
| 250 |
-
|
| 251 |
-
|
| 252 |
-
|
| 253 |
-
|
| 254 |
-
|
| 255 |
-
|
| 256 |
-
gr.Markdown("
|
| 257 |
-
|
| 258 |
-
|
| 259 |
-
|
| 260 |
-
|
| 261 |
-
|
| 262 |
-
|
| 263 |
-
|
| 264 |
-
|
| 265 |
-
|
| 266 |
-
|
| 267 |
-
|
| 268 |
-
|
| 269 |
-
|
| 270 |
-
|
| 271 |
-
|
| 272 |
-
|
| 273 |
-
|
| 274 |
-
|
| 275 |
-
|
|
| 276 |
-
|
|
| 277 |
-
|
|
| 278 |
-
|
|
| 279 |
-
|
| 280 |
-
|
| 281 |
-
|
| 282 |
-
|
| 283 |
-
|
| 284 |
-
|
| 285 |
-
|
| 286 |
-
|
| 287 |
-
|
| 288 |
-
* **
|
| 289 |
-
|
| 290 |
-
|
| 291 |
-
|
| 292 |
-
* **
|
| 293 |
-
|
| 294 |
-
|
| 295 |
-
|
| 296 |
-
* **
|
| 297 |
-
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
-
|
| 302 |
-
|
| 303 |
-
|
| 304 |
-
|
| 305 |
-
|
| 306 |
-
|
| 307 |
-
|
| 308 |
-
|
| 309 |
-
|
| 310 |
-
|
| 311 |
-
|
| 312 |
-
|
| 313 |
-
|
| 314 |
-
|
| 315 |
-
|
| 316 |
-
|
| 317 |
-
|
| 318 |
-
|
| 319 |
-
|
| 320 |
-
|
| 321 |
-
|
| 322 |
-
|
| 323 |
-
|
| 324 |
-
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
cls_button.click(cls_predict, inputs=cls_input, outputs=cls_output)
|
| 330 |
-
|
| 331 |
-
# 2. 语义分割 Tab
|
| 332 |
-
with gr.TabItem("✂️ 语义分割 (SegFormer)"):
|
| 333 |
-
with gr.Row():
|
| 334 |
-
with gr.Column(scale=2):
|
| 335 |
-
seg_input = gr.Image(type='pil', label="输入图像")
|
| 336 |
-
seg_button = gr.Button("🚀 提交分割任务")
|
| 337 |
-
with gr.Column(scale=2):
|
| 338 |
-
seg_output = gr.Image(type='pil', label="分割结果 (叠加)")
|
| 339 |
-
with gr.Column(scale=1):
|
| 340 |
-
# 🌟 展示图例
|
| 341 |
-
gr.HTML(value=generate_legend_html(ALL_SEG_COLOR_MAP), scale=1)
|
| 342 |
-
|
| 343 |
-
#
|
| 344 |
-
gr.
|
| 345 |
-
|
| 346 |
-
|
| 347 |
-
|
| 348 |
-
|
| 349 |
-
|
| 350 |
-
|
| 351 |
-
|
| 352 |
-
|
| 353 |
-
|
| 354 |
-
|
| 355 |
-
|
| 356 |
-
|
| 357 |
-
|
| 358 |
-
|
| 359 |
-
|
| 360 |
-
|
| 361 |
-
|
| 362 |
-
|
| 363 |
-
|
| 364 |
-
|
| 365 |
-
|
| 366 |
-
|
| 367 |
-
|
| 368 |
-
|
| 369 |
-
|
| 370 |
-
|
| 371 |
-
|
| 372 |
-
|
| 373 |
-
|
| 374 |
-
|
| 375 |
-
|
| 376 |
-
|
| 377 |
-
|
| 378 |
-
|
| 379 |
-
|
| 380 |
-
|
| 381 |
-
|
| 382 |
-
|
| 383 |
-
|
| 384 |
-
|
| 385 |
-
# 🌟
|
| 386 |
-
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
<
|
| 409 |
-
|
| 410 |
-
<div
|
| 411 |
-
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
<
|
| 415 |
-
|
| 416 |
-
<
|
| 417 |
-
|
| 418 |
-
<
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
|
|
|
|
|
|
| 429 |
demo.launch(share=True, css=CUSTOM_CSS)
|
|
|
|
| 1 |
+
import gradio as gr
|
| 2 |
+
from model import (
|
| 3 |
+
cls_predict,
|
| 4 |
+
det_predict,
|
| 5 |
+
seg_predict,
|
| 6 |
+
ALL_SEG_LABELS,
|
| 7 |
+
ALL_DET_LABELS,
|
| 8 |
+
ALL_SEG_COLOR_MAP,
|
| 9 |
+
ALL_CLS_LABELS
|
| 10 |
+
)
|
| 11 |
+
import os
|
| 12 |
+
import requests
|
| 13 |
+
from PIL import Image
|
| 14 |
+
from io import BytesIO
|
| 15 |
+
import time # 用于重试
|
| 16 |
+
|
| 17 |
+
# --- 配置 Logo 和版权信息 ---
|
| 18 |
+
LOGO_PATH = "logo/logo.png" # 请将您的 Logo 图片存放在此路径
|
| 19 |
+
# 🌟 更新:版权信息仅保留年份和权利声明,具体内容在 HTML 中处理
|
| 20 |
+
COPYRIGHT_TEXT = "© 2025 All Rights Reserved."
|
| 21 |
+
SCHOOL_NAME_EN = "School of Information Engineering, Wuhan University of Technology"
|
| 22 |
+
|
| 23 |
+
# --- 自动下载示例图片逻辑 (嵌入) ---
|
| 24 |
+
|
| 25 |
+
# 🌟 恢复到用户指定的 COCO ID 列表
|
| 26 |
+
TASK_EXAMPLE_URLS = {
|
| 27 |
+
"cls": [
|
| 28 |
+
# 图像分类示例 (val2017)
|
| 29 |
+
"http://images.cocodataset.org/val2017/000000000285.jpg", # 猫和键盘 (保留)
|
| 30 |
+
"http://images.cocodataset.org/val2017/000000000785.jpg", # 交通灯/汽车/巴士 (保留)
|
| 31 |
+
"http://images.cocodataset.org/val2017/000000000724.jpg", # 食物/餐具 (保留)
|
| 32 |
+
"http://images.cocodataset.org/val2017/000000001584.jpg", # 多人,多物体 (保留)
|
| 33 |
+
"http://images.cocodataset.org/train2017/000000001097.jpg", # 原 cls_5
|
| 34 |
+
],
|
| 35 |
+
"seg": [
|
| 36 |
+
# 语义分割示例 (val2017)
|
| 37 |
+
"http://images.cocodataset.org/val2017/000000000139.jpg", # 街景/汽车 (保留)
|
| 38 |
+
"http://images.cocodataset.org/val2017/000000000632.jpg", # 街景/行人 (保留)
|
| 39 |
+
"http://images.cocodataset.org/val2017/000000000885.jpg", # 滑板手 (保留)
|
| 40 |
+
"http://images.cocodataset.org/train2017/000000000267.jpg", # 原 seg_4
|
| 41 |
+
"http://images.cocodataset.org/train2017/000000001140.jpg", # 原 seg_5
|
| 42 |
+
],
|
| 43 |
+
"det": [
|
| 44 |
+
# 目标检测示例 (val2017)
|
| 45 |
+
"http://images.cocodataset.org/val2017/000000000785.jpg", # 交通灯/汽车/巴士 (保留)
|
| 46 |
+
"http://images.cocodataset.org/val2017/000000001268.jpg", # 原 det_2
|
| 47 |
+
"http://images.cocodataset.org/train2017/000000001072.jpg", # 原 det_3
|
| 48 |
+
"http://images.cocodataset.org/train2017/000000000119.jpg", # 原 det_4
|
| 49 |
+
"http://images.cocodataset.org/train2017/000000000570.jpg", # 原 det_5
|
| 50 |
+
]
|
| 51 |
+
}
|
| 52 |
+
|
| 53 |
+
# 项目期望的本地路径
|
| 54 |
+
OUTPUT_DIR = "examples"
|
| 55 |
+
|
| 56 |
+
|
| 57 |
+
def download_and_save_examples(max_retries=3):
|
| 58 |
+
"""下载示例图片到本地 examples/ 目录,使用任务前缀命名,增加重试机制"""
|
| 59 |
+
if not os.path.exists(OUTPUT_DIR):
|
| 60 |
+
os.makedirs(OUTPUT_DIR)
|
| 61 |
+
|
| 62 |
+
total_urls = sum(len(urls) for urls in TASK_EXAMPLE_URLS.values())
|
| 63 |
+
print(f"🚀 检查和下载 {total_urls} 张示例图片...")
|
| 64 |
+
|
| 65 |
+
# 迭代所有任务和 URL
|
| 66 |
+
for prefix, urls in TASK_EXAMPLE_URLS.items():
|
| 67 |
+
for i, url in enumerate(urls):
|
| 68 |
+
# 文件名格式:cls_1.jpg, seg_2.jpg, det_3.jpg
|
| 69 |
+
filename = f"{prefix}_{i + 1}.jpg"
|
| 70 |
+
filepath = os.path.join(OUTPUT_DIR, filename)
|
| 71 |
+
|
| 72 |
+
if os.path.exists(filepath):
|
| 73 |
+
continue # 跳过已存在的文件
|
| 74 |
+
|
| 75 |
+
for attempt in range(max_retries):
|
| 76 |
+
try:
|
| 77 |
+
# 增加更长的超时时间
|
| 78 |
+
response = requests.get(url, stream=True, timeout=15)
|
| 79 |
+
response.raise_for_status()
|
| 80 |
+
|
| 81 |
+
image = Image.open(BytesIO(response.content))
|
| 82 |
+
image.save(filepath)
|
| 83 |
+
print(f" 成功下载并保存: {filename} (尝试 {attempt + 1}/{max_retries})")
|
| 84 |
+
break # 成功则跳出重试循环
|
| 85 |
+
|
| 86 |
+
except requests.exceptions.RequestException as e:
|
| 87 |
+
print(f" ⚠️ 下载 {filename} 失败 (尝试 {attempt + 1}/{max_retries}): {e}")
|
| 88 |
+
if attempt < max_retries - 1:
|
| 89 |
+
time.sleep(2) # 失败后等待2秒再重试
|
| 90 |
+
else:
|
| 91 |
+
# 404 错误是 ClientError,意味着 URL 不存在
|
| 92 |
+
if '404 Client Error' in str(e):
|
| 93 |
+
print(f"❌ 最终下载失败 {filename}: URL {url} 不存在 (404 错误)。")
|
| 94 |
+
else:
|
| 95 |
+
print(f"❌ 最终下载失败 {filename}: 请检查网络连接或 URL。")
|
| 96 |
+
break
|
| 97 |
+
except Exception as e:
|
| 98 |
+
# 图像处理失败 (如 BytesIO 或 PIL 错误),停止重试
|
| 99 |
+
print(f"❌ 图像处理失败 {filename}: {e}")
|
| 100 |
+
break
|
| 101 |
+
|
| 102 |
+
# 立即执行下载,确保 examples 目录下的文件存在
|
| 103 |
+
|
| 104 |
+
|
| 105 |
+
download_and_save_examples()
|
| 106 |
+
|
| 107 |
+
# 🌟 关键:创建三个独立的示例列表,用于 Gradio Examples 组件
|
| 108 |
+
CLS_EXAMPLES = [[os.path.join(OUTPUT_DIR, f"cls_{i + 1}.jpg")] for i in range(5)]
|
| 109 |
+
SEG_EXAMPLES = [[os.path.join(OUTPUT_DIR, f"seg_{i + 1}.jpg")] for i in range(5)]
|
| 110 |
+
DET_EXAMPLES = [[os.path.join(OUTPUT_DIR, f"det_{i + 1}.jpg")] for i in range(5)]
|
| 111 |
+
|
| 112 |
+
|
| 113 |
+
# --- 辅助函数:生成颜色图例 HTML ---
|
| 114 |
+
def generate_legend_html(color_map_dict):
|
| 115 |
+
"""根据颜色映射字典生成 HTML 图例"""
|
| 116 |
+
html_content = "<div style='max-height: 300px; overflow-y: scroll; padding: 10px; border: 1px solid #ccc; background-color: #f7f7f7; border-radius: 8px;'>"
|
| 117 |
+
html_content += "<h4 style='margin-top: 0; color: #333;'>🎨 分割颜色图例</h4>"
|
| 118 |
+
|
| 119 |
+
if "Error" in color_map_dict:
|
| 120 |
+
html_content += "<p style='color: red;'>模型加载失败,图例不可用。</p>"
|
| 121 |
+
return html_content
|
| 122 |
+
|
| 123 |
+
for label, hex_color in color_map_dict.items():
|
| 124 |
+
html_content += f"""
|
| 125 |
+
<div style='display: flex; align-items: center; margin-bottom: 5px; font-family: sans-serif;'>
|
| 126 |
+
<div style='width: 15px; height: 15px; background-color: {hex_color}; border: 1px solid #333; margin-right: 10px; border-radius: 3px;'></div>
|
| 127 |
+
<span style='font-size: 14px; color: #555;'>{label}</span>
|
| 128 |
+
</div>
|
| 129 |
+
"""
|
| 130 |
+
html_content += "</div>"
|
| 131 |
+
return html_content
|
| 132 |
+
|
| 133 |
+
|
| 134 |
+
# --- 辅助函数:类别搜索逻辑 ---
|
| 135 |
+
def search_labels(query: str, all_labels) -> str:
|
| 136 |
+
"""
|
| 137 |
+
在标签列表或字典中搜索给定的查询。
|
| 138 |
+
all_labels 可以是 list (分类/分割) 或 dict (检测)。
|
| 139 |
+
"""
|
| 140 |
+
query = query.strip().lower()
|
| 141 |
+
if not query:
|
| 142 |
+
return "请输入有效的查询内容。"
|
| 143 |
+
|
| 144 |
+
MAX_MATCHES = 10
|
| 145 |
+
|
| 146 |
+
# 处理 List (Classification, Segmentation)
|
| 147 |
+
if isinstance(all_labels, list):
|
| 148 |
+
found_matches = [label for label in all_labels if query in label.lower()]
|
| 149 |
+
|
| 150 |
+
if found_matches:
|
| 151 |
+
result_list = "\n- ".join(found_matches[:MAX_MATCHES])
|
| 152 |
+
summary = f"✅ 找到 {len(found_matches)} 个匹配项 (仅显示前 {MAX_MATCHES} 个):"
|
| 153 |
+
return f"{summary}\n- {result_list}"
|
| 154 |
+
else:
|
| 155 |
+
return f"❌ 未找到包含 '{query}' 的类别。"
|
| 156 |
+
|
| 157 |
+
# 处理 Dict (Detection)
|
| 158 |
+
elif isinstance(all_labels, dict):
|
| 159 |
+
found_matches = {k: v for k, v in all_labels.items() if query in v.lower()}
|
| 160 |
+
|
| 161 |
+
if found_matches:
|
| 162 |
+
result_list = [f"ID {k}: {v}" for k, v in list(found_matches.items())[:MAX_MATCHES]]
|
| 163 |
+
summary = f"✅ 找到 {len(found_matches)} 个匹配项 (仅显示前 {MAX_MATCHES} 个):"
|
| 164 |
+
return f"{summary}\n- {result_list}"
|
| 165 |
+
else:
|
| 166 |
+
return f"❌ 未找到包含 '{query}' 的类别。"
|
| 167 |
+
|
| 168 |
+
return "类别数据格式错误或未加载。"
|
| 169 |
+
|
| 170 |
+
|
| 171 |
+
# 🌟 美化 1: Gradio Soft 主题 CSS (背景颜色调整)
|
| 172 |
+
CUSTOM_CSS = """
|
| 173 |
+
/* 整体背景和卡片阴影优化 */
|
| 174 |
+
.gradio-container {
|
| 175 |
+
background-color: #f7f7f7; /* 调整为柔和的浅灰色背景 */
|
| 176 |
+
font-family: 'Inter', system-ui, sans-serif;
|
| 177 |
+
}
|
| 178 |
+
/* 卡片和主内容区的美化 */
|
| 179 |
+
.gradio-container > div {
|
| 180 |
+
border-radius: 12px;
|
| 181 |
+
box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05); /* 柔和阴影 */
|
| 182 |
+
transition: all 0.3s ease;
|
| 183 |
+
}
|
| 184 |
+
|
| 185 |
+
/* 主标题样式 (用于承载 Logo 和文本) */
|
| 186 |
+
h1 {
|
| 187 |
+
display: flex;
|
| 188 |
+
align-items: center;
|
| 189 |
+
justify-content: center;
|
| 190 |
+
font-size: 2.2em;
|
| 191 |
+
color: #333333; /* 深色文字 */
|
| 192 |
+
padding: 20px 0;
|
| 193 |
+
margin: 0;
|
| 194 |
+
}
|
| 195 |
+
|
| 196 |
+
/* 按钮和输入框圆角 */
|
| 197 |
+
.gr-button, .gr-textbox, .gr-number, .gr-image {
|
| 198 |
+
border-radius: 8px !important;
|
| 199 |
+
}
|
| 200 |
+
|
| 201 |
+
/* 标签和组件背景 */
|
| 202 |
+
.gradio-container > div:not(.prose):not(.gr-row) {
|
| 203 |
+
background: white;
|
| 204 |
+
padding: 15px;
|
| 205 |
+
}
|
| 206 |
+
/* 页脚 Logo 样式 */
|
| 207 |
+
.footer-logo-container {
|
| 208 |
+
display: flex;
|
| 209 |
+
flex-direction: column;
|
| 210 |
+
align-items: center;
|
| 211 |
+
justify-content: center;
|
| 212 |
+
text-align: center;
|
| 213 |
+
padding-top: 15px;
|
| 214 |
+
}
|
| 215 |
+
|
| 216 |
+
/* 页脚图标/链接容器样式 */
|
| 217 |
+
.footer-links {
|
| 218 |
+
margin-top: 10px;
|
| 219 |
+
font-size: 14px;
|
| 220 |
+
color: #555;
|
| 221 |
+
display: flex;
|
| 222 |
+
gap: 15px; /* 图标之间的间隔 */
|
| 223 |
+
align-items: center;
|
| 224 |
+
/* 确保内容居中 */
|
| 225 |
+
justify-content: center;
|
| 226 |
+
}
|
| 227 |
+
|
| 228 |
+
/* 图标样式 */
|
| 229 |
+
.footer-icon {
|
| 230 |
+
font-size: 18px;
|
| 231 |
+
vertical-align: middle;
|
| 232 |
+
}
|
| 233 |
+
|
| 234 |
+
/* 链接颜色 */
|
| 235 |
+
.footer-links a {
|
| 236 |
+
color: #555;
|
| 237 |
+
text-decoration: none;
|
| 238 |
+
}
|
| 239 |
+
.footer-links a:hover {
|
| 240 |
+
color: #333;
|
| 241 |
+
}
|
| 242 |
+
"""
|
| 243 |
+
|
| 244 |
+
with gr.Blocks(
|
| 245 |
+
title="AI基础模型视觉任务演示平台",
|
| 246 |
+
) as demo:
|
| 247 |
+
# 注入 Favicon (网页选项卡图标)
|
| 248 |
+
gr.HTML(f"""
|
| 249 |
+
<head>
|
| 250 |
+
<link rel='icon' type='image/png' href='file/{LOGO_PATH}'/>
|
| 251 |
+
</head>
|
| 252 |
+
""", visible=False)
|
| 253 |
+
|
| 254 |
+
# 🌟 主标题区域:恢复机器人图标
|
| 255 |
+
gr.Markdown("<h1>🤖 AI基础模型视觉任务演示平台</h1>")
|
| 256 |
+
gr.Markdown("---")
|
| 257 |
+
|
| 258 |
+
# 🌟 简化功能说明 (只关注使用方法)
|
| 259 |
+
with gr.Accordion("📚 功能说明"):
|
| 260 |
+
gr.Markdown("""
|
| 261 |
+
本平台支持图像分类、语义分割和目标检测三大任务。
|
| 262 |
+
您可以通过以下步骤使用平台:
|
| 263 |
+
1. **切换选项卡**:选择您希望执行的 AI 任务。
|
| 264 |
+
2. **上传或选择图片**:上传您自己的图片或点击下方的示例图片。
|
| 265 |
+
3. **设置参数**:对于目标检测,调整置信度阈值。
|
| 266 |
+
4. **点击提交**:点击“提交任务”按钮,查看 AI 分析结果。
|
| 267 |
+
""")
|
| 268 |
+
|
| 269 |
+
# 🌟 新增:数据集介绍
|
| 270 |
+
with gr.Accordion("📚 基础数据集介绍", open=False):
|
| 271 |
+
gr.Markdown("""
|
| 272 |
+
### 📖 模型训练数据集概览
|
| 273 |
+
|
| 274 |
+
| 任务 | 模型 | 数据集 | 类别数 | 简介 |
|
| 275 |
+
| :--- | :--- | :--- | :--- | :--- |
|
| 276 |
+
| 图像分类 | ViT | **ImageNet-1K** | 1000 | 包含超过 100 万张图像,是图像识别领域的标准基准。 |
|
| 277 |
+
| 语义分割 | SegFormer | **ADE20K** | 150 | 专注于场景解析,提供 150 种语义概念的像素级标注。 |
|
| 278 |
+
| 目标检测 | YOLOv8n | **COCO** | 80 | 最常用的目标检测数据集之一,包含大量物体实例。 |
|
| 279 |
+
""")
|
| 280 |
+
|
| 281 |
+
# 🌟 新增:网络结构介绍
|
| 282 |
+
with gr.Accordion("🧠 网络结构介绍", open=False):
|
| 283 |
+
gr.Markdown("""
|
| 284 |
+
### 💻 模型架构说明
|
| 285 |
+
|
| 286 |
+
1. **图像分类 (ViT):**
|
| 287 |
+
* **全称:** Vision Transformer (ViT-Base-Patch16-224)
|
| 288 |
+
* **特点:** 基于 Transformer 结构,将图像切片后进行序列输入,通过自注意力机制实现全局建模。
|
| 289 |
+
|
| 290 |
+
2. **语义分割 (SegFormer):**
|
| 291 |
+
* **全称:** Segmentation Transformer
|
| 292 |
+
* **特点:** 高效的 Transformer 架构,使用轻量级解码器,专注于速度和准确性的平衡。
|
| 293 |
+
|
| 294 |
+
3. **目标检测 (YOLOv8n):**
|
| 295 |
+
* **全称:** You Only Look Once, Version 8 (Nano)
|
| 296 |
+
* **特点:** 单阶段检测器,以速度著称,Nano (n) 版本在保持高性能的同时,体积最小。
|
| 297 |
+
""")
|
| 298 |
+
|
| 299 |
+
# --- 任务选项卡 ---
|
| 300 |
+
with gr.Tabs():
|
| 301 |
+
# 1. 图像分类 Tab
|
| 302 |
+
with gr.TabItem("🖼️ 图像分类 (ViT)"):
|
| 303 |
+
with gr.Row():
|
| 304 |
+
with gr.Column(scale=1):
|
| 305 |
+
cls_input = gr.Image(type='pil', label="输入图像")
|
| 306 |
+
cls_button = gr.Button("🚀 提交分类任务")
|
| 307 |
+
with gr.Column(scale=1):
|
| 308 |
+
cls_output = gr.Label(num_top_classes=5, label="分类结果 (前 5)")
|
| 309 |
+
|
| 310 |
+
# 🌟 调整��序:Examples 先于 类别列表/查询 UI
|
| 311 |
+
gr.Examples(examples=CLS_EXAMPLES, inputs=[cls_input], label="示例图片")
|
| 312 |
+
|
| 313 |
+
# 🌟 展示所有分类类别列表
|
| 314 |
+
gr.Markdown("### 🌟 模型支持的全部分类类别 (ImageNet-1K)")
|
| 315 |
+
cls_category_json = gr.JSON(value=ALL_CLS_LABELS, label="所有类别列表", scale=1)
|
| 316 |
+
|
| 317 |
+
# 🌟 查询 UI
|
| 318 |
+
with gr.Row():
|
| 319 |
+
cls_search_query = gr.Textbox(label="查询类别", placeholder="Search Class Name (e.g., dog)", scale=3)
|
| 320 |
+
cls_search_button = gr.Button("🔍 搜索", scale=1)
|
| 321 |
+
cls_search_output = gr.Markdown("搜索结果将显示在这里。")
|
| 322 |
+
|
| 323 |
+
cls_search_button.click(
|
| 324 |
+
fn=search_labels,
|
| 325 |
+
inputs=[cls_search_query, cls_category_json],
|
| 326 |
+
outputs=cls_search_output
|
| 327 |
+
)
|
| 328 |
+
|
| 329 |
+
cls_button.click(cls_predict, inputs=cls_input, outputs=cls_output)
|
| 330 |
+
|
| 331 |
+
# 2. 语义分割 Tab
|
| 332 |
+
with gr.TabItem("✂️ 语义分割 (SegFormer)"):
|
| 333 |
+
with gr.Row():
|
| 334 |
+
with gr.Column(scale=2):
|
| 335 |
+
seg_input = gr.Image(type='pil', label="输入图像")
|
| 336 |
+
seg_button = gr.Button("🚀 提交分割任务")
|
| 337 |
+
with gr.Column(scale=2):
|
| 338 |
+
seg_output = gr.Image(type='pil', label="分割结果 (叠加)")
|
| 339 |
+
with gr.Column(scale=1):
|
| 340 |
+
# 🌟 展示图例
|
| 341 |
+
gr.HTML(value=generate_legend_html(ALL_SEG_COLOR_MAP), scale=1)
|
| 342 |
+
|
| 343 |
+
# 🌟 调整顺序:Examples 先于 类别列表/查询 UI
|
| 344 |
+
gr.Examples(examples=SEG_EXAMPLES, inputs=[seg_input], label="示例图片")
|
| 345 |
+
|
| 346 |
+
# 保留完整的类别列表(以 JSON 格式展示,作为额外的参考)
|
| 347 |
+
gr.Markdown("### 完整类别列表 (JSON)")
|
| 348 |
+
seg_category_json = gr.JSON(value={f"ID {i}": label for i, label in enumerate(ALL_SEG_LABELS)},
|
| 349 |
+
label="所有类别 JSON")
|
| 350 |
+
|
| 351 |
+
# 🌟 查询 UI 提示改为英文
|
| 352 |
+
with gr.Row():
|
| 353 |
+
seg_search_query = gr.Textbox(label="查询类别", placeholder="Search Class Name (e.g., road, sky)",
|
| 354 |
+
scale=3)
|
| 355 |
+
seg_search_button = gr.Button("🔍 搜索", scale=1)
|
| 356 |
+
seg_search_output = gr.Markdown("搜索结果将显示在这里。")
|
| 357 |
+
|
| 358 |
+
seg_search_button.click(
|
| 359 |
+
fn=search_labels,
|
| 360 |
+
inputs=[seg_search_query, seg_category_json],
|
| 361 |
+
outputs=seg_search_output
|
| 362 |
+
)
|
| 363 |
+
|
| 364 |
+
seg_button.click(seg_predict, inputs=seg_input, outputs=seg_output)
|
| 365 |
+
|
| 366 |
+
# 3. 目标检测 Tab
|
| 367 |
+
with gr.TabItem("🎯 目标检测 (YOLOv8n)"):
|
| 368 |
+
with gr.Row():
|
| 369 |
+
with gr.Column(scale=1):
|
| 370 |
+
det_input_image = gr.Image(type='pil', label="输入图像")
|
| 371 |
+
det_input_number = gr.Number(
|
| 372 |
+
precision=2,
|
| 373 |
+
minimum=0.01,
|
| 374 |
+
maximum=1,
|
| 375 |
+
value=0.30,
|
| 376 |
+
label='置信度阈值'
|
| 377 |
+
)
|
| 378 |
+
det_button = gr.Button("🚀 提交检测任务")
|
| 379 |
+
with gr.Column(scale=1):
|
| 380 |
+
det_output = gr.Image(type='pil', label="检测结果 (边界框)")
|
| 381 |
+
|
| 382 |
+
# 🌟 调整顺序:Examples 先于 类别列表/查询 UI
|
| 383 |
+
gr.Examples(examples=DET_EXAMPLES, inputs=[det_input_image], label="示例图片")
|
| 384 |
+
|
| 385 |
+
# 🌟 展示目标检测类别列表
|
| 386 |
+
gr.Markdown("### 🎯 模型支持的检测类别 (COCO)")
|
| 387 |
+
det_category_json = gr.JSON(value=ALL_DET_LABELS, label="所有类别列表")
|
| 388 |
+
|
| 389 |
+
# 🌟 查询 UI 提示改为英文
|
| 390 |
+
with gr.Row():
|
| 391 |
+
det_search_query = gr.Textbox(label="查询类别", placeholder="Search Class Name (e.g., bicycle, train)",
|
| 392 |
+
scale=3)
|
| 393 |
+
det_search_button = gr.Button("🔍 搜索", scale=1)
|
| 394 |
+
det_search_output = gr.Markdown("搜索结果将显示在这里。")
|
| 395 |
+
|
| 396 |
+
det_search_button.click(
|
| 397 |
+
fn=search_labels,
|
| 398 |
+
inputs=[det_search_query, det_category_json],
|
| 399 |
+
outputs=det_search_output
|
| 400 |
+
)
|
| 401 |
+
|
| 402 |
+
det_button.click(det_predict, inputs=[det_input_image, det_input_number], outputs=det_output)
|
| 403 |
+
|
| 404 |
+
# 🌟 添加页脚和 Logo/版权
|
| 405 |
+
gr.HTML(
|
| 406 |
+
f"""
|
| 407 |
+
<div class='footer-logo-container'>
|
| 408 |
+
<div class="footer-links">
|
| 409 |
+
<p>{COPYRIGHT_TEXT}</p>
|
| 410 |
+
</div>
|
| 411 |
+
|
| 412 |
+
<div class="footer-links">
|
| 413 |
+
|
| 414 |
+
<!-- 🌟 学校图标和名称 (添加超链接) -->
|
| 415 |
+
<span class="footer-icon">🏢</span>
|
| 416 |
+
<a href='https://wutinfo.whut.edu.cn/' target='_blank' style='text-decoration: none; color: inherit;'>
|
| 417 |
+
<span>{SCHOOL_NAME_EN}</span>
|
| 418 |
+
</a>
|
| 419 |
+
|
| 420 |
+
<!-- 🌟 额外 Logo 位于版权信息之后 -->
|
| 421 |
+
<img src='file/{LOGO_PATH}' alt='Logo' style='height: 30px; margin-left: 20px;' onerror="this.style.display='none'">
|
| 422 |
+
</div>
|
| 423 |
+
</div>
|
| 424 |
+
"""
|
| 425 |
+
)
|
| 426 |
+
|
| 427 |
+
if __name__ == "__main__":
|
| 428 |
+
gr.close_all()
|
| 429 |
+
print("Launching Gradio demo...")
|
| 430 |
+
# 🌟 传入 css 参数
|
| 431 |
demo.launch(share=True, css=CUSTOM_CSS)
|