Python3 人脸识别属性(百度AI)

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

第一步引用

from aip import AipFace
import base64
import tkinter.filedialog
from tkinter import messagebox

第二步调用百度AI的方法

def imgdata(file):
    """ 你的 APPID AK SK """
    APP_ID = 'XXXXX'
    API_KEY = 'XXXXXX'
    SECRET_KEY = 'XXXXXX'
    client = AipFace(APP_ID, API_KEY, SECRET_KEY)
 
    with open(file, "rb") as f:
        data = f.read()
        encodestr = base64.b64encode(data)
        images = str(encodestr, 'utf-8') 
    image = images
    imageType = "BASE64"
 
  
    """ 参数 """
    options = {}
    options["face_field"] = "age,beauty,expression,face_shape,gender,glasses,emotion,race"
 
    """ 带参数调用人脸检测 """
    error = client.detect(image, imageType, options)
    if error["error_msg"] == "SUCCESS":
        error2 = error["result"]
        num = error2["face_num"]
        error3 = error2["face_list"][0]
        age = error3["age"]
        beauty = error3["beauty"]
        # 表情
        exp = {'none': "不笑", 'smile': "微笑", 'laugh': "大笑"}
        expression = error3["expression"]['type']
        if expression in exp:
            expressions = exp[expression]
        else:
            expressions = "未知"
        # 脸型
        face = {'square': '正方形', 'triangle': '三角形', 'oval': '椭圆', 'heart': '心形', 'round': '圆形'}
        face_shape = error3["face_shape"]['type']
        if face_shape in face:
            face_shapes = face[face_shape]
        else:
            face_shapes = "未知"
        # 性别
        gen = {'male': '男', 'female': '女'}
        gender = error3["gender"]['type']
        if gender in gen:
            genders = gen[gender]
        else:
            genders = "未知" 
        # 眼镜
        gla = {'none': '无眼镜', 'common': '普通眼镜', 'sun': '墨镜'}
        glasses = error3["glasses"]['type']
        if glasses in gla:
            glassess = gla[glasses]
        else:
            glassess = "未知"
        # 情绪
        emo = {'angry': '愤怒', 'disgust': '厌恶', 'fear': '恐惧', 'happy':'高兴', 'sad': '伤心', 'surprise': '惊讶', 'neutral': '无情绪'}
        emotion = error3["emotion"]['type']
        if emotion in emo:
            emotions = emo[emotion]
        else:
            emotions = "未知"
        #人种
        ra={'yellow':'黄种人','white':'白种人','black':'黑种人'}
        race=error3["race"]['type']
        if race in ra:
            race=ra[race]
        else:
            race="未知"
        print("年龄:%d, 颜值:%d, 表情:%s, 脸型:%s, 性别:%s, 是否戴眼镜:%s, 情绪:%s, 人种:%s"
              %(age, beauty, expressions, face_shapes, genders, glassess, emotions,race))

第三步选取图片

