humanseg_postprocess.py 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. # coding: utf8
  2. # Copyright (c) 2020 PaddlePaddle Authors. All Rights Reserved.
  3. #
  4. # Licensed under the Apache License, Version 2.0 (the "License");
  5. # you may not use this file except in compliance with the License.
  6. # You may obtain a copy of the License at
  7. #
  8. # http://www.apache.org/licenses/LICENSE-2.0
  9. #
  10. # Unless required by applicable law or agreed to in writing, software
  11. # distributed under the License is distributed on an "AS IS" BASIS,
  12. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. # See the License for the specific language governing permissions and
  14. # limitations under the License.
  15. import numpy as np
  16. def human_seg_tracking(pre_gray, cur_gray, prev_cfd, dl_weights, disflow):
  17. """计算光流跟踪匹配点和光流图
  18. 输入参数:
  19. pre_gray: 上一帧灰度图
  20. cur_gray: 当前帧灰度图
  21. prev_cfd: 上一帧光流图
  22. dl_weights: 融合权重图
  23. disflow: 光流数据结构
  24. 返回值:
  25. is_track: 光流点跟踪二值图,即是否具有光流点匹配
  26. track_cfd: 光流跟踪图
  27. """
  28. check_thres = 8
  29. h, w = pre_gray.shape[:2]
  30. track_cfd = np.zeros_like(prev_cfd)
  31. is_track = np.zeros_like(pre_gray)
  32. flow_fw = disflow.calc(pre_gray, cur_gray, None)
  33. flow_bw = disflow.calc(cur_gray, pre_gray, None)
  34. flow_fw = np.round(flow_fw).astype(np.int)
  35. flow_bw = np.round(flow_bw).astype(np.int)
  36. y_list = np.array(range(h))
  37. x_list = np.array(range(w))
  38. yv, xv = np.meshgrid(y_list, x_list)
  39. yv, xv = yv.T, xv.T
  40. cur_x = xv + flow_fw[:, :, 0]
  41. cur_y = yv + flow_fw[:, :, 1]
  42. # 超出边界不跟踪
  43. not_track = (cur_x < 0) + (cur_x >= w) + (cur_y < 0) + (cur_y >= h)
  44. flow_bw[~not_track] = flow_bw[cur_y[~not_track], cur_x[~not_track]]
  45. not_track += (np.square(flow_fw[:, :, 0] + flow_bw[:, :, 0]) +
  46. np.square(flow_fw[:, :, 1] + flow_bw[:, :, 1])
  47. ) >= check_thres
  48. track_cfd[cur_y[~not_track], cur_x[~not_track]] = prev_cfd[~not_track]
  49. is_track[cur_y[~not_track], cur_x[~not_track]] = 1
  50. not_flow = np.all(np.abs(flow_fw) == 0,
  51. axis=-1) * np.all(np.abs(flow_bw) == 0, axis=-1)
  52. dl_weights[cur_y[not_flow], cur_x[not_flow]] = 0.05
  53. return track_cfd, is_track, dl_weights
  54. def human_seg_track_fuse(track_cfd, dl_cfd, dl_weights, is_track):
  55. """光流追踪图和人像分割结构融合
  56. 输入参数:
  57. track_cfd: 光流追踪图
  58. dl_cfd: 当前帧分割结果
  59. dl_weights: 融合权重图
  60. is_track: 光流点匹配二值图
  61. 返回
  62. cur_cfd: 光流跟踪图和人像分割结果融合图
  63. """
  64. fusion_cfd = dl_cfd.copy()
  65. is_track = is_track.astype(np.bool)
  66. fusion_cfd[is_track] = dl_weights[is_track] * dl_cfd[is_track] + (
  67. 1 - dl_weights[is_track]) * track_cfd[is_track]
  68. # 确定区域
  69. index_certain = ((dl_cfd > 0.9) + (dl_cfd < 0.1)) * is_track
  70. index_less01 = (dl_weights < 0.1) * index_certain
  71. fusion_cfd[index_less01] = 0.3 * dl_cfd[index_less01] + 0.7 * track_cfd[
  72. index_less01]
  73. index_larger09 = (dl_weights >= 0.1) * index_certain
  74. fusion_cfd[index_larger09] = 0.4 * dl_cfd[
  75. index_larger09] + 0.6 * track_cfd[index_larger09]
  76. return fusion_cfd
  77. def threshold_mask(img, thresh_bg, thresh_fg):
  78. dst = (img / 255.0 - thresh_bg) / (thresh_fg - thresh_bg)
  79. dst[np.where(dst > 1)] = 1
  80. dst[np.where(dst < 0)] = 0
  81. return dst.astype(np.float32)
  82. def postprocess(cur_gray, scoremap, prev_gray, pre_cfd, disflow, is_init):
  83. """光流优化
  84. Args:
  85. cur_gray : 当前帧灰度图
  86. pre_gray : 前一帧灰度图
  87. pre_cfd :前一帧融合结果
  88. scoremap : 当前帧分割结果
  89. difflow : 光流
  90. is_init : 是否第一帧
  91. Returns:
  92. fusion_cfd : 光流追踪图和预测结果融合图
  93. """
  94. h, w = scoremap.shape
  95. cur_cfd = scoremap.copy()
  96. if is_init:
  97. if h <= 64 or w <= 64:
  98. disflow.setFinestScale(1)
  99. elif h <= 160 or w <= 160:
  100. disflow.setFinestScale(2)
  101. else:
  102. disflow.setFinestScale(3)
  103. fusion_cfd = cur_cfd
  104. else:
  105. weights = np.ones((h, w), np.float32) * 0.3
  106. track_cfd, is_track, weights = human_seg_tracking(
  107. prev_gray, cur_gray, pre_cfd, weights, disflow)
  108. fusion_cfd = human_seg_track_fuse(track_cfd, cur_cfd, weights,
  109. is_track)
  110. return fusion_cfd