from aip import AipFace
import base64


# 定义人脸位置检测函数
def face_detect(client, img_det_path):
    # 功能：对一张照片进行检测，返回各张人脸位置
    # 参数client:百度AI人脸识别客户端，img_det_path：待检测图片路径
    # 返回值：人脸区域的位置列表，人脸区域的位置包括左端、顶端、右端、底端
    img_det64 = str(base64.b64encode(open(img_det_path, 'rb').read()), 'utf-8')
    # 调用人脸检测接口
    result = client.detect(img_det64, 'BASE64', {"max_face_num": 10})
    result = result['result']
    face_list = []
    if result is not None:
        for face in result['face_list']:
            left = face['location']['left']
            top = face['location']['top']
            right = left + face['location']['width']
            bottom = top + face['location']['height']
            face_list.append([left, top, right, bottom])
    return face_list


# 填写你的文字识别应用的App ID、API Key、Secret Key
APP_ID = ''
API_Key = ''
Secret_Key = ''
client = AipFace(APP_ID, API_Key, Secret_Key)  # 创建百度AI人脸识别客户端
img_det_path = './imgs/group-1.jpg'  # 设置人脸位置检测的图片路径
print(face_detect(client, img_det_path))  # 调用人脸位置检测函数，输出返回值
