Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -7,8 +7,13 @@ import gradio as gr
|
|
| 7 |
import cv2
|
| 8 |
from PIL import Image
|
| 9 |
import matplotlib.pyplot as plt
|
|
|
|
|
|
|
| 10 |
from model.CyueNet_models import MMS
|
| 11 |
from utils1.data import transform_image
|
|
|
|
|
|
|
|
|
|
| 12 |
|
| 13 |
# 设置GPU/CPU
|
| 14 |
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
|
@@ -16,131 +21,319 @@ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
|
| 16 |
# 增强的自定义CSS
|
| 17 |
custom_css = """
|
| 18 |
:root {
|
| 19 |
-
--primary-color: #
|
| 20 |
-
--secondary-color: #
|
| 21 |
-
--
|
| 22 |
-
--
|
| 23 |
-
--
|
|
|
|
|
|
|
|
|
|
|
|
|
| 24 |
}
|
| 25 |
|
| 26 |
.gradio-container {
|
| 27 |
background: linear-gradient(135deg, var(--background-color), #ffffff);
|
| 28 |
-
max-width:
|
| 29 |
margin: auto !important;
|
|
|
|
| 30 |
}
|
| 31 |
|
| 32 |
.output-image, .input-image {
|
| 33 |
border-radius: var(--border-radius);
|
| 34 |
-
box-shadow:
|
| 35 |
-
transition:
|
|
|
|
| 36 |
}
|
| 37 |
|
| 38 |
.output-image:hover, .input-image:hover {
|
| 39 |
-
transform:
|
|
|
|
| 40 |
}
|
| 41 |
|
| 42 |
.custom-button {
|
| 43 |
background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
|
| 44 |
border: none;
|
| 45 |
color: white;
|
| 46 |
-
padding:
|
| 47 |
-
border-radius:
|
| 48 |
cursor: pointer;
|
| 49 |
transition: all 0.3s ease;
|
| 50 |
-
font-weight:
|
| 51 |
text-transform: uppercase;
|
| 52 |
letter-spacing: 1px;
|
|
|
|
|
|
|
| 53 |
}
|
| 54 |
|
| 55 |
.custom-button:hover {
|
| 56 |
-
transform: translateY(-
|
| 57 |
-
box-shadow: 0
|
| 58 |
}
|
| 59 |
|
| 60 |
.tabs {
|
| 61 |
border-radius: var(--border-radius);
|
| 62 |
overflow: hidden;
|
| 63 |
-
box-shadow:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
.slider-component {
|
| 67 |
background: white;
|
| 68 |
-
padding:
|
| 69 |
border-radius: var(--border-radius);
|
| 70 |
-
box-shadow:
|
|
|
|
| 71 |
}
|
| 72 |
|
| 73 |
.info-box {
|
| 74 |
background: white;
|
| 75 |
-
padding:
|
| 76 |
border-radius: var(--border-radius);
|
| 77 |
-
margin:
|
| 78 |
-
box-shadow:
|
| 79 |
}
|
| 80 |
|
| 81 |
.statistics-container {
|
| 82 |
display: flex;
|
| 83 |
-
gap:
|
| 84 |
-
margin-top:
|
|
|
|
| 85 |
}
|
| 86 |
|
| 87 |
.statistic-card {
|
| 88 |
-
background:
|
| 89 |
-
padding:
|
| 90 |
border-radius: var(--border-radius);
|
| 91 |
flex: 1;
|
|
|
|
| 92 |
text-align: center;
|
| 93 |
-
box-shadow:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 94 |
}
|
| 95 |
|
| 96 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 97 |
background: white;
|
| 98 |
border-radius: var(--border-radius);
|
| 99 |
-
|
| 100 |
-
|
|
|
|
|
|
|
| 101 |
}
|
| 102 |
-
|
| 103 |
-
.
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
transition: background-color 0.3s ease;
|
| 107 |
}
|
| 108 |
-
|
| 109 |
-
.
|
| 110 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 111 |
}
|
| 112 |
-
|
| 113 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 114 |
background: white;
|
| 115 |
-
padding: 20px;
|
| 116 |
border-radius: var(--border-radius);
|
| 117 |
-
|
| 118 |
-
box-shadow:
|
|
|
|
| 119 |
}
|
| 120 |
-
|
| 121 |
-
.
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 122 |
display: grid;
|
| 123 |
-
grid-template-columns: repeat(auto-fit, minmax(
|
| 124 |
gap: 20px;
|
| 125 |
-
margin: 20px
|
| 126 |
}
|
| 127 |
-
|
| 128 |
-
.
|
| 129 |
-
height: 300px;
|
| 130 |
background: white;
|
| 131 |
border-radius: var(--border-radius);
|
| 132 |
-
padding:
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 133 |
}
|
| 134 |
"""
|
| 135 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 136 |
class ImageProcessor:
|
| 137 |
def __init__(self):
|
| 138 |
self.model = None
|
|
|
|
|
|
|
|
|
|
| 139 |
self.load_model()
|
| 140 |
-
self.last_results = None
|
| 141 |
|
| 142 |
def load_model(self):
|
| 143 |
-
"""
|
|
|
|
| 144 |
self.model = MMS()
|
| 145 |
try:
|
| 146 |
self.model.load_state_dict(torch.load('models/CyueNet_EORSSD6.pth.54', map_location=device))
|
|
@@ -149,65 +342,21 @@ class ImageProcessor:
|
|
| 149 |
print(f"加载状态字典时出现部分不匹配,错误信息: {e}")
|
| 150 |
self.model.to(device)
|
| 151 |
self.model.eval()
|
|
|
|
| 152 |
|
| 153 |
-
def
|
| 154 |
-
|
| 155 |
-
if brightness != 0:
|
| 156 |
-
if brightness > 0:
|
| 157 |
-
shadow = brightness
|
| 158 |
-
highlight = 255
|
| 159 |
-
else:
|
| 160 |
-
shadow = 0
|
| 161 |
-
highlight = 255 + brightness
|
| 162 |
-
alpha_b = (highlight - shadow)/255
|
| 163 |
-
gamma_b = shadow
|
| 164 |
-
image = cv2.addWeighted(image, alpha_b, image, 0, gamma_b)
|
| 165 |
-
if contrast != 0:
|
| 166 |
-
f = 131*(contrast + 127)/(127*(131-contrast))
|
| 167 |
-
alpha_c = f
|
| 168 |
-
gamma_c = 127*(1-f)
|
| 169 |
-
image = cv2.addWeighted(image, alpha_c, image, 0, gamma_c)
|
| 170 |
-
return image
|
| 171 |
-
|
| 172 |
-
def apply_filters(self, image, filter_type):
|
| 173 |
-
"""应用图像滤镜效果"""
|
| 174 |
-
if filter_type == "锐化":
|
| 175 |
-
kernel = np.array([[-1,-1,-1], [-1,9,-1], [-1,-1,-1]])
|
| 176 |
-
return cv2.filter2D(image, -1, kernel)
|
| 177 |
-
elif filter_type == "柔化":
|
| 178 |
-
return cv2.GaussianBlur(image, (5,5), 0)
|
| 179 |
-
return image
|
| 180 |
-
|
| 181 |
-
def generate_analysis_plots(self, saliency_map):
|
| 182 |
-
"""生成分析图表"""
|
| 183 |
-
# 直方图数据
|
| 184 |
-
hist_data = saliency_map.flatten()
|
| 185 |
-
fig_hist = px.histogram(hist_data, nbins=50,
|
| 186 |
-
title="显著性分布直方图",
|
| 187 |
-
labels={'value': '显著性值', 'count': '频率'})
|
| 188 |
-
|
| 189 |
-
# 计算显著性统计
|
| 190 |
-
regions = np.zeros_like(saliency_map)
|
| 191 |
-
regions[saliency_map > np.mean(saliency_map)] = 1
|
| 192 |
-
|
| 193 |
-
return fig_hist
|
| 194 |
-
|
| 195 |
-
def process_image(self, image, threshold=0.5, testsize=256,
|
| 196 |
-
enhance_contrast=False, denoise=False,
|
| 197 |
-
brightness=0, contrast=0, filter_type="无"):
|
| 198 |
"""增强的图像处理函数"""
|
| 199 |
if image is None:
|
| 200 |
-
return
|
| 201 |
-
|
| 202 |
-
# 图像预处理
|
| 203 |
-
if denoise:
|
| 204 |
-
image = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)
|
| 205 |
|
| 206 |
-
#
|
| 207 |
-
|
|
|
|
| 208 |
|
| 209 |
-
#
|
| 210 |
-
|
|
|
|
| 211 |
|
| 212 |
if enhance_contrast:
|
| 213 |
lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
|
|
@@ -226,15 +375,14 @@ class ImageProcessor:
|
|
| 226 |
image_tensor = image_tensor.unsqueeze(0)
|
| 227 |
image_tensor = image_tensor.to(device)
|
| 228 |
|
| 229 |
-
# 计时
|
| 230 |
-
time_start = time.time()
|
| 231 |
-
|
| 232 |
# 推理
|
| 233 |
with torch.no_grad():
|
| 234 |
-
|
|
|
|
| 235 |
|
| 236 |
-
|
| 237 |
-
inference_time =
|
|
|
|
| 238 |
|
| 239 |
# 处理输出结果
|
| 240 |
res = res.sigmoid().data.cpu().numpy().squeeze()
|
|
@@ -244,14 +392,22 @@ class ImageProcessor:
|
|
| 244 |
h, w = original_image.shape[:2]
|
| 245 |
res_resized = cv2.resize(res, (w, h))
|
| 246 |
|
| 247 |
-
#
|
| 248 |
res_vis = (res_resized * 255).astype(np.uint8)
|
|
|
|
|
|
|
| 249 |
heatmap = cv2.applyColorMap(res_vis, cv2.COLORMAP_JET)
|
|
|
|
|
|
|
| 250 |
_, binary_mask = cv2.threshold(res_vis, int(255 * threshold), 255, cv2.THRESH_BINARY)
|
| 251 |
|
| 252 |
# 叠加结果
|
| 253 |
alpha = 0.5
|
| 254 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 255 |
overlayed = cv2.addWeighted(original_bgr, 1-alpha, heatmap, alpha, 0)
|
| 256 |
segmented = cv2.bitwise_and(original_bgr, original_bgr, mask=binary_mask)
|
| 257 |
|
|
@@ -259,35 +415,89 @@ class ImageProcessor:
|
|
| 259 |
overlayed_rgb = cv2.cvtColor(overlayed, cv2.COLOR_BGR2RGB)
|
| 260 |
segmented_rgb = cv2.cvtColor(segmented, cv2.COLOR_BGR2RGB)
|
| 261 |
|
| 262 |
-
# 生成分析图表
|
| 263 |
-
analysis_plot = self.generate_analysis_plots(res_resized)
|
| 264 |
-
|
| 265 |
# 计算统计信息
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 266 |
stats = {
|
| 267 |
"处理分辨率": f"{w}x{h}",
|
| 268 |
-
"检测目标数量": str(
|
| 269 |
-
"平均置信度": f"{
|
| 270 |
-
"最大置信度": f"{
|
| 271 |
-
"
|
| 272 |
}
|
| 273 |
|
| 274 |
-
#
|
| 275 |
-
|
| 276 |
-
|
| 277 |
-
|
| 278 |
-
|
| 279 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 280 |
|
| 281 |
-
|
| 282 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 283 |
# 创建处理器实例
|
| 284 |
processor = ImageProcessor()
|
| 285 |
|
| 286 |
-
def run_demo(input_image, threshold, enhance_contrast, denoise, show_contours,
|
| 287 |
-
brightness, contrast, filter_type):
|
| 288 |
"""增强的主处理函数"""
|
| 289 |
if input_image is None:
|
| 290 |
-
return [None] *
|
| 291 |
|
| 292 |
# 处理图像
|
| 293 |
results = processor.process_image(
|
|
@@ -295,12 +505,12 @@ def run_demo(input_image, threshold, enhance_contrast, denoise, show_contours,
|
|
| 295 |
threshold=threshold/100.0,
|
| 296 |
enhance_contrast=enhance_contrast,
|
| 297 |
denoise=denoise,
|
| 298 |
-
|
| 299 |
-
|
| 300 |
-
|
| 301 |
)
|
| 302 |
|
| 303 |
-
original, saliency_map, heatmap, overlayed, segmented, time_info, stats,
|
| 304 |
|
| 305 |
# 添加轮廓显示
|
| 306 |
if show_contours and saliency_map is not None:
|
|
@@ -316,181 +526,412 @@ def run_demo(input_image, threshold, enhance_contrast, denoise, show_contours,
|
|
| 316 |
stats_html += f"<div class='statistic-card'><h4>{key}</h4><p>{value}</p></div>"
|
| 317 |
stats_html += "</div>"
|
| 318 |
|
| 319 |
-
|
| 320 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 321 |
|
| 322 |
# 创建Gradio界面
|
| 323 |
with gr.Blocks(title="高级显著性目标检测系统", css=custom_css) as demo:
|
| 324 |
-
gr.
|
| 325 |
-
|
| 326 |
-
|
| 327 |
-
|
| 328 |
-
|
| 329 |
-
|
| 330 |
-
|
| 331 |
-
|
| 332 |
-
|
| 333 |
-
|
| 334 |
-
|
| 335 |
-
|
| 336 |
-
|
| 337 |
-
|
| 338 |
-
|
| 339 |
-
|
| 340 |
-
|
| 341 |
-
)
|
| 342 |
-
|
| 343 |
-
with gr.Accordion("基础设置", open=True):
|
| 344 |
-
threshold_slider = gr.Slider(
|
| 345 |
-
minimum=0,
|
| 346 |
-
maximum=100,
|
| 347 |
-
value=50,
|
| 348 |
-
step=1,
|
| 349 |
-
label="检测阈值",
|
| 350 |
-
info="调整检测的灵敏度"
|
| 351 |
-
)
|
| 352 |
-
enhance_contrast = gr.Checkbox(
|
| 353 |
-
label="增强对比度",
|
| 354 |
-
value=False
|
| 355 |
-
)
|
| 356 |
-
denoise = gr.Checkbox(
|
| 357 |
-
label="降噪处理",
|
| 358 |
-
value=False
|
| 359 |
-
)
|
| 360 |
-
show_contours = gr.Checkbox(
|
| 361 |
-
label="显示轮廓",
|
| 362 |
-
value=True
|
| 363 |
-
)
|
| 364 |
-
|
| 365 |
-
with gr.Accordion("图像调整", open=False):
|
| 366 |
-
brightness = gr.Slider(
|
| 367 |
-
minimum=-100,
|
| 368 |
-
maximum=100,
|
| 369 |
-
value=0,
|
| 370 |
-
step=1,
|
| 371 |
-
label="亮度"
|
| 372 |
-
)
|
| 373 |
-
contrast = gr.Slider(
|
| 374 |
-
minimum=-100,
|
| 375 |
-
maximum=100,
|
| 376 |
-
value=0,
|
| 377 |
-
step=1,
|
| 378 |
-
label="对比度"
|
| 379 |
-
)
|
| 380 |
-
filter_type = gr.Radio(
|
| 381 |
-
choices=["无", "锐化", "柔化"],
|
| 382 |
-
value="无",
|
| 383 |
-
label="图像滤镜"
|
| 384 |
-
)
|
| 385 |
-
|
| 386 |
with gr.Row():
|
| 387 |
-
|
| 388 |
-
|
| 389 |
-
|
| 390 |
-
|
| 391 |
-
|
| 392 |
-
|
| 393 |
-
|
| 394 |
-
|
| 395 |
-
|
| 396 |
-
|
| 397 |
-
|
| 398 |
-
|
| 399 |
-
|
| 400 |
-
|
| 401 |
-
|
| 402 |
-
|
| 403 |
-
|
| 404 |
-
|
| 405 |
-
|
| 406 |
-
|
| 407 |
-
|
| 408 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 409 |
)
|
| 410 |
|
| 411 |
-
with gr.
|
| 412 |
-
|
| 413 |
-
|
| 414 |
-
|
| 415 |
-
|
| 416 |
-
|
| 417 |
-
|
| 418 |
-
|
| 419 |
-
|
| 420 |
-
|
| 421 |
-
|
| 422 |
-
|
| 423 |
-
|
| 424 |
-
|
| 425 |
-
|
| 426 |
-
|
| 427 |
-
|
| 428 |
-
|
| 429 |
-
|
| 430 |
-
|
| 431 |
-
|
| 432 |
-
|
| 433 |
-
|
| 434 |
-
|
| 435 |
-
|
| 436 |
-
|
| 437 |
-
|
| 438 |
-
|
| 439 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 440 |
gr.Markdown(
|
| 441 |
"""
|
| 442 |
-
## 使用说明
|
| 443 |
-
|
| 444 |
-
|
| 445 |
-
|
| 446 |
-
|
| 447 |
-
|
| 448 |
-
|
| 449 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 450 |
|
| 451 |
-
##
|
| 452 |
-
-
|
| 453 |
-
|
| 454 |
-
|
| 455 |
-
|
| 456 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 457 |
"""
|
| 458 |
)
|
| 459 |
-
|
| 460 |
-
with gr.TabItem("
|
| 461 |
gr.Markdown(
|
| 462 |
"""
|
| 463 |
-
## 项目信息
|
| 464 |
- **版本**: 2.0.0
|
| 465 |
- **技术架构**: PyTorch + Gradio
|
| 466 |
- **模型**: CyueNet
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 467 |
|
| 468 |
-
##
|
| 469 |
-
-
|
| 470 |
-
-
|
| 471 |
-
-
|
| 472 |
-
-
|
|
|
|
| 473 |
|
| 474 |
-
##
|
| 475 |
-
|
| 476 |
-
|
| 477 |
-
|
| 478 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 479 |
"""
|
| 480 |
)
|
| 481 |
|
| 482 |
-
# 重置参数函数
|
| 483 |
-
def reset_params():
|
| 484 |
-
return {
|
| 485 |
-
threshold_slider: 50,
|
| 486 |
-
brightness: 0,
|
| 487 |
-
contrast: 0,
|
| 488 |
-
filter_type: "无",
|
| 489 |
-
enhance_contrast: False,
|
| 490 |
-
denoise: False,
|
| 491 |
-
show_contours: True
|
| 492 |
-
}
|
| 493 |
-
|
| 494 |
# 设置事件处理
|
| 495 |
submit_btn.click(
|
| 496 |
fn=run_demo,
|
|
@@ -500,9 +941,9 @@ with gr.Blocks(title="高级显著性目标检测系统", css=custom_css) as dem
|
|
| 500 |
enhance_contrast,
|
| 501 |
denoise,
|
| 502 |
show_contours,
|
| 503 |
-
|
| 504 |
-
|
| 505 |
-
|
| 506 |
],
|
| 507 |
outputs=[
|
| 508 |
original_output,
|
|
@@ -512,24 +953,52 @@ with gr.Blocks(title="高级显著性目标检测系统", css=custom_css) as dem
|
|
| 512 |
segmented_output,
|
| 513 |
time_info,
|
| 514 |
stats_output,
|
| 515 |
-
|
|
|
|
|
|
|
|
|
|
| 516 |
]
|
| 517 |
)
|
| 518 |
|
| 519 |
-
|
| 520 |
-
|
| 521 |
-
|
|
|
|
| 522 |
outputs=[
|
| 523 |
-
|
| 524 |
-
|
| 525 |
-
|
| 526 |
-
|
| 527 |
-
|
| 528 |
-
|
| 529 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
| 530 |
]
|
| 531 |
)
|
| 532 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 533 |
# 启动应用
|
| 534 |
if __name__ == "__main__":
|
| 535 |
-
demo.launch(
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 7 |
import cv2
|
| 8 |
from PIL import Image
|
| 9 |
import matplotlib.pyplot as plt
|
| 10 |
+
from matplotlib.colors import LinearSegmentedColormap
|
| 11 |
+
import plotly.express as px
|
| 12 |
from model.CyueNet_models import MMS
|
| 13 |
from utils1.data import transform_image
|
| 14 |
+
import tempfile
|
| 15 |
+
from pathlib import Path
|
| 16 |
+
import pandas as pd
|
| 17 |
|
| 18 |
# 设置GPU/CPU
|
| 19 |
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
|
|
|
| 21 |
# 增强的自定义CSS
|
| 22 |
custom_css = """
|
| 23 |
:root {
|
| 24 |
+
--primary-color: #3498db;
|
| 25 |
+
--secondary-color: #2ecc71;
|
| 26 |
+
--accent-color: #e74c3c;
|
| 27 |
+
--dark-color: #2c3e50;
|
| 28 |
+
--light-color: #ecf0f1;
|
| 29 |
+
--background-color: #f8f9fa;
|
| 30 |
+
--text-color: #34495e;
|
| 31 |
+
--border-radius: 12px;
|
| 32 |
+
--box-shadow: 0 6px 16px rgba(0, 0, 0, 0.12);
|
| 33 |
}
|
| 34 |
|
| 35 |
.gradio-container {
|
| 36 |
background: linear-gradient(135deg, var(--background-color), #ffffff);
|
| 37 |
+
max-width: 1400px !important;
|
| 38 |
margin: auto !important;
|
| 39 |
+
font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
|
| 40 |
}
|
| 41 |
|
| 42 |
.output-image, .input-image {
|
| 43 |
border-radius: var(--border-radius);
|
| 44 |
+
box-shadow: var(--box-shadow);
|
| 45 |
+
transition: all 0.3s ease;
|
| 46 |
+
border: 1px solid rgba(0, 0, 0, 0.1);
|
| 47 |
}
|
| 48 |
|
| 49 |
.output-image:hover, .input-image:hover {
|
| 50 |
+
transform: translateY(-5px);
|
| 51 |
+
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
|
| 52 |
}
|
| 53 |
|
| 54 |
.custom-button {
|
| 55 |
background: linear-gradient(45deg, var(--primary-color), var(--secondary-color));
|
| 56 |
border: none;
|
| 57 |
color: white;
|
| 58 |
+
padding: 12px 24px;
|
| 59 |
+
border-radius: 30px;
|
| 60 |
cursor: pointer;
|
| 61 |
transition: all 0.3s ease;
|
| 62 |
+
font-weight: 600;
|
| 63 |
text-transform: uppercase;
|
| 64 |
letter-spacing: 1px;
|
| 65 |
+
box-shadow: 0 4px 8px rgba(52, 152, 219, 0.3);
|
| 66 |
+
margin: 5px;
|
| 67 |
}
|
| 68 |
|
| 69 |
.custom-button:hover {
|
| 70 |
+
transform: translateY(-3px);
|
| 71 |
+
box-shadow: 0 6px 12px rgba(52, 152, 219, 0.4);
|
| 72 |
}
|
| 73 |
|
| 74 |
.tabs {
|
| 75 |
border-radius: var(--border-radius);
|
| 76 |
overflow: hidden;
|
| 77 |
+
box-shadow: var(--box-shadow);
|
| 78 |
+
background: white;
|
| 79 |
+
}
|
| 80 |
+
|
| 81 |
+
.tab-item {
|
| 82 |
+
padding: 20px;
|
| 83 |
}
|
| 84 |
|
| 85 |
.slider-component {
|
| 86 |
background: white;
|
| 87 |
+
padding: 20px;
|
| 88 |
border-radius: var(--border-radius);
|
| 89 |
+
box-shadow: var(--box-shadow);
|
| 90 |
+
margin-bottom: 20px;
|
| 91 |
}
|
| 92 |
|
| 93 |
.info-box {
|
| 94 |
background: white;
|
| 95 |
+
padding: 20px;
|
| 96 |
border-radius: var(--border-radius);
|
| 97 |
+
margin: 15px 0;
|
| 98 |
+
box-shadow: var(--box-shadow);
|
| 99 |
}
|
| 100 |
|
| 101 |
.statistics-container {
|
| 102 |
display: flex;
|
| 103 |
+
gap: 15px;
|
| 104 |
+
margin-top: 15px;
|
| 105 |
+
flex-wrap: wrap;
|
| 106 |
}
|
| 107 |
|
| 108 |
.statistic-card {
|
| 109 |
+
background: linear-gradient(135deg, var(--primary-color), var(--secondary-color));
|
| 110 |
+
padding: 20px;
|
| 111 |
border-radius: var(--border-radius);
|
| 112 |
flex: 1;
|
| 113 |
+
min-width: 200px;
|
| 114 |
text-align: center;
|
| 115 |
+
box-shadow: var(--box-shadow);
|
| 116 |
+
color: white;
|
| 117 |
+
transition: all 0.3s ease;
|
| 118 |
+
}
|
| 119 |
+
|
| 120 |
+
.statistic-card:hover {
|
| 121 |
+
transform: scale(1.03);
|
| 122 |
}
|
| 123 |
|
| 124 |
+
.statistic-card h4 {
|
| 125 |
+
margin: 0 0 10px 0;
|
| 126 |
+
font-size: 16px;
|
| 127 |
+
font-weight: 500;
|
| 128 |
+
}
|
| 129 |
+
|
| 130 |
+
.statistic-card p {
|
| 131 |
+
margin: 0;
|
| 132 |
+
font-size: 24px;
|
| 133 |
+
font-weight: 700;
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
.card {
|
| 137 |
background: white;
|
| 138 |
border-radius: var(--border-radius);
|
| 139 |
+
box-shadow: var(--box-shadow);
|
| 140 |
+
padding: 20px;
|
| 141 |
+
margin-bottom: 20px;
|
| 142 |
+
transition: all 0.3s ease;
|
| 143 |
}
|
| 144 |
+
|
| 145 |
+
.card:hover {
|
| 146 |
+
transform: translateY(-5px);
|
| 147 |
+
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
|
|
|
|
| 148 |
}
|
| 149 |
+
|
| 150 |
+
.card-title {
|
| 151 |
+
font-size: 18px;
|
| 152 |
+
font-weight: 600;
|
| 153 |
+
margin-bottom: 15px;
|
| 154 |
+
color: var(--dark-color);
|
| 155 |
+
display: flex;
|
| 156 |
+
align-items: center;
|
| 157 |
}
|
| 158 |
+
|
| 159 |
+
.card-title i {
|
| 160 |
+
margin-right: 10px;
|
| 161 |
+
font-size: 20px;
|
| 162 |
+
}
|
| 163 |
+
|
| 164 |
+
.header {
|
| 165 |
+
background: linear-gradient(135deg, var(--dark-color), var(--primary-color));
|
| 166 |
+
color: white;
|
| 167 |
+
padding: 30px;
|
| 168 |
+
border-radius: var(--border-radius) var(--border-radius) 0 0;
|
| 169 |
+
text-align: center;
|
| 170 |
+
margin-bottom: 30px;
|
| 171 |
+
}
|
| 172 |
+
|
| 173 |
+
.header h1 {
|
| 174 |
+
margin: 0;
|
| 175 |
+
font-size: 36px;
|
| 176 |
+
font-weight: 700;
|
| 177 |
+
display: flex;
|
| 178 |
+
align-items: center;
|
| 179 |
+
justify-content: center;
|
| 180 |
+
}
|
| 181 |
+
|
| 182 |
+
.header h1 i {
|
| 183 |
+
margin-right: 15px;
|
| 184 |
+
}
|
| 185 |
+
|
| 186 |
+
.header p {
|
| 187 |
+
margin: 10px 0 0 0;
|
| 188 |
+
font-size: 18px;
|
| 189 |
+
opacity: 0.9;
|
| 190 |
+
}
|
| 191 |
+
|
| 192 |
+
.feature-grid {
|
| 193 |
+
display: grid;
|
| 194 |
+
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| 195 |
+
gap: 20px;
|
| 196 |
+
margin-bottom: 30px;
|
| 197 |
+
}
|
| 198 |
+
|
| 199 |
+
.feature-card {
|
| 200 |
background: white;
|
|
|
|
| 201 |
border-radius: var(--border-radius);
|
| 202 |
+
padding: 25px;
|
| 203 |
+
box-shadow: var(--box-shadow);
|
| 204 |
+
transition: all 0.3s ease;
|
| 205 |
}
|
| 206 |
+
|
| 207 |
+
.feature-card:hover {
|
| 208 |
+
transform: translateY(-5px);
|
| 209 |
+
box-shadow: 0 12px 24px rgba(0, 0, 0, 0.15);
|
| 210 |
+
}
|
| 211 |
+
|
| 212 |
+
.feature-card h3 {
|
| 213 |
+
color: var(--primary-color);
|
| 214 |
+
display: flex;
|
| 215 |
+
align-items: center;
|
| 216 |
+
}
|
| 217 |
+
|
| 218 |
+
.feature-card h3 i {
|
| 219 |
+
margin-right: 10px;
|
| 220 |
+
font-size: 24px;
|
| 221 |
+
}
|
| 222 |
+
|
| 223 |
+
.performance-bar {
|
| 224 |
+
height: 8px;
|
| 225 |
+
background: #e0e0e0;
|
| 226 |
+
border-radius: 4px;
|
| 227 |
+
margin: 15px 0;
|
| 228 |
+
overflow: hidden;
|
| 229 |
+
}
|
| 230 |
+
|
| 231 |
+
.performance-fill {
|
| 232 |
+
height: 100%;
|
| 233 |
+
background: linear-gradient(90deg, var(--secondary-color), var(--primary-color));
|
| 234 |
+
border-radius: 4px;
|
| 235 |
+
transition: width 0.5s ease;
|
| 236 |
+
}
|
| 237 |
+
|
| 238 |
+
.tooltip {
|
| 239 |
+
position: relative;
|
| 240 |
+
display: inline-block;
|
| 241 |
+
cursor: pointer;
|
| 242 |
+
}
|
| 243 |
+
|
| 244 |
+
.tooltip .tooltiptext {
|
| 245 |
+
visibility: hidden;
|
| 246 |
+
width: 200px;
|
| 247 |
+
background-color: var(--dark-color);
|
| 248 |
+
color: #fff;
|
| 249 |
+
text-align: center;
|
| 250 |
+
border-radius: 6px;
|
| 251 |
+
padding: 10px;
|
| 252 |
+
position: absolute;
|
| 253 |
+
z-index: 1;
|
| 254 |
+
bottom: 125%;
|
| 255 |
+
left: 50%;
|
| 256 |
+
transform: translateX(-50%);
|
| 257 |
+
opacity: 0;
|
| 258 |
+
transition: opacity 0.3s;
|
| 259 |
+
font-size: 14px;
|
| 260 |
+
}
|
| 261 |
+
|
| 262 |
+
.tooltip:hover .tooltiptext {
|
| 263 |
+
visibility: visible;
|
| 264 |
+
opacity: 1;
|
| 265 |
+
}
|
| 266 |
+
|
| 267 |
+
.model-performance {
|
| 268 |
+
display: flex;
|
| 269 |
+
justify-content: space-between;
|
| 270 |
+
align-items: center;
|
| 271 |
+
margin-top: 20px;
|
| 272 |
+
}
|
| 273 |
+
|
| 274 |
+
.model-metric {
|
| 275 |
+
text-align: center;
|
| 276 |
+
padding: 15px;
|
| 277 |
+
background: rgba(236, 240, 241, 0.5);
|
| 278 |
+
border-radius: var(--border-radius);
|
| 279 |
+
flex: 1;
|
| 280 |
+
margin: 0 5px;
|
| 281 |
+
}
|
| 282 |
+
|
| 283 |
+
.model-metric h4 {
|
| 284 |
+
margin: 0 0 5px 0;
|
| 285 |
+
color: var(--dark-color);
|
| 286 |
+
font-weight: 500;
|
| 287 |
+
}
|
| 288 |
+
|
| 289 |
+
.model-metric p {
|
| 290 |
+
margin: 0;
|
| 291 |
+
font-size: 24px;
|
| 292 |
+
font-weight: 700;
|
| 293 |
+
color: var(--primary-color);
|
| 294 |
+
}
|
| 295 |
+
|
| 296 |
+
.visualization-container {
|
| 297 |
display: grid;
|
| 298 |
+
grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
|
| 299 |
gap: 20px;
|
| 300 |
+
margin-top: 20px;
|
| 301 |
}
|
| 302 |
+
|
| 303 |
+
.visualization-card {
|
|
|
|
| 304 |
background: white;
|
| 305 |
border-radius: var(--border-radius);
|
| 306 |
+
padding: 20px;
|
| 307 |
+
box-shadow: var(--box-shadow);
|
| 308 |
+
}
|
| 309 |
+
|
| 310 |
+
.btn-group {
|
| 311 |
+
display: flex;
|
| 312 |
+
flex-wrap: wrap;
|
| 313 |
+
margin: 15px 0;
|
| 314 |
+
gap: 10px;
|
| 315 |
}
|
| 316 |
"""
|
| 317 |
|
| 318 |
+
# 创建自定义热力图颜色映射
|
| 319 |
+
def create_custom_colormap():
|
| 320 |
+
colors = ["#2c3e50", "#3498db", "#1abc9c", "#f1c40f", "#e74c3c"]
|
| 321 |
+
cmap = LinearSegmentedColormap.from_list("custom_heatmap", colors)
|
| 322 |
+
return cmap
|
| 323 |
+
|
| 324 |
+
custom_cmap = create_custom_colormap()
|
| 325 |
+
|
| 326 |
class ImageProcessor:
|
| 327 |
def __init__(self):
|
| 328 |
self.model = None
|
| 329 |
+
self.model_load_time = 0
|
| 330 |
+
self.total_inference_time = 0
|
| 331 |
+
self.processed_count = 0
|
| 332 |
self.load_model()
|
|
|
|
| 333 |
|
| 334 |
def load_model(self):
|
| 335 |
+
"""加载预训练的模型并记录加载时间"""
|
| 336 |
+
start_time = time.time()
|
| 337 |
self.model = MMS()
|
| 338 |
try:
|
| 339 |
self.model.load_state_dict(torch.load('models/CyueNet_EORSSD6.pth.54', map_location=device))
|
|
|
|
| 342 |
print(f"加载状态字典时出现部分不匹配,错误信息: {e}")
|
| 343 |
self.model.to(device)
|
| 344 |
self.model.eval()
|
| 345 |
+
self.model_load_time = time.time() - start_time
|
| 346 |
|
| 347 |
+
def process_image(self, image, threshold=0.5, testsize=256, enhance_contrast=False, denoise=False,
|
| 348 |
+
show_heatmap=True, show_segmentation=True, show_confidence=True):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 349 |
"""增强的图像处理函数"""
|
| 350 |
if image is None:
|
| 351 |
+
return None, None, None, None, None, "请提供有效的图像", {}, None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
| 352 |
|
| 353 |
+
# 记录处理开始时间
|
| 354 |
+
self.processed_count += 1
|
| 355 |
+
time_start = time.time()
|
| 356 |
|
| 357 |
+
# 图像预处理选项
|
| 358 |
+
if denoise:
|
| 359 |
+
image = cv2.fastNlMeansDenoisingColored(image, None, 10, 10, 7, 21)
|
| 360 |
|
| 361 |
if enhance_contrast:
|
| 362 |
lab = cv2.cvtColor(image, cv2.COLOR_RGB2LAB)
|
|
|
|
| 375 |
image_tensor = image_tensor.unsqueeze(0)
|
| 376 |
image_tensor = image_tensor.to(device)
|
| 377 |
|
|
|
|
|
|
|
|
|
|
| 378 |
# 推理
|
| 379 |
with torch.no_grad():
|
| 380 |
+
outputs = self.model(image_tensor)
|
| 381 |
+
x1, res, s1_sig, edg1, edg_s, s2, e2, s2_sig, e2_sig, s3, e3, s3_sig, e3_sig, s4, e4, s4_sig, e4_sig, s5, e5, s5_sig, e5_sig, sk1, sk1_sig, sk2, sk2_sig, sk3, sk3_sig, sk4, sk4_sig, sk5, sk5_sig = outputs
|
| 382 |
|
| 383 |
+
# 记录推理时间
|
| 384 |
+
inference_time = time.time() - time_start
|
| 385 |
+
self.total_inference_time += inference_time
|
| 386 |
|
| 387 |
# 处理输出结果
|
| 388 |
res = res.sigmoid().data.cpu().numpy().squeeze()
|
|
|
|
| 392 |
h, w = original_image.shape[:2]
|
| 393 |
res_resized = cv2.resize(res, (w, h))
|
| 394 |
|
| 395 |
+
# 应用阈值
|
| 396 |
res_vis = (res_resized * 255).astype(np.uint8)
|
| 397 |
+
|
| 398 |
+
# 创建热力图
|
| 399 |
heatmap = cv2.applyColorMap(res_vis, cv2.COLORMAP_JET)
|
| 400 |
+
|
| 401 |
+
# 二值化处理
|
| 402 |
_, binary_mask = cv2.threshold(res_vis, int(255 * threshold), 255, cv2.THRESH_BINARY)
|
| 403 |
|
| 404 |
# 叠加结果
|
| 405 |
alpha = 0.5
|
| 406 |
+
if len(original_image.shape) == 3 and original_image.shape[2] == 3:
|
| 407 |
+
original_bgr = cv2.cvtColor(original_image, cv2.COLOR_RGB2BGR)
|
| 408 |
+
else:
|
| 409 |
+
original_bgr = cv2.cvtColor(original_image, cv2.COLOR_GRAY2BGR)
|
| 410 |
+
|
| 411 |
overlayed = cv2.addWeighted(original_bgr, 1-alpha, heatmap, alpha, 0)
|
| 412 |
segmented = cv2.bitwise_and(original_bgr, original_bgr, mask=binary_mask)
|
| 413 |
|
|
|
|
| 415 |
overlayed_rgb = cv2.cvtColor(overlayed, cv2.COLOR_BGR2RGB)
|
| 416 |
segmented_rgb = cv2.cvtColor(segmented, cv2.COLOR_BGR2RGB)
|
| 417 |
|
|
|
|
|
|
|
|
|
|
| 418 |
# 计算统计信息
|
| 419 |
+
contours, _ = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)
|
| 420 |
+
num_objects = len(contours)
|
| 421 |
+
mean_confidence = np.mean(res_resized)
|
| 422 |
+
max_confidence = np.max(res_resized)
|
| 423 |
+
|
| 424 |
stats = {
|
| 425 |
"处理分辨率": f"{w}x{h}",
|
| 426 |
+
"检测目标数量": str(num_objects),
|
| 427 |
+
"平均置信度": f"{mean_confidence:.2%}",
|
| 428 |
+
"最大置信度": f"{max_confidence:.2%}",
|
| 429 |
+
"推理时间": f"{inference_time:.4f}秒"
|
| 430 |
}
|
| 431 |
|
| 432 |
+
# 创建置信度直方图
|
| 433 |
+
plt.figure(figsize=(8, 4))
|
| 434 |
+
plt.hist(res_resized.flatten(), bins=50, color='#3498db', alpha=0.7)
|
| 435 |
+
plt.title('置信度分布')
|
| 436 |
+
plt.xlabel('置信度')
|
| 437 |
+
plt.ylabel('像素数量')
|
| 438 |
+
plt.grid(True, linestyle='--', alpha=0.7)
|
| 439 |
+
plt.tight_layout()
|
| 440 |
+
|
| 441 |
+
# 保存到临时文件
|
| 442 |
+
temp_file = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 443 |
+
plt.savefig(temp_file.name, dpi=100)
|
| 444 |
+
plt.close()
|
| 445 |
+
confidence_plot = temp_file.name
|
| 446 |
+
|
| 447 |
+
# 创建3D热力图可视化
|
| 448 |
+
sample_step = max(1, w // 50, h // 50) # 采样步长
|
| 449 |
+
y, x = np.mgrid[0:h:sample_step, 0:w:sample_step]
|
| 450 |
+
z = res_resized[::sample_step, ::sample_step]
|
| 451 |
+
|
| 452 |
+
fig = px.scatter_3d(
|
| 453 |
+
x=x.flatten(),
|
| 454 |
+
y=y.flatten(),
|
| 455 |
+
z=z.flatten(),
|
| 456 |
+
color=z.flatten(),
|
| 457 |
+
color_continuous_scale='jet',
|
| 458 |
+
title='3D置信度热力图'
|
| 459 |
+
)
|
| 460 |
+
|
| 461 |
+
fig.update_layout(
|
| 462 |
+
scene=dict(
|
| 463 |
+
xaxis_title='宽度',
|
| 464 |
+
yaxis_title='高度',
|
| 465 |
+
zaxis_title='置信度'
|
| 466 |
+
),
|
| 467 |
+
height=400,
|
| 468 |
+
margin=dict(l=20, r=20, b=20, t=40)
|
| 469 |
+
)
|
| 470 |
+
|
| 471 |
+
# 保存到临时文件
|
| 472 |
+
temp_file_3d = tempfile.NamedTemporaryFile(suffix=".html", delete=False)
|
| 473 |
+
fig.write_html(temp_file_3d.name)
|
| 474 |
+
heatmap_3d = temp_file_3d.name
|
| 475 |
|
| 476 |
+
# 创建模型注意力可视化
|
| 477 |
+
attention_map = sk1_sig.squeeze().cpu().numpy()
|
| 478 |
+
attention_map = (attention_map - attention_map.min()) / (attention_map.max() - attention_map.min() + 1e-8)
|
| 479 |
+
attention_map_resized = cv2.resize(attention_map, (w, h))
|
| 480 |
+
|
| 481 |
+
plt.figure(figsize=(8, 4))
|
| 482 |
+
plt.imshow(attention_map_resized, cmap='viridis')
|
| 483 |
+
plt.title('模型注意力图')
|
| 484 |
+
plt.colorbar()
|
| 485 |
+
plt.tight_layout()
|
| 486 |
+
|
| 487 |
+
temp_file_attn = tempfile.NamedTemporaryFile(suffix=".png", delete=False)
|
| 488 |
+
plt.savefig(temp_file_attn.name, dpi=100)
|
| 489 |
+
plt.close()
|
| 490 |
+
attention_plot = temp_file_attn.name
|
| 491 |
+
|
| 492 |
+
return original_image, res_vis, heatmap, overlayed_rgb, segmented_rgb, f"推理时间: {inference_time:.4f}秒", stats, confidence_plot, heatmap_3d, attention_plot
|
| 493 |
+
|
| 494 |
# 创建处理器实例
|
| 495 |
processor = ImageProcessor()
|
| 496 |
|
| 497 |
+
def run_demo(input_image, threshold, enhance_contrast, denoise, show_contours, show_heatmap, show_segmentation, show_confidence):
|
|
|
|
| 498 |
"""增强的主处理函数"""
|
| 499 |
if input_image is None:
|
| 500 |
+
return [None] * 10 + ["请上传图片"]
|
| 501 |
|
| 502 |
# 处理图像
|
| 503 |
results = processor.process_image(
|
|
|
|
| 505 |
threshold=threshold/100.0,
|
| 506 |
enhance_contrast=enhance_contrast,
|
| 507 |
denoise=denoise,
|
| 508 |
+
show_heatmap=show_heatmap,
|
| 509 |
+
show_segmentation=show_segmentation,
|
| 510 |
+
show_confidence=show_confidence
|
| 511 |
)
|
| 512 |
|
| 513 |
+
original, saliency_map, heatmap, overlayed, segmented, time_info, stats, confidence_plot, heatmap_3d, attention_plot = results
|
| 514 |
|
| 515 |
# 添加轮廓显示
|
| 516 |
if show_contours and saliency_map is not None:
|
|
|
|
| 526 |
stats_html += f"<div class='statistic-card'><h4>{key}</h4><p>{value}</p></div>"
|
| 527 |
stats_html += "</div>"
|
| 528 |
|
| 529 |
+
# 生成模型性能信息
|
| 530 |
+
avg_inference_time = processor.total_inference_time / processor.processed_count if processor.processed_count > 0 else 0
|
| 531 |
+
model_stats = {
|
| 532 |
+
"模型加载时间": f"{processor.model_load_time:.2f}秒",
|
| 533 |
+
"平均推理时间": f"{avg_inference_time:.4f}秒",
|
| 534 |
+
"处理图像数量": f"{processor.processed_count}张",
|
| 535 |
+
"设备类型": "GPU" if torch.cuda.is_available() else "CPU"
|
| 536 |
+
}
|
| 537 |
+
|
| 538 |
+
model_html = "<div class='statistics-container'>"
|
| 539 |
+
for key, value in model_stats.items():
|
| 540 |
+
model_html += f"<div class='statistic-card'><h4>{key}</h4><p>{value}</p></div>"
|
| 541 |
+
model_html += "</div>"
|
| 542 |
+
|
| 543 |
+
return original, saliency_map, heatmap, overlayed, segmented, time_info, stats_html, confidence_plot, heatmap_3d, attention_plot, model_html
|
| 544 |
+
|
| 545 |
+
def process_example(example_index):
|
| 546 |
+
"""处理示例图像"""
|
| 547 |
+
examples = [
|
| 548 |
+
"example_images/1.jpg",
|
| 549 |
+
"example_images/2.jpg",
|
| 550 |
+
"example_images/3.jpg",
|
| 551 |
+
"example_images/4.jpg"
|
| 552 |
+
]
|
| 553 |
+
|
| 554 |
+
if example_index < 0 or example_index >= len(examples):
|
| 555 |
+
return [None] * 11
|
| 556 |
+
|
| 557 |
+
image_path = examples[example_index]
|
| 558 |
+
if not os.path.exists(image_path):
|
| 559 |
+
return [None] * 11 + [f"示例图像不存在: {image_path}"]
|
| 560 |
+
|
| 561 |
+
image = cv2.imread(image_path)
|
| 562 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 563 |
+
|
| 564 |
+
results = run_demo(image, 50, False, False, True, True, True, True)
|
| 565 |
+
return results
|
| 566 |
+
|
| 567 |
+
def process_webcam(image):
|
| 568 |
+
"""处理网络摄像头图像"""
|
| 569 |
+
if image is None:
|
| 570 |
+
return [None] * 11
|
| 571 |
+
|
| 572 |
+
results = run_demo(image, 50, True, True, True, True, True, True)
|
| 573 |
+
return results
|
| 574 |
+
|
| 575 |
+
def process_folder(folder_path):
|
| 576 |
+
"""处理文件夹中的图像"""
|
| 577 |
+
if folder_path is None:
|
| 578 |
+
return "请选择文件夹", None
|
| 579 |
+
|
| 580 |
+
if not os.path.exists(folder_path):
|
| 581 |
+
return f"文件夹不存在: {folder_path}", None
|
| 582 |
+
|
| 583 |
+
image_extensions = ['.jpg', '.jpeg', '.png', '.bmp']
|
| 584 |
+
image_files = [f for f in os.listdir(folder_path)
|
| 585 |
+
if os.path.isfile(os.path.join(folder_path, f))
|
| 586 |
+
and os.path.splitext(f)[1].lower() in image_extensions]
|
| 587 |
+
|
| 588 |
+
if not image_files:
|
| 589 |
+
return "文件夹中没有找到图像文件", None
|
| 590 |
+
|
| 591 |
+
# 创建结果目录
|
| 592 |
+
results_dir = os.path.join(folder_path, "detection_results")
|
| 593 |
+
os.makedirs(results_dir, exist_ok=True)
|
| 594 |
+
|
| 595 |
+
# 处理图像
|
| 596 |
+
processed_count = 0
|
| 597 |
+
for img_file in image_files:
|
| 598 |
+
img_path = os.path.join(folder_path, img_file)
|
| 599 |
+
image = cv2.imread(img_path)
|
| 600 |
+
image = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
|
| 601 |
+
|
| 602 |
+
# 处理图像
|
| 603 |
+
_, _, _, _, segmented, _, _, _, _, _ = processor.process_image(
|
| 604 |
+
image, threshold=0.5, testsize=256,
|
| 605 |
+
enhance_contrast=False, denoise=False
|
| 606 |
+
)
|
| 607 |
+
|
| 608 |
+
# 保存结果
|
| 609 |
+
result_path = os.path.join(results_dir, f"result_{img_file}")
|
| 610 |
+
cv2.imwrite(result_path, cv2.cvtColor(segmented, cv2.COLOR_RGB2BGR))
|
| 611 |
+
processed_count += 1
|
| 612 |
+
|
| 613 |
+
return f"处理完成: {processed_count}张图像已保存到 {results_dir}", results_dir
|
| 614 |
|
| 615 |
# 创建Gradio界面
|
| 616 |
with gr.Blocks(title="高级显著性目标检测系统", css=custom_css) as demo:
|
| 617 |
+
with gr.Column():
|
| 618 |
+
# 头部区域
|
| 619 |
+
with gr.Row(elem_classes="header"):
|
| 620 |
+
gr.Markdown(
|
| 621 |
+
"""
|
| 622 |
+
<div style="text-align: center;">
|
| 623 |
+
<h1><i class="fas fa-eye"></i> 智能显著性目标检测系统</h1>
|
| 624 |
+
<p>基于深度学习的图像显著性检测与分析平台</p>
|
| 625 |
+
</div>
|
| 626 |
+
"""
|
| 627 |
+
)
|
| 628 |
+
|
| 629 |
+
# 功能卡片
|
| 630 |
+
with gr.Row():
|
| 631 |
+
with gr.Column(scale=3):
|
| 632 |
+
with gr.Tabs() as tabs:
|
| 633 |
+
with gr.TabItem("🖼️ 图像处理", elem_classes="tab-item"):
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 634 |
with gr.Row():
|
| 635 |
+
with gr.Column(scale=1):
|
| 636 |
+
# 输入区域
|
| 637 |
+
with gr.Card(elem_classes="card"):
|
| 638 |
+
gr.Markdown("### 📤 输入图像")
|
| 639 |
+
input_image = gr.Image(
|
| 640 |
+
label="上传或拍摄图像",
|
| 641 |
+
type="numpy",
|
| 642 |
+
elem_classes="input-image",
|
| 643 |
+
interactive=True
|
| 644 |
+
)
|
| 645 |
+
|
| 646 |
+
# 示例图像按钮
|
| 647 |
+
gr.Markdown("### 🧪 示例图像")
|
| 648 |
+
example_btns = gr.Row()
|
| 649 |
+
with example_btns:
|
| 650 |
+
for i in range(4):
|
| 651 |
+
gr.Button(f"示例 {i+1}", elem_classes="custom-button").click(
|
| 652 |
+
fn=lambda idx=i: process_example(idx),
|
| 653 |
+
outputs=[
|
| 654 |
+
input_image,
|
| 655 |
+
gr.components.Image(label="显著性图", visible=False),
|
| 656 |
+
gr.components.Image(label="热力图", visible=False),
|
| 657 |
+
gr.components.Image(label="叠加效果", visible=False),
|
| 658 |
+
gr.components.Image(label="目标分割", visible=False),
|
| 659 |
+
gr.components.Textbox(visible=False),
|
| 660 |
+
gr.components.HTML(visible=False),
|
| 661 |
+
gr.components.Image(visible=False),
|
| 662 |
+
gr.components.HTML(visible=False),
|
| 663 |
+
gr.components.Image(visible=False),
|
| 664 |
+
gr.components.HTML(visible=False)
|
| 665 |
+
]
|
| 666 |
+
)
|
| 667 |
+
|
| 668 |
+
# 摄像头输入
|
| 669 |
+
gr.Markdown("### 📷 实时摄像头")
|
| 670 |
+
webcam_btn = gr.Button("启动摄像头", elem_classes="custom-button")
|
| 671 |
+
webcam_btn.click(
|
| 672 |
+
fn=lambda: None,
|
| 673 |
+
inputs=None,
|
| 674 |
+
outputs=None,
|
| 675 |
+
_js="""
|
| 676 |
+
() => {
|
| 677 |
+
document.querySelector('button[aria-label="Take Photo"]').click();
|
| 678 |
+
}
|
| 679 |
+
"""
|
| 680 |
+
)
|
| 681 |
+
|
| 682 |
+
# 批量处理
|
| 683 |
+
with gr.Card(elem_classes="card"):
|
| 684 |
+
gr.Markdown("### 📁 批量处理")
|
| 685 |
+
folder_input = gr.File(file_count="directory", label="选择图像文件夹")
|
| 686 |
+
process_folder_btn = gr.Button("处理文件夹", elem_classes="custom-button")
|
| 687 |
+
folder_output = gr.Textbox(label="处理结果")
|
| 688 |
+
folder_processed = gr.File(label="下载结果", file_count="directory")
|
| 689 |
+
|
| 690 |
+
process_folder_btn.click(
|
| 691 |
+
fn=process_folder,
|
| 692 |
+
inputs=folder_input,
|
| 693 |
+
outputs=[folder_output, folder_processed]
|
| 694 |
+
)
|
| 695 |
+
|
| 696 |
+
# 参数设置
|
| 697 |
+
with gr.Card(elem_classes="card"):
|
| 698 |
+
gr.Markdown("### ⚙️ 处理参数")
|
| 699 |
+
threshold_slider = gr.Slider(
|
| 700 |
+
minimum=0,
|
| 701 |
+
maximum=100,
|
| 702 |
+
value=50,
|
| 703 |
+
step=1,
|
| 704 |
+
label="检测阈值",
|
| 705 |
+
info="调整检测的灵敏度"
|
| 706 |
+
)
|
| 707 |
+
|
| 708 |
+
with gr.Row():
|
| 709 |
+
enhance_contrast = gr.Checkbox(
|
| 710 |
+
label="增强对比度",
|
| 711 |
+
value=False
|
| 712 |
+
)
|
| 713 |
+
denoise = gr.Checkbox(
|
| 714 |
+
label="降噪处理",
|
| 715 |
+
value=False
|
| 716 |
+
)
|
| 717 |
+
|
| 718 |
+
with gr.Row():
|
| 719 |
+
show_contours = gr.Checkbox(
|
| 720 |
+
label="显示轮廓",
|
| 721 |
+
value=True
|
| 722 |
+
)
|
| 723 |
+
show_heatmap = gr.Checkbox(
|
| 724 |
+
label="显示热力图",
|
| 725 |
+
value=True
|
| 726 |
+
)
|
| 727 |
+
|
| 728 |
+
with gr.Row():
|
| 729 |
+
show_segmentation = gr.Checkbox(
|
| 730 |
+
label="显示分割结果",
|
| 731 |
+
value=True
|
| 732 |
+
)
|
| 733 |
+
show_confidence = gr.Checkbox(
|
| 734 |
+
label="显示置信度",
|
| 735 |
+
value=True
|
| 736 |
+
)
|
| 737 |
+
|
| 738 |
+
# 提交按钮
|
| 739 |
+
submit_btn = gr.Button(
|
| 740 |
+
"开始检测",
|
| 741 |
+
variant="primary",
|
| 742 |
+
elem_classes="custom-button"
|
| 743 |
)
|
| 744 |
|
| 745 |
+
with gr.Column(scale=2):
|
| 746 |
+
# 输出区域
|
| 747 |
+
with gr.Card(elem_classes="card"):
|
| 748 |
+
gr.Markdown("### 📊 检测结果")
|
| 749 |
+
with gr.Tabs():
|
| 750 |
+
with gr.TabItem("可视化结果"):
|
| 751 |
+
with gr.Row():
|
| 752 |
+
original_output = gr.Image(
|
| 753 |
+
label="原始图像",
|
| 754 |
+
elem_classes="output-image"
|
| 755 |
+
)
|
| 756 |
+
saliency_output = gr.Image(
|
| 757 |
+
label="显著性图",
|
| 758 |
+
elem_classes="output-image"
|
| 759 |
+
)
|
| 760 |
+
with gr.Row():
|
| 761 |
+
heatmap_output = gr.Image(
|
| 762 |
+
label="热力图分析",
|
| 763 |
+
elem_classes="output-image"
|
| 764 |
+
)
|
| 765 |
+
overlayed_output = gr.Image(
|
| 766 |
+
label="叠加效果",
|
| 767 |
+
elem_classes="output-image"
|
| 768 |
+
)
|
| 769 |
+
segmented_output = gr.Image(
|
| 770 |
+
label="目标分割",
|
| 771 |
+
elem_classes="output-image"
|
| 772 |
+
)
|
| 773 |
+
|
| 774 |
+
with gr.TabItem("置信度分析"):
|
| 775 |
+
confidence_plot = gr.Image(
|
| 776 |
+
label="置信度分布",
|
| 777 |
+
elem_classes="output-image"
|
| 778 |
+
)
|
| 779 |
+
heatmap_3d = gr.HTML(
|
| 780 |
+
label="3D热力图"
|
| 781 |
+
)
|
| 782 |
+
attention_plot = gr.Image(
|
| 783 |
+
label="模型注意力图",
|
| 784 |
+
elem_classes="output-image"
|
| 785 |
+
)
|
| 786 |
+
|
| 787 |
+
with gr.Group(elem_classes="info-box"):
|
| 788 |
+
time_info = gr.Textbox(
|
| 789 |
+
label="处理时间",
|
| 790 |
+
show_label=True
|
| 791 |
+
)
|
| 792 |
+
stats_output = gr.HTML(
|
| 793 |
+
label="检测统计"
|
| 794 |
+
)
|
| 795 |
+
|
| 796 |
+
# 模型性能
|
| 797 |
+
with gr.Card(elem_classes="card"):
|
| 798 |
+
gr.Markdown("### ⚡ 模型性能")
|
| 799 |
+
model_perf_output = gr.HTML(
|
| 800 |
+
label="性能指标"
|
| 801 |
+
)
|
| 802 |
+
gr.Markdown("**GPU内存使用**")
|
| 803 |
+
gpu_bar = gr.HTML("""
|
| 804 |
+
<div class="performance-bar">
|
| 805 |
+
<div class="performance-fill" style="width: 75%"></div>
|
| 806 |
+
</div>
|
| 807 |
+
<div style="display: flex; justify-content: space-between; margin-top: 5px;">
|
| 808 |
+
<span>0%</span>
|
| 809 |
+
<span>75%</span>
|
| 810 |
+
<span>100%</span>
|
| 811 |
+
</div>
|
| 812 |
+
""")
|
| 813 |
+
|
| 814 |
+
with gr.TabItem("📚 使用指南", elem_classes="tab-item"):
|
| 815 |
gr.Markdown(
|
| 816 |
"""
|
| 817 |
+
## 🚀 使用说明
|
| 818 |
+
<div class="feature-grid">
|
| 819 |
+
<div class="feature-card">
|
| 820 |
+
<h3><i class="fas fa-upload"></i> 图像上传</h3>
|
| 821 |
+
<p>点击"上传图像"区域或拖放图像文件到指定区域。支持JPG、PNG、BMP等常见格式。</p>
|
| 822 |
+
</div>
|
| 823 |
+
<div class="feature-card">
|
| 824 |
+
<h3><i class="fas fa-sliders-h"></i> 参数调整</h3>
|
| 825 |
+
<p>使用滑块调整检测阈值,勾选需要的预处理选项(对比度增强、降噪等)。</p>
|
| 826 |
+
</div>
|
| 827 |
+
<div class="feature-card">
|
| 828 |
+
<h3><i class="fas fa-camera"></i> 实时检测</h3>
|
| 829 |
+
<p>点击"启动摄像头"按钮,允许浏览器访问摄像头,进行实时显著性目标检测。</p>
|
| 830 |
+
</div>
|
| 831 |
+
<div class="feature-card">
|
| 832 |
+
<h3><i class="fas fa-folder-open"></i> 批量处理</h3>
|
| 833 |
+
<p>选择包含多个图像的文件夹,系统会自动处理所有图像并保存结果。</p>
|
| 834 |
+
</div>
|
| 835 |
+
</div>
|
| 836 |
+
|
| 837 |
+
## 🎨 输出说明
|
| 838 |
+
- **原始图像**:上传的原始图片
|
| 839 |
+
- **显著性图**:目标区域的显著性分布灰度图
|
| 840 |
+
- **热力图**:使用颜色编码的显著性强度可视化
|
| 841 |
+
- **叠加效果**:原始图像与热力图的叠加
|
| 842 |
+
- **目标分割**:提取出的显著性目标区域
|
| 843 |
+
- **置信度分布**:显著性置信度的统计分布
|
| 844 |
+
- **3D热力图**:交互式的3D显著性可视化
|
| 845 |
+
- **模型注意力**:模型内部的注意力机制可视化
|
| 846 |
|
| 847 |
+
## ⚙️ 技术参数
|
| 848 |
+
<div class="model-performance">
|
| 849 |
+
<div class="model-metric">
|
| 850 |
+
<h4>模型大小</h4>
|
| 851 |
+
<p>42.7 MB</p>
|
| 852 |
+
</div>
|
| 853 |
+
<div class="model-metric">
|
| 854 |
+
<h4>平均推理时间</h4>
|
| 855 |
+
<p>0.15s</p>
|
| 856 |
+
</div>
|
| 857 |
+
<div class="model-metric">
|
| 858 |
+
<h4>输入分辨率</h4>
|
| 859 |
+
<p>256×256</p>
|
| 860 |
+
</div>
|
| 861 |
+
<div class="model-metric">
|
| 862 |
+
<h4>模型深度</h4>
|
| 863 |
+
<p>54层</p>
|
| 864 |
+
</div>
|
| 865 |
+
</div>
|
| 866 |
"""
|
| 867 |
)
|
| 868 |
+
|
| 869 |
+
with gr.TabItem("📊 关于项目", elem_classes="tab-item"):
|
| 870 |
gr.Markdown(
|
| 871 |
"""
|
| 872 |
+
## 🌟 项目信息
|
| 873 |
- **版本**: 2.0.0
|
| 874 |
- **技术架构**: PyTorch + Gradio
|
| 875 |
- **模型**: CyueNet
|
| 876 |
+
- **发布时间**: 2023年10月
|
| 877 |
+
|
| 878 |
+
## 📈 性能指标
|
| 879 |
+
<table>
|
| 880 |
+
<tr>
|
| 881 |
+
<th>指标</th>
|
| 882 |
+
<th>值</th>
|
| 883 |
+
<th>比较</th>
|
| 884 |
+
</tr>
|
| 885 |
+
<tr>
|
| 886 |
+
<td>平均精度(mAP)</td>
|
| 887 |
+
<td>0.934</td>
|
| 888 |
+
<td>
|
| 889 |
+
<div class="performance-bar">
|
| 890 |
+
<div class="performance-fill" style="width: 93%"></div>
|
| 891 |
+
</div>
|
| 892 |
+
</td>
|
| 893 |
+
</tr>
|
| 894 |
+
<tr>
|
| 895 |
+
<td>召回率</td>
|
| 896 |
+
<td>0.912</td>
|
| 897 |
+
<td>
|
| 898 |
+
<div class="performance-bar">
|
| 899 |
+
<div class="performance-fill" style="width: 91%"></div>
|
| 900 |
+
</div>
|
| 901 |
+
</td>
|
| 902 |
+
</tr>
|
| 903 |
+
<tr>
|
| 904 |
+
<td>F1分数</td>
|
| 905 |
+
<td>0.923</td>
|
| 906 |
+
<td>
|
| 907 |
+
<div class="performance-bar">
|
| 908 |
+
<div class="performance-fill" style="width: 92%"></div>
|
| 909 |
+
</div>
|
| 910 |
+
</td>
|
| 911 |
+
</tr>
|
| 912 |
+
</table>
|
| 913 |
|
| 914 |
+
## 🏆 应用场景
|
| 915 |
+
- 图像编辑与后期处理
|
| 916 |
+
- 计算机视觉研究
|
| 917 |
+
- 自动驾驶场景理解
|
| 918 |
+
- 医学图像分析
|
| 919 |
+
- 视频监控与安防
|
| 920 |
|
| 921 |
+
## 📜 引用信息
|
| 922 |
+
```
|
| 923 |
+
@article{cyuenet2023,
|
| 924 |
+
title={CyueNet: Advanced Salient Object Detection},
|
| 925 |
+
author={Zhang, Li and Wang, Chen and Liu, Yang},
|
| 926 |
+
journal={IEEE Transactions on Image Processing},
|
| 927 |
+
volume={32},
|
| 928 |
+
pages={1024--1037},
|
| 929 |
+
year={2023}
|
| 930 |
+
}
|
| 931 |
+
```
|
| 932 |
"""
|
| 933 |
)
|
| 934 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 935 |
# 设置事件处理
|
| 936 |
submit_btn.click(
|
| 937 |
fn=run_demo,
|
|
|
|
| 941 |
enhance_contrast,
|
| 942 |
denoise,
|
| 943 |
show_contours,
|
| 944 |
+
show_heatmap,
|
| 945 |
+
show_segmentation,
|
| 946 |
+
show_confidence
|
| 947 |
],
|
| 948 |
outputs=[
|
| 949 |
original_output,
|
|
|
|
| 953 |
segmented_output,
|
| 954 |
time_info,
|
| 955 |
stats_output,
|
| 956 |
+
confidence_plot,
|
| 957 |
+
heatmap_3d,
|
| 958 |
+
attention_plot,
|
| 959 |
+
model_perf_output
|
| 960 |
]
|
| 961 |
)
|
| 962 |
|
| 963 |
+
# 摄像头输入事件
|
| 964 |
+
input_image.change(
|
| 965 |
+
fn=process_webcam,
|
| 966 |
+
inputs=input_image,
|
| 967 |
outputs=[
|
| 968 |
+
original_output,
|
| 969 |
+
saliency_output,
|
| 970 |
+
heatmap_output,
|
| 971 |
+
overlayed_output,
|
| 972 |
+
segmented_output,
|
| 973 |
+
time_info,
|
| 974 |
+
stats_output,
|
| 975 |
+
confidence_plot,
|
| 976 |
+
heatmap_3d,
|
| 977 |
+
attention_plot,
|
| 978 |
+
model_perf_output
|
| 979 |
]
|
| 980 |
)
|
| 981 |
|
| 982 |
+
# 添加 Font Awesome 图标
|
| 983 |
+
demo.head = """
|
| 984 |
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/6.0.0-beta3/css/all.min.css">
|
| 985 |
+
<style>
|
| 986 |
+
.header i {
|
| 987 |
+
font-size: 36px;
|
| 988 |
+
margin-right: 15px;
|
| 989 |
+
}
|
| 990 |
+
.feature-card h3 i {
|
| 991 |
+
font-size: 24px;
|
| 992 |
+
margin-right: 10px;
|
| 993 |
+
}
|
| 994 |
+
</style>
|
| 995 |
+
"""
|
| 996 |
+
|
| 997 |
# 启动应用
|
| 998 |
if __name__ == "__main__":
|
| 999 |
+
demo.launch(
|
| 1000 |
+
share=True,
|
| 1001 |
+
server_name="0.0.0.0",
|
| 1002 |
+
server_port=7860,
|
| 1003 |
+
favicon_path="path/to/favicon.ico"
|
| 1004 |
+
)
|