Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: MTCNN模型人脸检测针对不合规图片增加异常处理 #202

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 33 additions & 30 deletions hivision/creator/face_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,40 @@ def detect_face_mtcnn(ctx: Context, scale: int = 2):
(ctx.origin_image.shape[1] // scale, ctx.origin_image.shape[0] // scale),
interpolation=cv2.INTER_AREA,
)
# landmarks 是 5 个关键点,分别是左眼、右眼、鼻子、左嘴角、右嘴角,
faces, landmarks = mtcnn.detect(image, thresholds=[0.8, 0.8, 0.8])

# print(len(faces))
if len(faces) != 1:
# 保险措施,如果检测到多个人脸或者没有人脸,用原图再检测一次
faces, landmarks = mtcnn.detect(ctx.origin_image)
try:
# landmarks 是 5 个关键点,分别是左眼、右眼、鼻子、左嘴角、右嘴角,
faces, landmarks = mtcnn.detect(image, thresholds=[0.8, 0.8, 0.8])
except Exception as e:
raise FaceError("Failed to detect face: {}".format(str(e)), str(e))
else:
# 如果只有一个人脸,将人脸坐标放大
for item, param in enumerate(faces[0]):
faces[0][item] = param * 2
if len(faces) != 1:
raise FaceError("Expected 1 face, but got {}".format(len(faces)), len(faces))

# 计算人脸坐标
left = faces[0][0]
top = faces[0][1]
width = faces[0][2] - left + 1
height = faces[0][3] - top + 1
ctx.face["rectangle"] = (left, top, width, height)

# 根据landmarks计算人脸偏转角度,以眼睛为标准,计算的人脸偏转角度,用于人脸矫正
# 示例landmarks [106.37181 150.77415 127.21012 108.369156 144.61522 105.24723 107.45625 133.62355 151.24269 153.34407 ]
landmarks = landmarks[0]
left_eye = np.array([landmarks[0], landmarks[5]])
right_eye = np.array([landmarks[1], landmarks[6]])
dy = right_eye[1] - left_eye[1]
dx = right_eye[0] - left_eye[0]
roll_angle = np.degrees(np.arctan2(dy, dx))

ctx.face["roll_angle"] = roll_angle
# print(len(faces))
if len(faces) != 1:
# 保险措施,如果检测到多个人脸或者没有人脸,用原图再检测一次
faces, landmarks = mtcnn.detect(ctx.origin_image)
else:
# 如果只有一个人脸,将人脸坐标放大
for item, param in enumerate(faces[0]):
faces[0][item] = param * 2
if len(faces) != 1:
raise FaceError("Expected 1 face, but got {}".format(len(faces)), len(faces))

# 计算人脸坐标
left = faces[0][0]
top = faces[0][1]
width = faces[0][2] - left + 1
height = faces[0][3] - top + 1
ctx.face["rectangle"] = (left, top, width, height)

# 根据landmarks计算人脸偏转角度,以眼睛为标准,计算的人脸偏转角度,用于人脸矫正
# 示例landmarks [106.37181 150.77415 127.21012 108.369156 144.61522 105.24723 107.45625 133.62355 151.24269 153.34407 ]
landmarks = landmarks[0]
left_eye = np.array([landmarks[0], landmarks[5]])
right_eye = np.array([landmarks[1], landmarks[6]])
dy = right_eye[1] - left_eye[1]
dx = right_eye[0] - left_eye[0]
roll_angle = np.degrees(np.arctan2(dy, dx))

ctx.face["roll_angle"] = roll_angle


def detect_face_face_plusplus(ctx: Context):
Expand Down