Python利用百度AI库颜值分析(包括脸型情绪颜值打分)

导入库

from aip import AipFace
import base64
import time
from PIL import Image,ImageDraw,ImageFont
from openpyxl import load_workbook

用到百度ai的人脸识别aip

""" 你的 APPID AK SK """
APP_ID = '你的appID'
API_KEY = '你的AK'
SECRET_KEY = '你的SK'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)

导入图片

time.sleep(2)
filename='pic.jpg'#你的图片的名字
fo=open(filename,'rb')
image=fo.read()
fo.close()

一些百度ai的参数设置和结果返回值

image = str(base64.b64encode(image), 'utf-8')
image_type = 'BASE64'
options = {}
options['face_field']= "age,gender,beauty,face_shape,emotion,glasses"
options['max_face_num']=10
options["face_type"] = "LIVE"#详情参考百度ai人脸识别的apk文件
result = client.detect(image, image_type,options)
#print(result)"""经过百度ai返回的结果的数组"""

对字体和图片的处理(之后要导入图片,在图片上框选出人脸,还要在图片上显示文字)

img = Image.open(filename)
draw=ImageDraw.Draw(img)
ttfont = ImageFont.truetype("C:/WINDOWS/Fonts/simsun.ttc",23)#字体的路径

对前文返回的百度ai的结果数组“result”进行处理

#性别
for face in result['result']['face_list']:
    if face['gender']['type'] == 'male':
        gender = "男"
    else:
        gender = "女"

    age = face['age']
    location = face['location']
    beauty = face['beauty']
#脸型
    face_shape = face['face_shape']
    if face['face_shape']['type'] == 'square':
        face_shape = '正方形'
    elif face['face_shape']['type'] == 'triangle':
        face_shape = '三角形'
    elif face['face_shape']['type'] == 'oval':
        face_shape = '椭圆'
    elif face['face_shape']['type'] == 'heart':
        face_shape = '心形'
    else:
        face_shape = '圆形'

#是否戴眼镜
    if face['glasses']['type'] == 'none':
        glasses = "没戴眼镜"
    else:
        glasses = "戴眼镜"
#情绪
    emotion = face['emotion']
    if face['emotion']['type'] == 'angry':
        emotion = '愤怒'
    elif face['emotion']['type'] == 'disgust':
        emotion = '厌恶'
    elif face['emotion']['type'] == 'fear':
        emotion = '恐惧'
    elif face['emotion']['type'] == 'sad':
        emotion = '伤心'
    elif face['emotion']['type'] == 'surprise':
        emotion = '惊讶'
    elif face['emotion']['type'] == 'happy':
        emotion = '高兴'
    else:
        emotion = '无情绪'
#人脸位置
    x1 = face['location']['left']
    y1 = face['location']['top']
    x2 = x1 + face['location']['width']
    y2 = y1 + face['location']['height']
    draw.rectangle((x1, y1, x2, y2), outline="yellow")#通过人脸位置的坐标画框
    x = x2 + 5                                          #x2是边框的右坐标
    draw.text([x+5, y1+0], "性别:" + gender, "black", font=ttfont)
    draw.text([x+5, y1+20], "年龄:" + str(age), "black", font=ttfont)
    draw.text([x+5, y1+40], "颜值:" + str(beauty), "black", font=ttfont)
    draw.text([x+5, y1+60], "表情:" + str(emotion), "black", font=ttfont)
    draw.text([x+5, y1+80], "脸型:" + str(face_shape), "black", font=ttfont)
    draw.text([x + 5, y1 + 100], "眼镜:" + str(glasses), "black", font=ttfont)

    print("性别:" + gender)
    print("年龄:" + str(age))
    print("颜值:" + str(beauty) )
    print("情绪:" + str(emotion))
    print("脸型:" + str(face_shape))
    print(glasses+"\n")

img.show()

保存结果到“jieguo.xlsx”没有新建的需要新建一个xlsl的excel文件

wb = load_workbook('jieguo.xlsx')
sheet = wb.active
sheet.append(['性别', '年龄', '颜值', '情绪', '是否戴眼镜', '脸型'])
sheet.append([gender, str(age), str(beauty), str(emotion), glasses, face_shape])
wb.save('jieguo.xlsx')

结果示例如图:

### 解决 PP-OCRv4 出现的错误 当遇到 `WARNING: The pretrained params backbone.blocks2.0.dw_conv.lab.scale not in model` 这样的警告时,这通常意味着预训练模型中的某些参数未能匹配到当前配置下的模型结构中[^2]。 对于此问题的一个有效解决方案是采用特定配置文件来适配预训练权重。具体操作方法如下: 通过指定配置文件 `ch_PP-OCRv4_det_student.yml` 并利用已有的最佳精度预训练模型 (`best_accuracy`) 来启动训练过程可以绕过上述不兼容的问题。执行命令如下所示: ```bash python3 tools/train.py -c configs/det/ch_PP-OCRv4/ch_PP-OCRv4_det_student.yml ``` 该方案不仅解决了参数缺失带来的警告,还能够继续基于高质量的预训练成果进行微调,从而提升最终检测效果。 关于蒸馏的概念,在机器学习领域内指的是将大型复杂网络(teacher 模型)的知识迁移到小型简单网络(student 模型)。这里 student 和 teacher 的关系是指两个不同规模或架构的神经网络之间的指导与被指导的关系;其中 teacher 已经经过充分训练并具有良好的性能,而 student 则试图模仿前者的行为模式以达到相似的效果但保持更高效的计算特性。 至于提到的 `Traceback` 错误信息部分,由于未提供具体的跟踪堆栈详情,难以给出针对性建议。不过一般而言,这报错往往涉及代码逻辑错误或是环境配置不当等问题。为了更好地帮助定位和解决问题,推荐记录完整的异常日志,并仔细检查最近修改过的代码片段以及确认依赖库版本的一致性。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值