import cv2 
import numpy as np
import os

def detect_frontal_face(img_path,save_path):
    img = cv2.imread(img_path)
    """检测人脸并判断是否为正脸"""
    # 灰度化图片
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    
    # 加载haarface级联分类器
    face_cascade = cv2.CascadeClassifier(r'D:\anaconda3\envs\py38\Library\etc\haarcascades\haarcascade_frontalface_default.xml')
    
    # 检测人脸
    faces = face_cascade.detectMultiScale(gray, 1.3, 5)
    if len(faces)>=1:
        # 在原图img上画矩形
        for (x, y, w, h) in faces: 
            # 获取人脸矩形区域
            face_img = img[y:y+h, x:x+w] 
            cv2.imshow('Face Detection', face_img)
            cv2.waitKey(0)
            # # 判断人脸矩形宽高比是否为正脸
            if w / h < 1.2 and w / h > 0.8:  
                cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)  
                label = 'Frontal Face'
            else:  
                cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)  
                label = 'Not Frontal'
                
            # 在图片上打标签    
            cv2.putText(img, label, (x, y - 5),cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2) 
                
        # 展示图片
        # cv2.imshow('Face Detection', img)
        # cv2.waitKey(0)
        cv2.imwrite(save_path,img)
    else:
        # cv2.imwrite(save_path, img)
        print("No facial information was detected")

dir_path = r'.\img'
save_path = r'.\img_result'
imgs = os.listdir(dir_path)
# print(imgs)
for img in imgs:
    img_path = os.path.join(dir_path, img)  
    # 判断是否为图片文件
    if img_path.endswith('.jpg') or img_path.endswith('.png'):
        detect_frontal_face(img_path,os.path.join(save_path,"result_"+img))




# def is_frontal_face(face_path):
#     flag = 0
#     face_img = cv2.imread(face_path)
#     # cv2.imshow('image',face_img)
#     # cv2.waitKey(0)
#     # cv2.destroyAllWindows()
    
#     """判断是否是正脸"""

#     # D:\anaconda3\envs\py38\Library\etc\haarcascades
#     face_cascade = cv2.CascadeClassifier(r'D:\anaconda3\envs\py38\Library\etc\haarcascades\haarcascade_frontalface_default.xml')
#     # face_cascade.load(r'D:\anaconda3\envs\py38\Library\etc\haarcascades\haarcascade_frontalface_default.xml')


#     face_img_gray = cv2.cvtColor(face_img, cv2.COLOR_BGR2GRAY)
#     faces = face_cascade.detectMultiScale(face_img_gray, 1.3, 5)
#     if len(faces) != 1:
#         return False 
    
#     face_x, face_y, face_w, face_h = faces[0]
#     center_x = face_x + face_w/2
#     center_y = face_y + face_h/2

#     eye_cascade = cv2.CascadeClassifier(r'D:\anaconda3\envs\py38\Library\etc\haarcascades\haarcascade_eye.xml')
#     eyes = eye_cascade.detectMultiScale(face_img_gray, 1.3, 5, 
#                                        minSize=(30, 30))

#     if len(eyes) != 2:
#         return False
    
#     left_eye_x, left_eye_y, left_eye_w, left_eye_h = eyes[0]
#     right_eye_x, right_eye_y, right_eye_w, right_eye_h = eyes[1]
    
#     left_eye_center_x = left_eye_x + left_eye_w/2 
#     left_eye_center_y = left_eye_y + left_eye_h/2
#     right_eye_center_x = right_eye_x + right_eye_w/2
#     right_eye_center_y = right_eye_y + right_eye_h/2
    
#     x_diff = abs(left_eye_center_x - right_eye_center_x)
#     y_diff = abs(left_eye_center_y - right_eye_center_y)  

#     # 两眼距离小于面部宽度的60%视为正脸
#     if x_diff <= 0.6 * face_w and y_diff <= 0.6 * face_h:
#         flag = 1
#         return True
#     return False


# print(is_frontal_face(img_paht))

# opencv_detect_frontal_faces(img_paht)













# def opencv_detect_frontal_faces(face_path):
#     img = cv2.imread(face_path)
#     # cv2.imshow('image',face_img)
#     # cv2.waitKey(0)
#     # cv2.destroyAllWindows()
    
#     """判断是否是正脸"""

#     # D:\anaconda3\envs\py38\Library\etc\haarcascades
#     face_cascade = cv2.CascadeClassifier(r'D:\anaconda3\envs\py38\Library\etc\haarcascades\haarcascade_frontalface_default.xml')
#     # face_cascade.load(r'D:\anaconda3\envs\py38\Library\etc\haarcascades\haarcascade_frontalface_default.xml')

#     img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
#     faces = face_cascade.detectMultiScale(img_gray, 1.3, 5)
#     if len(faces) >= 1:
#         face_x, face_y, face_w, face_h = faces[0]
#         center_x = face_x + face_w/2
#         center_y = face_y + face_h/2

#         eye_cascade = cv2.CascadeClassifier(r'D:\anaconda3\envs\py38\Library\etc\haarcascades\haarcascade_eye.xml')
#         eyes = eye_cascade.detectMultiScale(img_gray, 1.3, 5, 
#                                         minSize=(30, 30))
#         print(eyes)
#         left_eye_x, left_eye_y, left_eye_w, left_eye_h = eyes[0]
#         right_eye_x, right_eye_y, right_eye_w, right_eye_h = eyes[1]

#         left_eye_center_x = left_eye_x + left_eye_w/2 
#         left_eye_center_y = left_eye_y + left_eye_h/2
#         right_eye_center_x = right_eye_x + right_eye_w/2
#         right_eye_center_y = right_eye_y + right_eye_h/2

#         x_diff = abs(left_eye_center_x - right_eye_center_x)
#         y_diff = abs(left_eye_center_y - right_eye_center_y)  

#         for (x, y, w, h) in faces: 
#             # 获取人脸矩形区域
#             face_img = img[y:y+h, x:x+w] 
#             if len(eyes) ==2:
#             # 两眼距离小于面部宽度的60%视为正脸
#                 if x_diff <= 0.6 * face_w and y_diff <= 0.6 * face_h:
#                     cv2.rectangle(img, (x, y), (x+w, y+h), (0, 255, 0), 2)  
#                     label = 'Frontal Face'
#                 else:  
#                     cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)  
#                     label = 'Not Frontal'
#             else:
#                 cv2.rectangle(img, (x, y), (x+w, y+h), (0, 0, 255), 2)  
#                 label = 'Not Frontal'
#             # 在图片上打标签    
#             cv2.putText(img, label, (x, y - 5),cv2.FONT_HERSHEY_SIMPLEX, 0.6, (0, 255, 255), 2)
#         cv2.imshow('Face Detection', img)
#         cv2.waitKey(0)
#     else:
#         print("No facial information was detected")