djl234 commited on
Commit
77df495
·
verified ·
1 Parent(s): 3669afd

Update app.py

Browse files

[fix]more assertion

Files changed (1) hide show
  1. app.py +31 -13
app.py CHANGED
@@ -32,28 +32,46 @@ def generate_mask(img, coord):
32
  #x, y = map(int, coord.split(','))
33
 
34
  #
35
- mask = sepia(img,img,img,img,img, stack_image=False)
36
  return overlay_mask(img, mask)
37
 
38
  def overlay_mask(img, mask):
39
- # 尺寸对齐
40
- mask_resized = cv2.resize(mask, (img.shape[1], img.shape[0])) # 匹配原图尺寸
 
41
 
42
- # 通道数转换
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
43
  if len(mask_resized.shape) == 2:
44
  mask_rgb = cv2.cvtColor(mask_resized, cv2.COLOR_GRAY2BGR)
45
  else:
46
  mask_rgb = mask_resized
47
 
48
- # 归一化处理(参考网页5的医学图像处理方案)
49
- mask_normalized = mask_rgb.astype(np.float32) / 255.0
50
- img_normalized = img.astype(np.float32) / 255.0
51
-
52
- # 带透明度叠加(根据网页3的工业标准)
53
- alpha = 0.7 # 原图可见度
54
- blended = cv2.addWeighted(img_normalized, alpha,
55
- mask_normalized, 1-alpha, 0)
56
- return (blended * 255).astype(np.uint8)
57
 
58
  def create_mode2_interface():
59
  with gr.Blocks() as mode2:
 
32
  #x, y = map(int, coord.split(','))
33
 
34
  #
35
+ mask = sepia(img,img*0.999999,img*0.999999,img*0.999999,img*0.999999, stack_image=False)
36
  return overlay_mask(img, mask)
37
 
38
  def overlay_mask(img, mask):
39
+ # 前置校验(参考网页1数据验证方案)
40
+ assert mask is not None, "掩码数据为空"
41
+ assert img.size > 0 and mask.size > 0, "输入图像或掩码尺寸异常"
42
 
43
+ # 数据类型强制转换(根据网页2的浮点型要求)
44
+ mask_float = mask.astype(np.float32) if mask.dtype != np.float32 else mask
45
+
46
+ # 维度压缩(应对4D数组问题)
47
+ if mask_float.ndim == 4:
48
+ mask_float = np.squeeze(mask_float, axis=(0,1)) # 去除批次和通道维度
49
+
50
+ # 尺寸调整(参考网页3的最佳实践)
51
+ try:
52
+ mask_resized = cv2.resize(
53
+ mask_float,
54
+ (img.shape[1], img.shape[0]),
55
+ interpolation=cv2.INTER_CUBIC # 使用高阶插值保持缺陷边缘
56
+ )
57
+ except cv2.error as e:
58
+ print(f"Resize参数异常: 原图尺寸{img.shape} 掩码尺寸{mask.shape}")
59
+ raise
60
+
61
+ # 通道统一(三通道对齐)
62
  if len(mask_resized.shape) == 2:
63
  mask_rgb = cv2.cvtColor(mask_resized, cv2.COLOR_GRAY2BGR)
64
  else:
65
  mask_rgb = mask_resized
66
 
67
+ # 归一化处理
68
+ return cv2.addWeighted(
69
+ img.astype(np.float32)/255,
70
+ 0.7,
71
+ mask_rgb.astype(np.float32)/255,
72
+ 0.3,
73
+ 0
74
+ ).astype(np.uint8)
 
75
 
76
  def create_mode2_interface():
77
  with gr.Blocks() as mode2: