ai人脸识别

这段代码展示了如何使用AipFace SDK进行人脸识别。通过读取图像文件,进行Base64编码,然后调用API获取年龄、美丽度、性别和眼镜类型等信息,并在图像上标注这些属性。最后,显示带有标注信息的图像。
摘要由CSDN通过智能技术生成
from aip import AipFace
import base64
import time
from PIL import  Image,ImageDraw,ImageFont
APP_ID = '24119354'
API_KEY = '05FFjyTy3Oj6A5RdXgi8ua6q'
SECRET_KEY = '4zudUSQkl8hFkuDEBovwR1nBg6SIjn8Y'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
image_f = '7.webp'
f_img = open('7.webp','rb')
img = f_img.read()
f_img.close()
imge =Image.open(image_f)
draw = ImageDraw.Draw(imge)
tfont =ImageFont.truetype('‪C:\Windows\Fonts\STSONG.TTF',16)
# imge.show()
# print(img)
image = str(base64.b64encode(img),"utf-8")
imageType = "BASE64"
options = {}
options["face_field"] = "age,beauty,gender,glasses"
options["max_face_num"] = 10
options["face_type"] = "LIVE"
result = client.detect(image, imageType, options)
time.sleep(0.5)
# print(result)
for each in result['result']['face_list']:
    print(each)
    print("$"*100,'\n')
    age = each['age']
    beauty = each['beauty']
    glasses = each['glasses']
    if each['gender']['type'] == 'female':
        gender = '女'
    else:
        gender = '男'
    if each['glasses']['type'] =='common':
        glasses = '普通眼镜'
    if each['glasses']['type'] =='sun':
        glasses = '墨镜'
    else:
        glasses = '无眼镜'

    x1 =each['location']['left']
    y1 =each['location']['top']
    x2 =x1 + each['location']['width']
    y2 =y1 + each['location']['height']
    draw.rectangle((x1,y1,x2,y2),outline='purple')

    draw.text((x2+5,y1),'年龄:'+ str(age),'black',font=tfont)
    draw.text((x2 + 5, y1+15), '颜值:' + str(beauty), 'black', font=tfont)
    draw.text((x2 + 5, y1+30), '性别:' + str(gender), 'black', font=tfont)
    draw.text((x2 + 5, y1+45), '眼镜:' + str(glasses), 'black', font=tfont)
    print('年龄:',age)
    print('颜值:',beauty)
    print('性别:',gender)
    print('眼镜:',glasses)
imge.show()
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值