root = tkinter.Tk()  # 创建
root.withdraw()  # 隐藏
file_path = tkinter.filedialog.askopenfilename(title=u'选择文件')
if(file_path.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff'))):
   
else:
    messagebox.showinfo("提示","请选择正确的图片格式!")

完整代码


from aip import AipFace
import base64
import tkinter.filedialog
from tkinter import messagebox
def imgdata(file):
    """ 你的 APPID AK SK """
    APP_ID = 'XXXX'
    API_KEY = 'XXXX'
    SECRET_KEY = 'XXXX'
    client = AipFace(APP_ID, API_KEY, SECRET_KEY)
 
    with open(file, "rb") as f:
        data = f.read()
        encodestr = base64.b64encode(data)
        images = str(encodestr, 'utf-8') 
    image = images
    imageType = "BASE64"
 
  
    """ 参数 """
    options = {}
    options["face_field"] = "age,beauty,expression,face_shape,gender,glasses,emotion,race"
 
    """ 带参数调用人脸检测 """
    error = client.detect(image, imageType, options)
    if error["error_msg"] == "SUCCESS":
        error2 = error["result"]
        num = error2["face_num"]
        error3 = error2["face_list"][0]
        age = error3["age"]
        beauty = error3["beauty"]
        # 表情
        exp = {'none': "不笑", 'smile': "微笑", 'laugh': "大笑"}
        expression = error3["expression"]['type']
        if expression in exp:
            expressions = exp[expression]
        else:
            expressions = "未知"
        # 脸型
        face = {'square': '正方形', 'triangle': '三角形', 'oval': '椭圆', 'heart': '心形', 'round': '圆形'}
        face_shape = error3["face_shape"]['type']
        if face_shape in face:
            face_shapes = face[face_shape]
        else:
            face_shapes = "未知"
        # 性别
        gen = {'male': '男', 'female': '女'}
        gender = error3["gender"]['type']
        if gender in gen:
            genders = gen[gender]
        else:
            genders = "未知" 
        # 眼镜
        gla = {'none': '无眼镜', 'common': '普通眼镜', 'sun': '墨镜'}
        glasses = error3["glasses"]['type']
        if glasses in gla:
            glassess = gla[glasses]
        else:
            glassess = "未知"
        # 情绪
        emo = {'angry': '愤怒', 'disgust': '厌恶', 'fear': '恐惧', 'happy':'高兴', 'sad': '伤心', 'surprise': '惊讶', 'neutral': '无情绪'}
        emotion = error3["emotion"]['type']
        if emotion in emo:
            emotions = emo[emotion]
        else:
            emotions = "未知"
        #人种
        ra={'yellow':'黄种人','white':'白种人','black':'黑种人'}
        race=error3["race"]['type']
        if race in ra:
            race=ra[race]
        else:
            race="未知"
        print("年龄:%d, 颜值:%d, 表情:%s, 脸型:%s, 性别:%s, 是否戴眼镜:%s, 情绪:%s, 人种:%s"
              %(age, beauty, expressions, face_shapes, genders, glassess, emotions,race))

root = tkinter.Tk()  # 创建
root.withdraw()  # 隐藏
file_path = tkinter.filedialog.askopenfilename(title=u'选择文件')
if(file_path.lower().endswith(('.bmp', '.dib', '.png', '.jpg', '.jpeg', '.pbm', '.pgm', '.ppm', '.tif', '.tiff'))):
   imgdata(file_path)
else:
    messagebox.showinfo("提示","请选择正确的图片格式!")



  • 3
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
识别结果大概是这样 {'result': {'face_num': 1, 'face_list': [{'quality': {'occlusion': {'right_eye': 0, 'left_cheek': 0.1459853947, 'right_cheek': 0.05144193396, 'left_eye': 0.465408802, 'mouth': 0.02919708006, 'chin_contour': 0.01420217194, 'nose': 0}, 'illumination': 116, 'blur': 7.266304692e-06, 'completeness': 1}, 'age': 22, 'face_token': 'dc6f8f9df5d977ea476e2d04acdf5063', 'race': {'type': 'white', 'probability': 0.6173604727}, 'glasses': {'type': 'common', 'probability': 0.9834988713}, 'gender': {'type': 'male', 'probability': 0.655915916}, 'face_probability': 0.9185044169, 'beauty': 51.21487427, 'angle': {'roll': -2.750922441, 'yaw': 28.97134399, 'pitch': 5.202290535}, 'location': {'height': 65, 'top': 112.0704803, 'width': 76, 'left': 76.20765686, 'rotation': -4}, 'face_type': {'type': 'human', 'probability': 0.9992217422}, 'face_shape': {'type': 'oval', 'probability': 0.4419156313}, 'expression': {'type': 'none', 'probability': 0.9999142885}}]}, 'error_msg': 'SUCCESS', 'timestamp': 1537413754, 'cached': 0, 'error_code': 0, 'log_id': 9465840013520} 年龄:22 颜值:51.21487427 表情-type(none:不笑;smile:微笑;laugh:大笑):none 表情-probability(表情置信度,范围【0~1】,0最小、1最大):0.9999142885 脸型-type(square: 正方形 triangle:三角形 oval: 椭圆 heart: 心形 round: 圆形):oval 脸型-probability(置信度,范围【0~1】,代表这是人脸形状判断正确的概率,0最小、1最大):0.4419156313 性别-type(male:男性 female:女性):male 性别-probability(性别置信度,范围【0~1】,0代表概率最小、1代表最大。):0.655915916 是否带眼镜-type(none:无眼镜,common:普通眼镜,sun:墨镜):common 是否带眼镜-probability(眼镜置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9834988713 人种-type(yellow: 黄种人 white: 白种人 black:黑种人 arabs: 阿拉伯人):white 人种-probability(人种置信度,范围【0~1】,0代表概率最小、1代表最大。):0.6173604727 真实人脸/卡通人脸 -type(human: 真实人脸 cartoon: 卡通人脸):human 真实人脸/卡通人脸 -probability(人脸类型判断正确的置信度,范围【0~1】,0代表概率最小、1代表最大。):0.9992217422

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值