kunkk commited on
Commit
e9f17fe
·
verified ·
1 Parent(s): 751a096

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -20
app.py CHANGED
@@ -9,12 +9,10 @@ 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
- import plotly.graph_objects as go
13
- import plotly.express as px
14
- import pandas as pd
15
 
16
- # 设置GPU/CPU
17
- device = torch.device('cuda:0' if torch.cuda.cuda.is_available() else 'cpu')
 
18
  custom_css = """
19
  :root {
20
  --primary-color: #2196F3;
@@ -140,6 +138,7 @@ custom_css = """
140
  margin: 15px 0;
141
  }
142
  """
 
143
  class ImageProcessor:
144
  def __init__(self):
145
  self.model = None
@@ -186,25 +185,31 @@ class ImageProcessor:
186
  return image
187
 
188
  def generate_analysis_plots(self, saliency_map):
189
- """生成分析图表"""
190
- # 直方图数据
191
- hist_data = saliency_map.flatten()
192
- fig_hist = px.histogram(hist_data, nbins=50,
193
- title="显著性分布直方图",
194
- labels={'value': '显著性值', 'count': '频率'})
 
 
195
 
196
- # 计算显著性统计
197
- regions = np.zeros_like(saliency_map)
198
- regions[saliency_map > np.mean(saliency_map)] = 1
 
199
 
200
- return fig_hist
 
 
 
201
 
202
  def process_image(self, image, threshold=0.5, testsize=256,
203
  enhance_contrast=False, denoise=False,
204
  brightness=0, contrast=0, filter_type="无"):
205
  """增强的图像处理函数"""
206
  if image is None:
207
- return [None] * 7 + ["请提供有效的图像"]
208
 
209
  # 图像预处理
210
  if denoise:
@@ -270,9 +275,10 @@ class ImageProcessor:
270
  analysis_plot = self.generate_analysis_plots(res_resized)
271
 
272
  # 计算统计信息
 
273
  stats = {
274
  "处理分辨率": f"{w}x{h}",
275
- "检测目标数量": str(len(cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0])),
276
  "平均置信度": f"{np.mean(res_resized):.2%}",
277
  "最大置信度": f"{np.max(res_resized):.2%}",
278
  "处理时间": f"{inference_time:.3f}秒"
@@ -440,8 +446,9 @@ with gr.Blocks(title="高级显著性目标检测系统", css=custom_css) as dem
440
  stats_output = gr.HTML(
441
  label="统计信息"
442
  )
443
- analysis_plot = gr.Plot(
444
- label="显著性分布分析"
 
445
  )
446
 
447
  with gr.TabItem("📖 使用指南"):
@@ -540,4 +547,4 @@ with gr.Blocks(title="高级显著性目标检测系统", css=custom_css) as dem
540
 
541
  # 启动应用
542
  if __name__ == "__main__":
543
- demo.launch(share=True)
 
9
  import matplotlib.pyplot as plt
10
  from model.CyueNet_models import MMS
11
  from utils1.data import transform_image
 
 
 
12
 
13
+ # 设置GPU/CPU - 修复了torch.cuda.is_available()的调用
14
+ device = torch.device('cuda:0' if torch.cuda.is_available() else 'cpu')
15
+
16
  custom_css = """
17
  :root {
18
  --primary-color: #2196F3;
 
138
  margin: 15px 0;
139
  }
140
  """
141
+
142
  class ImageProcessor:
143
  def __init__(self):
144
  self.model = None
 
185
  return image
186
 
187
  def generate_analysis_plots(self, saliency_map):
188
+ """生成分析图表 - 使用matplotlib替代plotly"""
189
+ # 创建直方图
190
+ plt.figure(figsize=(8, 4))
191
+ plt.hist(saliency_map.flatten(), bins=50, color='skyblue', edgecolor='black')
192
+ plt.title('显著性分布直方图')
193
+ plt.xlabel('显著性值')
194
+ plt.ylabel('频率')
195
+ plt.grid(axis='y', alpha=0.75)
196
 
197
+ # 保存到缓冲区
198
+ plt.tight_layout()
199
+ plt.savefig('temp_hist.png')
200
+ plt.close()
201
 
202
+ # 读取并返回图像
203
+ hist_img = cv2.imread('temp_hist.png')
204
+ hist_img = cv2.cvtColor(hist_img, cv2.COLOR_BGR2RGB)
205
+ return hist_img
206
 
207
  def process_image(self, image, threshold=0.5, testsize=256,
208
  enhance_contrast=False, denoise=False,
209
  brightness=0, contrast=0, filter_type="无"):
210
  """增强的图像处理函数"""
211
  if image is None:
212
+ return [None] * 8 + ["请提供有效的图像"]
213
 
214
  # 图像预处理
215
  if denoise:
 
275
  analysis_plot = self.generate_analysis_plots(res_resized)
276
 
277
  # 计算统计信息
278
+ contours = cv2.findContours(binary_mask, cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)[0]
279
  stats = {
280
  "处理分辨率": f"{w}x{h}",
281
+ "检测目标数量": str(len(contours)),
282
  "平均置信度": f"{np.mean(res_resized):.2%}",
283
  "最大置信度": f"{np.max(res_resized):.2%}",
284
  "处理时间": f"{inference_time:.3f}秒"
 
446
  stats_output = gr.HTML(
447
  label="统计信息"
448
  )
449
+ analysis_plot = gr.Image(
450
+ label="显著性分布分析",
451
+ elem_classes="output-image"
452
  )
453
 
454
  with gr.TabItem("📖 使用指南"):
 
547
 
548
  # 启动应用
549
  if __name__ == "__main__":
550
+ demo.launch(share=True)