sponsored links

多种人脸属性识别(python)

基于 Python +  百度AI 开发的多种人脸属性识别程序

可检测:年龄、 颜值、表情、脸型、性别、眼镜、情绪等各种人脸属性

程序代码如下:

# -*- coding:utf-8 -*-
from aip import AipFace
import base64


def shibe(file):
    """ 你的 APPID AK SK """
    APP_ID = ''
    API_KEY = ''
    SECRET_KEY = ''
    client = AipFace(APP_ID, API_KEY, SECRET_KEY)

    with open(file, "rb") as f:
        data = f.read()
        encodestr = base64.b64encode(data)  # 得到 byte 编码的数据
        images = str(encodestr, 'utf-8')  # 重新编码数据
    image = images
    imageType = "BASE64"

    """ 调用人脸检测 """
    # m = client.detect(image, imageType)

    """ 如果有可选参数 """
    options = {}
    options["face_field"] = "age,beauty,expression,face_shape,gender,glasses,emotion"

    """ 带参数调用人脸检测 """
    m = client.detect(image, imageType, options)
    if m["error_msg"] == "SUCCESS":
        mm = m["result"]
        print(mm)
        num = mm["face_num"]
        mmm = mm["face_list"][0]
        age = mmm["age"]
        beauty = mmm["beauty"]

        # 表情
        exp = {'none': "不笑", 'smile': "微笑", 'laugh': "大笑"}
        expression = mmm["expression"]['type']
        if expression in exp:
            expressions = exp[expression]
        else:
            expressions = "未知"

        # 脸型
        face = {'square': '正方形', 'triangle': '三角形', 'oval': '椭圆', 'heart': '心形', 'round': '圆形'}
        face_shape = mmm["face_shape"]['type']
        if face_shape in face:
            face_shapes = face[face_shape]
        else:
            face_shapes = "未知"

        # 性别
        gen = {'male': '男', 'female': '女'}
        gender = mmm["gender"]['type']
        if gender in gen:
            genders = gen[gender]
        else:
            genders = "未知"

        # 眼镜
        gla = {'none': '无眼镜', 'common': '普通眼镜', 'sun': '墨镜'}
        glasses = mmm["glasses"]['type']
        if glasses in gla:
            glassess = gla[glasses]
        else:
            glassess = "未知"

        # 情绪
        emo = {'angry': '愤怒', 'disgust': '厌恶', 'fear': '恐惧', 'happy':'高兴', 'sad': '伤心', 'surprise': '惊讶', 'neutral': '无情绪'}
        emotion = mmm["emotion"]['type']
        if emotion in emo:
            emotions = emo[emotion]
        else:
            emotions = "未知"

        print("人脸数:%d, 年龄:%d, 颜值:%d, 表情:%s, 脸型:%s, 性别:%s, 眼镜:%s, 情绪:%s"
              %(num, age, beauty, expressions, face_shapes, genders, glassess, emotions))

if __name__ == '__main__':
    shibie("666.jpg")  # 人脸图片地址

 

需要安装相关库:

pip install aip
pip install base64

 

Tags: