detector.py 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import cv2
  2. import numpy as np
  3. import typing as T
  4. from munch import munchify
  5. from collections import namedtuple
  6. from skimage import filters
  7. # the coordinates are relative!
  8. BBoxInfo = namedtuple("BBoxInfo", "area ratio mean std selected", defaults=[-1, -1, -1, -1, False])
  9. Detection = namedtuple("Detection", "bbox info")
  10. class BBox(namedtuple("BBox", "x0 y0 x1 y1")):
  11. __slots__ = ()
  12. @property
  13. def w(self):
  14. return abs(self.x1 - self.x0)
  15. @property
  16. def h(self):
  17. return abs(self.y1 - self.y0)
  18. @property
  19. def area(self):
  20. return self.h * self.w
  21. @property
  22. def ratio(self):
  23. return min(self.h, self.w) / max(self.h, self.w)
  24. def crop(self, im: np.ndarray, enlarge: bool = True):
  25. x0, y0, x1, y1 = self
  26. H, W, *_ = im.shape
  27. # translate from relative coordinates to pixel
  28. # coordinates for the given image
  29. x0, x1 = int(x0 * W), int(x1 * W)
  30. y0, y1 = int(y0 * H), int(y1 * H)
  31. # enlarge to a square extent
  32. if enlarge:
  33. h, w = int(self.h * H), int(self.h * W)
  34. size = max(h, w)
  35. dw, dh = (size - w) / 2, (size - h) / 2
  36. x0, x1 = max(int(x0 - dw), 0), int(x0 - dw + size)
  37. y0, y1 = max(int(y0 - dh), 0), int(y0 - dh + size)
  38. if im.ndim == 2:
  39. return im[y0:y1, x0:x1]
  40. elif im.ndim == 3:
  41. return im[y0:y1, x0:x1, :]
  42. else:
  43. ValueError(f"Unsupported ndims: {im.ndims=}")
  44. class Detector(object):
  45. def __init__(self, configuration: T.Dict[str, T.Dict]) -> None:
  46. super().__init__()
  47. config = munchify(configuration)
  48. self.scale: float = config.preprocess.scale
  49. self.min_size: int = config.preprocess.min_size
  50. self.sigma: float = config.preprocess.sigma
  51. self.block_size_scale: float = config.threshold.block_size_scale
  52. self.dilate_iterations: int = config.postprocess.dilate_iterations
  53. self.kernel_size: int = config.postprocess.kernel_size
  54. def __call__(self, im: np.ndarray) -> T.List[Detection]:
  55. _im = self.rescale(im)
  56. im0 = self.preprocess(_im)
  57. im1 = self.threshold(im0)
  58. im2 = self.postprocess(im1)
  59. bboxes = self.detect(im2)
  60. return self.postprocess_boxes(_im, bboxes)
  61. def detect(self, im: np.ndarray) -> T.List[BBox]:
  62. contours, hierarchy = cv2.findContours(im, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
  63. contours = sorted(contours, key=cv2.contourArea, reverse=True)
  64. return [_contour2bbox(c, im.shape) for c in contours]
  65. def rescale(self, im: np.ndarray) -> np.ndarray:
  66. H, W = im.shape
  67. _scale = self.min_size / min(H, W)
  68. scale = max(self.scale, min(1, _scale))
  69. size = int(W * scale), int(H * scale)
  70. return cv2.resize(im, dsize=size)
  71. def preprocess(self, im: np.ndarray) -> np.ndarray:
  72. res = filters.gaussian(im, sigma=self.sigma, preserve_range=True)
  73. return res.astype(im.dtype)
  74. def threshold(self, im: np.ndarray) -> np.ndarray:
  75. block_size_scale = self.block_size_scale
  76. # make block size an odd number
  77. block_size = min(im.shape) * block_size_scale // 2 * 2 + 1
  78. thresh = filters.threshold_local(im,
  79. block_size=block_size,
  80. mode="constant",
  81. )
  82. max_value = 255
  83. bin_im = ((im > thresh) * max_value).astype(np.uint8)
  84. return max_value - bin_im
  85. def postprocess(self, im: np.ndarray) -> np.ndarray:
  86. kernel_size = self.kernel_size
  87. iterations = self.dilate_iterations
  88. kernel = np.ones((kernel_size, kernel_size), dtype=np.uint8)
  89. im = cv2.morphologyEx(im, cv2.MORPH_OPEN, kernel)
  90. im = cv2.morphologyEx(im, cv2.MORPH_CLOSE, kernel)
  91. if iterations >= 1:
  92. im = cv2.erode(im, kernel, iterations=iterations)
  93. im = cv2.dilate(im, kernel, iterations=iterations)
  94. return im
  95. def postprocess_boxes(self, im: np.ndarray, bboxes: T.List[BBox]):
  96. detections = [Detection(bbox, BBoxInfo()) for bbox in bboxes]
  97. _im = im.astype(np.float64) / 255.
  98. integral, integral_sq = cv2.integral2(_im)
  99. # im_mean, im_std, im_n = _im_mean_std(integral, integral_sq)
  100. inds = cv2.dnn.NMSBoxes([[x0, y0, x1-x0, y1-y0] for (x0, y0, x1, y1) in bboxes],
  101. np.ones(len(bboxes), dtype=np.float32),
  102. score_threshold=0.99,
  103. nms_threshold=0.1,
  104. )
  105. # calculate the BBoxInfos only for the selected and update the detections
  106. for i in inds.squeeze():
  107. bbox, _ = detections[i]
  108. mean, std, n = _im_mean_std(integral, integral_sq, bbox)
  109. area, ratio = bbox.area, bbox.ratio
  110. selected = self.is_selected(mean, std, ratio, area)
  111. info = BBoxInfo(mean, std, area, ratio, selected)
  112. detections[i] = Detection(bbox, info)
  113. return detections
  114. def is_selected(self, mean: float, std: float, ratio: float, area: float) -> bool:
  115. # Caution, here are some magic numbers!
  116. return \
  117. std >= 5e-2 and \
  118. ratio >= 2.5e-1 and \
  119. 4e-4 <= area <= 1/9
  120. def _contour2bbox(contour: np.ndarray, shape: T.Tuple[int, int]) -> BBox:
  121. """ Gets the maximal extent of a contour and translates it to a bounding box. """
  122. x0, y0 = contour.min(axis=0)[0].astype(np.int32)
  123. x1, y1 = contour.max(axis=0)[0].astype(np.int32)
  124. h, w = shape
  125. return BBox(x0/w, y0/h, x1/w, y1/h)
  126. def _im_mean_std(integral: np.ndarray,
  127. integral_sq: np.ndarray,
  128. bbox: T.Optional[BBox] = None
  129. ) -> T.Tuple[float, float, int]:
  130. h, w = integral.shape[0] - 1, integral.shape[1] - 1
  131. if bbox is None:
  132. arr_sum = integral[-1, -1]
  133. arr_sum_sq = integral_sq[-1, -1]
  134. N = h * w
  135. else:
  136. x0, y0, x1, y1 = bbox
  137. x0, x1 = int(x0 * w), int(x1 * w)
  138. y0, y1 = int(y0 * h), int(y1 * h)
  139. A, B, C, D = (y0,x0), (y1,x0), (y0,x1), (y1,x1)
  140. arr_sum = integral[D] + integral[A] - integral[B] - integral[C]
  141. arr_sum_sq = integral_sq[D] + integral_sq[A] - integral_sq[B] - integral_sq[C]
  142. N = (x1-x0) * (y1-y0)
  143. arr_mean = arr_sum / N
  144. arr_std = np.sqrt((arr_sum_sq - (arr_sum**2) / N) / N)
  145. return arr_mean, arr_std, N