utils.py 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. # Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
  2. #
  3. # Licensed under the Apache License, Version 2.0 (the "License");
  4. # you may not use this file except in compliance with the License.
  5. # You may obtain a copy of the License at
  6. #
  7. # http://www.apache.org/licenses/LICENSE-2.0
  8. #
  9. # Unless required by applicable law or agreed to in writing, software
  10. # distributed under the License is distributed on an "AS IS" BASIS,
  11. # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. # See the License for the specific language governing permissions and
  13. # limitations under the License.
  14. import os
  15. import cv2
  16. import time
  17. import paddle
  18. import numpy as np
  19. __all__ = [
  20. 'Timer',
  21. 'Detection',
  22. 'load_det_results',
  23. 'preprocess_reid',
  24. 'get_crops',
  25. 'clip_box',
  26. 'scale_coords',
  27. ]
  28. class Timer(object):
  29. """
  30. This class used to compute and print the current FPS while evaling.
  31. """
  32. def __init__(self):
  33. self.total_time = 0.
  34. self.calls = 0
  35. self.start_time = 0.
  36. self.diff = 0.
  37. self.average_time = 0.
  38. self.duration = 0.
  39. def tic(self):
  40. # using time.time instead of time.clock because time time.clock
  41. # does not normalize for multithreading
  42. self.start_time = time.time()
  43. def toc(self, average=True):
  44. self.diff = time.time() - self.start_time
  45. self.total_time += self.diff
  46. self.calls += 1
  47. self.average_time = self.total_time / self.calls
  48. if average:
  49. self.duration = self.average_time
  50. else:
  51. self.duration = self.diff
  52. return self.duration
  53. def clear(self):
  54. self.total_time = 0.
  55. self.calls = 0
  56. self.start_time = 0.
  57. self.diff = 0.
  58. self.average_time = 0.
  59. self.duration = 0.
  60. class Detection(object):
  61. """
  62. This class represents a bounding box detection in a single image.
  63. Args:
  64. tlwh (ndarray): Bounding box in format `(top left x, top left y,
  65. width, height)`.
  66. confidence (ndarray): Detector confidence score.
  67. feature (Tensor): A feature vector that describes the object
  68. contained in this image.
  69. """
  70. def __init__(self, tlwh, confidence, feature):
  71. self.tlwh = np.asarray(tlwh, dtype=np.float32)
  72. self.confidence = np.asarray(confidence, dtype=np.float32)
  73. self.feature = feature
  74. def to_tlbr(self):
  75. """
  76. Convert bounding box to format `(min x, min y, max x, max y)`, i.e.,
  77. `(top left, bottom right)`.
  78. """
  79. ret = self.tlwh.copy()
  80. ret[2:] += ret[:2]
  81. return ret
  82. def to_xyah(self):
  83. """
  84. Convert bounding box to format `(center x, center y, aspect ratio,
  85. height)`, where the aspect ratio is `width / height`.
  86. """
  87. ret = self.tlwh.copy()
  88. ret[:2] += ret[2:] / 2
  89. ret[2] /= ret[3]
  90. return ret
  91. def load_det_results(det_file, num_frames):
  92. assert os.path.exists(det_file) and os.path.isfile(det_file), \
  93. 'Error: det_file: {} not exist or not a file.'.format(det_file)
  94. labels = np.loadtxt(det_file, dtype='float32', delimiter=',')
  95. results_list = []
  96. for frame_i in range(0, num_frames):
  97. results = {'bbox': [], 'score': []}
  98. lables_with_frame = labels[labels[:, 0] == frame_i + 1]
  99. for l in lables_with_frame:
  100. results['bbox'].append(l[1:5])
  101. results['score'].append(l[5])
  102. results_list.append(results)
  103. return results_list
  104. def scale_coords(coords, input_shape, im_shape, scale_factor):
  105. im_shape = im_shape.numpy()[0]
  106. ratio = scale_factor[0][0]
  107. pad_w = (input_shape[1] - int(im_shape[1])) / 2
  108. pad_h = (input_shape[0] - int(im_shape[0])) / 2
  109. coords = paddle.cast(coords, 'float32')
  110. coords[:, 0::2] -= pad_w
  111. coords[:, 1::2] -= pad_h
  112. coords[:, 0:4] /= ratio
  113. coords[:, :4] = paddle.clip(coords[:, :4], min=0, max=coords[:, :4].max())
  114. return coords.round()
  115. def clip_box(xyxy, input_shape, im_shape, scale_factor):
  116. im_shape = im_shape.numpy()[0]
  117. ratio = scale_factor.numpy()[0][0]
  118. img0_shape = [int(im_shape[0] / ratio), int(im_shape[1] / ratio)]
  119. xyxy[:, 0::2] = paddle.clip(xyxy[:, 0::2], min=0, max=img0_shape[1])
  120. xyxy[:, 1::2] = paddle.clip(xyxy[:, 1::2], min=0, max=img0_shape[0])
  121. return xyxy
  122. def get_crops(xyxy, ori_img, pred_scores, w, h):
  123. crops = []
  124. keep_scores = []
  125. xyxy = xyxy.numpy().astype(np.int64)
  126. ori_img = ori_img.numpy()
  127. ori_img = np.squeeze(ori_img, axis=0).transpose(1, 0, 2)
  128. pred_scores = pred_scores.numpy()
  129. for i, bbox in enumerate(xyxy):
  130. if bbox[2] <= bbox[0] or bbox[3] <= bbox[1]:
  131. continue
  132. crop = ori_img[bbox[0]:bbox[2], bbox[1]:bbox[3], :]
  133. crops.append(crop)
  134. keep_scores.append(pred_scores[i])
  135. if len(crops) == 0:
  136. return [], []
  137. crops = preprocess_reid(crops, w, h)
  138. return crops, keep_scores
  139. def preprocess_reid(imgs,
  140. w=64,
  141. h=192,
  142. mean=[0.485, 0.456, 0.406],
  143. std=[0.229, 0.224, 0.225]):
  144. im_batch = []
  145. for img in imgs:
  146. img = cv2.resize(img, (w, h))
  147. img = img[:, :, ::-1].astype('float32').transpose((2, 0, 1)) / 255
  148. img_mean = np.array(mean).reshape((3, 1, 1))
  149. img_std = np.array(std).reshape((3, 1, 1))
  150. img -= img_mean
  151. img /= img_std
  152. img = np.expand_dims(img, axis=0)
  153. im_batch.append(img)
  154. im_batch = np.concatenate(im_batch, 0)
  155. return im_batch