Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
pick = self.__nms(total_boxes.copy(), 0.7, 'Union')
total_boxes = total_boxes[pick, :]
regw = total_boxes[:, 2] - total_boxes[:, 0]
regh = total_boxes[:, 3] - total_boxes[:, 1]
qq1 = total_boxes[:, 0] + total_boxes[:, 5] * regw
qq2 = total_boxes[:, 1] + total_boxes[:, 6] * regh
qq3 = total_boxes[:, 2] + total_boxes[:, 7] * regw
qq4 = total_boxes[:, 3] + total_boxes[:, 8] * regh
total_boxes = np.transpose(np.vstack([qq1, qq2, qq3, qq4, total_boxes[:, 4]]))
total_boxes = self.__rerec(total_boxes.copy())
total_boxes[:, 0:4] = np.fix(total_boxes[:, 0:4]).astype(np.int32)
status = StageStatus(self.__pad(total_boxes.copy(), stage_status.width, stage_status.height),
width=stage_status.width, height=stage_status.height)
return total_boxes, status
def __stage3(self, img, total_boxes, stage_status: StageStatus):
"""
Third stage of the MTCNN.
:param img:
:param total_boxes:
:param stage_status:
:return:
"""
num_boxes = total_boxes.shape[0]
if num_boxes == 0:
return total_boxes, np.empty(shape=(0,))
total_boxes = np.fix(total_boxes).astype(np.int32)
status = StageStatus(self.__pad(total_boxes.copy(), stage_status.width, stage_status.height),
width=stage_status.width, height=stage_status.height)
tempimg = np.zeros((48, 48, 3, num_boxes))
for k in range(0, num_boxes):
tmp = np.zeros((int(status.tmph[k]), int(status.tmpw[k]), 3))
tmp[status.dy[k] - 1:status.edy[k], status.dx[k] - 1:status.edx[k], :] = \
img[status.y[k] - 1:status.ey[k], status.x[k] - 1:status.ex[k], :]
if tmp.shape[0] > 0 and tmp.shape[1] > 0 or tmp.shape[0] == 0 and tmp.shape[1] == 0:
tempimg[:, :, :, k] = cv2.resize(tmp, (48, 48), interpolation=cv2.INTER_AREA)
else:
return np.empty(shape=(0,)), np.empty(shape=(0,))
def detect_faces(self, img) -> list:
"""
Detects bounding boxes from the specified image.
:param img: image to process
:return: list containing all the bounding boxes detected with their keypoints.
"""
if img is None or not hasattr(img, "shape"):
raise InvalidImage("Image not valid.")
height, width, _ = img.shape
stage_status = StageStatus(width=width, height=height)
m = 12 / self._min_face_size
min_layer = np.amin([height, width]) * m
scales = self.__compute_scale_pyramid(m, min_layer)
stages = [self.__stage1, self.__stage2, self.__stage3]
result = [scales, stage_status]
# We pipe here each of the stages
for stage in stages:
result = stage(img, result[0], result[1])
[total_boxes, points] = result
bounding_boxes = []
pick = self.__nms(total_boxes.copy(), 0.7, 'Union')
total_boxes = total_boxes[pick, :]
regw = total_boxes[:, 2] - total_boxes[:, 0]
regh = total_boxes[:, 3] - total_boxes[:, 1]
qq1 = total_boxes[:, 0] + total_boxes[:, 5] * regw
qq2 = total_boxes[:, 1] + total_boxes[:, 6] * regh
qq3 = total_boxes[:, 2] + total_boxes[:, 7] * regw
qq4 = total_boxes[:, 3] + total_boxes[:, 8] * regh
total_boxes = np.transpose(np.vstack([qq1, qq2, qq3, qq4, total_boxes[:, 4]]))
total_boxes = self.__rerec(total_boxes.copy())
total_boxes[:, 0:4] = np.fix(total_boxes[:, 0:4]).astype(np.int32)
status = StageStatus(self.__pad(total_boxes.copy(), stage_status.width, stage_status.height),
width=stage_status.width, height=stage_status.height)
return total_boxes, status
def detect_faces(self, img) -> list:
"""
Detects bounding boxes from the specified image.
:param img: image to process
:return: list containing all the bounding boxes detected with their keypoints.
"""
if img is None or not hasattr(img, "shape"):
raise InvalidImage("Image not valid.")
height, width, _ = img.shape
stage_status = StageStatus(width=width, height=height)
m = 12 / self.__min_face_size
min_layer = np.amin([height, width]) * m
scales = self.__compute_scale_pyramid(m, min_layer)
stages = [self.__stage1, self.__stage2, self.__stage3]
result = [scales, stage_status]
# We pipe here each of the stages
for stage in stages:
result = stage(img, result[0], result[1])
[total_boxes, points] = result
bounding_boxes = []
def __stage3(self, img, total_boxes, stage_status: StageStatus):
"""
Third stage of the MTCNN.
:param img:
:param total_boxes:
:param stage_status:
:return:
"""
num_boxes = total_boxes.shape[0]
if num_boxes == 0:
return total_boxes, np.empty(shape=(0,))
total_boxes = np.fix(total_boxes).astype(np.int32)
status = StageStatus(self.__pad(total_boxes.copy(), stage_status.width, stage_status.height),
width=stage_status.width, height=stage_status.height)
tempimg = np.zeros((48, 48, 3, num_boxes))
for k in range(0, num_boxes):
tmp = np.zeros((int(status.tmph[k]), int(status.tmpw[k]), 3))
tmp[status.dy[k] - 1:status.edy[k], status.dx[k] - 1:status.edx[k], :] = \
img[status.y[k] - 1:status.ey[k], status.x[k] - 1:status.ex[k], :]
if tmp.shape[0] > 0 and tmp.shape[1] > 0 or tmp.shape[0] == 0 and tmp.shape[1] == 0:
tempimg[:, :, :, k] = cv2.resize(tmp, (48, 48), interpolation=cv2.INTER_AREA)
else:
return np.empty(shape=(0,)), np.empty(shape=(0,))