bdsqlsz commited on
Commit
58db293
1 Parent(s): 3dfc879

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +8 -20
app.py CHANGED
@@ -38,33 +38,21 @@ def fn(image):
38
  pred_score_thr=instance_thres
39
  )
40
 
41
- drawed = img.copy()
42
- im_h, im_w = img.shape[:2]
43
 
44
  # instances.bboxes, instances.masks will be None, None if no obj is detected
45
  if instances.bboxes is None:
46
- return Image.fromarray(drawed[..., ::-1])
47
 
48
  for ii, (xywh, mask) in enumerate(zip(instances.bboxes, instances.masks)):
49
- color = get_color(ii)
 
50
 
51
- mask_alpha = 0.5
52
- linewidth = max(round(sum(img.shape) / 2 * 0.003), 2)
53
 
54
- # draw bbox
55
- p1, p2 = (int(xywh[0]), int(xywh[1])), (int(xywh[2] + xywh[0]), int(xywh[3] + xywh[1]))
56
- cv2.rectangle(drawed, p1, p2, color, thickness=linewidth, lineType=cv2.LINE_AA)
57
-
58
- # draw mask
59
- p = mask.astype(np.float32)
60
- blend_mask = np.full((im_h, im_w, 3), color, dtype=np.float32)
61
- alpha_msk = (mask_alpha * p)[..., None]
62
- alpha_ori = 1 - alpha_msk
63
- drawed = drawed * alpha_ori + alpha_msk * blend_mask
64
-
65
- drawed = drawed.astype(np.uint8)
66
-
67
- return Image.fromarray(drawed[..., ::-1])
68
 
69
  iface = gr.Interface(
70
  # design titles and text descriptions
 
38
  pred_score_thr=instance_thres
39
  )
40
 
41
+ # 创建一个空白的白色图像,和原图大小一致
42
+ white = np.full_like(img, 255)
43
 
44
  # instances.bboxes, instances.masks will be None, None if no obj is detected
45
  if instances.bboxes is None:
46
+ return Image.fromarray(white)
47
 
48
  for ii, (xywh, mask) in enumerate(zip(instances.bboxes, instances.masks)):
49
+ # 把mask转换为bool类型,方便后续操作
50
+ mask = mask.astype(np.bool)
51
 
52
+ # 用原图中对应的区域替换白色图像中的区域,实现去除背景的效果
53
+ white[mask] = img[mask]
54
 
55
+ return Image.fromarray(white[..., ::-1])
 
 
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  iface = gr.Interface(
58
  # design titles and text descriptions