Codes

Face Alignment

Python Codes:

from mtcnn import MTCNN
import cv2
import numpy as np
import imutils
import os

img_path = "Beautiful faces\\Asia\\Male\\1.jpg"
img = cv2.imread(img_path)
detector = MTCNN()
face_list = detector.detect_faces(img) 

for face in face_list:    
    box = face["box"]    
    keypoints = face["keypoints"]       
    left = keypoints["left_eye"]
    right = keypoints["right_eye"]
    p1 = np.array(left)
    p2 = np.array(right)
    dp = p1-p2
    angle = np.arctan(dp[1] / dp[0])*180/np.pi
    rot_img = imutils.rotate(img, angle)  # rotate
    x,y,w,h = box
    if y>=0: # prevent x/y <0:
        cropped = rot_img[y:y+h,x:x+w]
    if y<0:
        cropped = rot_img[0:h,x:x+w]

cv2.imwrite("C:\\Users\\lenovo\\Desktop\\1.jpg", cropped)
Kathleen Li

发表回复

您的电子邮箱地址不会被公开。 必填项已用*标注