Skip to content

Instantly share code, notes, and snippets.

@Xreath
Created July 4, 2023 06:39
Show Gist options
  • Save Xreath/ae54a3e2962fd8bbe300dc53e02961fd to your computer and use it in GitHub Desktop.
Save Xreath/ae54a3e2962fd8bbe300dc53e02961fd to your computer and use it in GitHub Desktop.
def post_process(self, outputs):
loc, conf, landmarks = outputs
height, width = 640, 640
priorbox = PriorBox(image_size=(height, width))
priors = priorbox.forward()
variance = [0.1, 0.2]
scale = np.array([width, height, width, height])
scale1 = np.array(
[width, height, width, height, width, height, width, height, width, height]
)
# bboxe
boxes = decode(loc[0], priors, variance)
boxes = boxes * scale
# score
scores = np.squeeze(conf, axis=0)[:, 1]
# landmark
landmarks = decode_landm(landmarks[0], priors, variance)
landmarks = landmarks * scale1
# ignore low scores
inds = np.where(scores > self.conf_threshold)[0]
boxes, landmarks, scores = boxes[inds], landmarks[inds], scores[inds]
# sort
order = scores.argsort()[::-1]
boxes, landmarks, scores = boxes[order], landmarks[order], scores[order]
# do NMS
bounding_boxes = np.hstack((boxes, scores[:, np.newaxis])).astype(
np.float32, copy=False
)
keep = nms_boxes(
bounding_boxes[:, :4], bounding_boxes[:, 4], self.nms_threshold
)
bounding_boxes, landmarks = bounding_boxes[keep, :], landmarks[keep]
return np.concatenate((bounding_boxes, landmarks), axis=1)
resizedImg = pre_process(mainImg)
start = int(round(time.time() * 1000))
outputs = ort_session.run(None, {"input": resizedImg})
bboxes = post_process(outputs)
_, all_landmarks_5 = get_face_landmarks_5(bboxes, eye_dist_threshold=5)
new_landmarks=find_landmark_orginal_image(mainImg,all_landmarks_5)
cropped_faces, affine_matrices = align_warp_face(mainImg, new_landmarks)
end = int(round(time.time() * 1000))
estimation_time = end - start
logger.info(f"'RetinaFace infer time: {estimation_time} ms")
save_faces(cropped_faces,"result_faces")
return cropped_faces, affine_matrices
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment