Spaces:
Sleeping
Sleeping
Update app.py
Browse files
app.py
CHANGED
|
@@ -1,15 +1,18 @@
|
|
| 1 |
-
import gradio as gr
|
| 2 |
import torch
|
| 3 |
import torch.nn.functional as F
|
| 4 |
import numpy as np
|
| 5 |
import os
|
| 6 |
import time
|
|
|
|
| 7 |
import cv2
|
| 8 |
from PIL import Image
|
| 9 |
from model.CyueNet_models import MMS
|
| 10 |
from utils1.data import transform_image
|
| 11 |
|
| 12 |
-
#
|
|
|
|
|
|
|
|
|
|
| 13 |
custom_css = """
|
| 14 |
.gradio-container {
|
| 15 |
background: linear-gradient(to right, #f6f8fa, #ffffff);
|
|
@@ -33,10 +36,77 @@ custom_css = """
|
|
| 33 |
}
|
| 34 |
"""
|
| 35 |
|
| 36 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 37 |
|
| 38 |
def run_demo(input_image, threshold):
|
| 39 |
-
"""
|
| 40 |
if input_image is None:
|
| 41 |
return [None] * 6 + ["请上传图片"]
|
| 42 |
|
|
@@ -46,12 +116,19 @@ def run_demo(input_image, threshold):
|
|
| 46 |
)
|
| 47 |
|
| 48 |
# 计算检测区域占比
|
| 49 |
-
|
| 50 |
-
|
|
|
|
|
|
|
|
|
|
| 51 |
|
| 52 |
return original, saliency_map, heatmap, overlayed, segmented, time_info, area_info
|
| 53 |
|
| 54 |
-
#
|
|
|
|
|
|
|
|
|
|
|
|
|
| 55 |
with gr.Blocks(title="高级显著性目标检测系统", css=custom_css) as demo:
|
| 56 |
gr.Markdown(
|
| 57 |
"""
|
|
|
|
|
|
|
| 1 |
import torch
|
| 2 |
import torch.nn.functional as F
|
| 3 |
import numpy as np
|
| 4 |
import os
|
| 5 |
import time
|
| 6 |
+
import gradio as gr
|
| 7 |
import cv2
|
| 8 |
from PIL import Image
|
| 9 |
from model.CyueNet_models import MMS
|
| 10 |
from utils1.data import transform_image
|
| 11 |
|
| 12 |
+
# 设置GPU/CPU
|
| 13 |
+
device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
|
| 14 |
+
|
| 15 |
+
# 自定义CSS
|
| 16 |
custom_css = """
|
| 17 |
.gradio-container {
|
| 18 |
background: linear-gradient(to right, #f6f8fa, #ffffff);
|
|
|
|
| 36 |
}
|
| 37 |
"""
|
| 38 |
|
| 39 |
+
def load_model():
|
| 40 |
+
"""加载预训练的模型"""
|
| 41 |
+
model = MMS()
|
| 42 |
+
try:
|
| 43 |
+
model.load_state_dict(torch.load('models/CyueNet_EORSSD6.pth.54', map_location=device))
|
| 44 |
+
print("模型加载成功")
|
| 45 |
+
except RuntimeError as e:
|
| 46 |
+
print(f"加载状态字典时出现部分不匹配,错误信息: {e}")
|
| 47 |
+
model.to(device)
|
| 48 |
+
model.eval()
|
| 49 |
+
return model
|
| 50 |
+
|
| 51 |
+
def process_image(image, model, threshold=0.5, testsize=256):
|
| 52 |
+
"""处理图像并返回显著性检测结果"""
|
| 53 |
+
if image is None:
|
| 54 |
+
return None, None, None, None, None, "请提供有效的图像"
|
| 55 |
+
|
| 56 |
+
# 保存原始图像
|
| 57 |
+
original_image = image.copy()
|
| 58 |
+
|
| 59 |
+
# 预处理图像
|
| 60 |
+
image_pil = Image.fromarray(image).convert('RGB')
|
| 61 |
+
image_tensor = transform_image(image_pil, testsize)
|
| 62 |
+
image_tensor = image_tensor.unsqueeze(0)
|
| 63 |
+
image_tensor = image_tensor.to(device)
|
| 64 |
+
|
| 65 |
+
# 计时
|
| 66 |
+
time_start = time.time()
|
| 67 |
+
|
| 68 |
+
# 推理
|
| 69 |
+
with torch.no_grad():
|
| 70 |
+
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 = model(image_tensor)
|
| 71 |
+
|
| 72 |
+
time_end = time.time()
|
| 73 |
+
inference_time = time_end - time_start
|
| 74 |
+
|
| 75 |
+
# 处理输出结果
|
| 76 |
+
res = res.sigmoid().data.cpu().numpy().squeeze()
|
| 77 |
+
res = (res - res.min()) / (res.max() - res.min() + 1e-8)
|
| 78 |
+
|
| 79 |
+
# 调整大小
|
| 80 |
+
h, w = original_image.shape[:2]
|
| 81 |
+
res_resized = cv2.resize(res, (w, h))
|
| 82 |
+
|
| 83 |
+
# 应用阈值
|
| 84 |
+
res_vis = (res_resized * 255).astype(np.uint8)
|
| 85 |
+
|
| 86 |
+
# 创建热力图
|
| 87 |
+
heatmap = cv2.applyColorMap(res_vis, cv2.COLORMAP_JET)
|
| 88 |
+
|
| 89 |
+
# 叠加结果
|
| 90 |
+
alpha = 0.5
|
| 91 |
+
if len(original_image.shape) == 3 and original_image.shape[2] == 3:
|
| 92 |
+
original_bgr = cv2.cvtColor(original_image, cv2.COLOR_RGB2BGR)
|
| 93 |
+
else:
|
| 94 |
+
original_bgr = cv2.cvtColor(original_image, cv2.COLOR_GRAY2BGR)
|
| 95 |
+
|
| 96 |
+
overlayed = cv2.addWeighted(original_bgr, 1-alpha, heatmap, alpha, 0)
|
| 97 |
+
|
| 98 |
+
# 使用阈值进行二值化
|
| 99 |
+
_, binary_mask = cv2.threshold(res_vis, int(255 * threshold), 255, cv2.THRESH_BINARY)
|
| 100 |
+
segmented = cv2.bitwise_and(original_bgr, original_bgr, mask=binary_mask)
|
| 101 |
+
|
| 102 |
+
# 转回RGB格式
|
| 103 |
+
overlayed_rgb = cv2.cvtColor(overlayed, cv2.COLOR_BGR2RGB)
|
| 104 |
+
segmented_rgb = cv2.cvtColor(segmented, cv2.COLOR_BGR2RGB)
|
| 105 |
+
|
| 106 |
+
return original_image, res_vis, heatmap, overlayed_rgb, segmented_rgb, f"推理时间: {inference_time:.4f}秒"
|
| 107 |
|
| 108 |
def run_demo(input_image, threshold):
|
| 109 |
+
"""Gradio界面的主函数"""
|
| 110 |
if input_image is None:
|
| 111 |
return [None] * 6 + ["请上传图片"]
|
| 112 |
|
|
|
|
| 116 |
)
|
| 117 |
|
| 118 |
# 计算检测区域占比
|
| 119 |
+
if saliency_map is not None:
|
| 120 |
+
mask_area = np.sum(saliency_map > 127) / (saliency_map.shape[0] * saliency_map.shape[1])
|
| 121 |
+
area_info = f"检测区域占比: {mask_area:.2%}"
|
| 122 |
+
else:
|
| 123 |
+
area_info = "无法计算区域占比"
|
| 124 |
|
| 125 |
return original, saliency_map, heatmap, overlayed, segmented, time_info, area_info
|
| 126 |
|
| 127 |
+
# 加载模型
|
| 128 |
+
print("正在加载模型...")
|
| 129 |
+
model = load_model()
|
| 130 |
+
|
| 131 |
+
# 创建Gradio界面
|
| 132 |
with gr.Blocks(title="高级显著性目标检测系统", css=custom_css) as demo:
|
| 133 |
gr.Markdown(
|
| 134 |
"""
|