Use python to achieve simple face recognition, you can estimate the value of the face

Face recognition

Realization ideas

1. Provide identification pictures

I put the required picture into D:/face recognition and named the picture 0.jpg .

The recognition pictures are as follows:

Insert picture description here

2. Create a face recognition application on Baidu AI

1. Baidu search, "Baidu AI", enter the official website, click on the console in the upper right corner, scan the code to log in.
2. After logging in, find the face recognition
Insert picture description here
3. Create an application, create successfully, click Manage application, you will get three values
Insert picture description here

Three, identification code

from aip import AipFace
from PIL import Image, ImageDraw, ImageFont
import base64
# 在百度AI里面新建的应用的 APP_ID API_KEY SECRET_KEY
APP_ID = '你的APP_ID值'
API_KEY = '你的API_KEY值'
SECRET_KEY = '你的SECRET_KEY值'
client = AipFace(APP_ID, API_KEY, SECRET_KEY)
# 调用人脸检测接口并进行人脸检测
image = str(base64.b64encode(open('D:/人脸识别/0.jpg', 'rb').read()), 'utf-8')
imageType = "BASE64"
options = dict()
options["face_field"] = "age,beauty,gender,expression,face_shape"  # 包括很多内容,例如这里的年龄,颜值,性别,表情
options["max_face_num"] = 1  # 最多可识别人脸数量,默认值为1,最大值为10
options["face_type"] = "LIVE"  # 人脸的类型,默认为“LIVE”,即生活照
options["liveness_control"] = "NONE"
a = client.detect(image, imageType, options)
# 输出检测结果
print('总共检测了', a['result']['face_num'], '个人脸')   # 这部分可以看一下官方文档的返回实例,就懂了
print('人物的年龄:', a['result']['face_list'][0]['age'])
print('人物的颜值:', a['result']['face_list'][0]['beauty'])
print('人物的表情:', a['result']['face_list'][0]['expression']['type'])
print('人物的性别:', a['result']['face_list'][0]['gender']['type'])
print('人物的脸型:', a['result']['face_list'][0]['face_shape']['type'])
# 打开检测的图像
im = Image.open("D:/人脸识别/0.jpg")  # image.open需要精确到图片的位置,而不是图片文件夹的位置
# 在图像上绘制信息,矩形,文本信息
my_draw = ImageDraw.Draw(im)
x1 = a['result']['face_list'][0]['location']['left']
y1 = a['result']['face_list'][0]['location']['top']
x2 = a['result']['face_list'][0]['location']['left']+a['result']['face_list'][0]['location']['width']
y2 = a['result']['face_list'][0]['location']['top']+a['result']['face_list'][0]['location']['height']
my_draw.rectangle([x1, y1, x2, y2], outline='blue', width=3)
# 在图像上输出文本
my_text = ImageFont.truetype('C:/Windows/Fonts/simfang.ttf', 25)  # 字体所在的文件路径(这里是仿宋),字体大小
text1 = '年龄:'+str(a['result']['face_list'][0]['age'])
my_draw.text((x2+180, y1), text1, fill='black', font=my_text)
text2 = '性别:'+str(a['result']['face_list'][0]['gender']['type'])
my_draw.text((x2+180, y1+120), text2, fill='black', font=my_text)
text3 = '脸型:'+a['result']['face_list'][0]['face_shape']['type']
my_draw.text((x2+180, y1+80), text3, fill='black', font=my_text)
text4 = '表情:'+str(a['result']['face_list'][0]['expression']['type'])
my_draw.text((x2+180, y1+160), text4, fill='black', font=my_text)
text5 = '颜值:'+str(a['result']['face_list'][0]['beauty'])
my_draw.text((x2+180, y1+40), text5, fill='black', font=my_text)
# 显示并保存结果
im.show()
im.save("D:/人脸识别/result.jpg")

For more details, please refer to the official document , official document portal

Four, running results

Insert picture description here

Five, supplement

1. The recognition in this example can only recognize one face at a time, that is, when there are multiple faces in a picture, only one face can be recognized.
2. The face value and expression data will change with the sharpness of the photo. For example, for the same person, the shooting angle and distance will affect these data.

Guess you like

Origin blog.csdn.net/qq_44921056/article/details/113977796