百度AI之图像识别SDK:实现人脸检测+比对(python)

  之前的博客中体验了百度AI图像识别的SDK,相信很多人跟我一样对人脸识别也有着很深的兴趣,今天来试一下这套SDK里的face相关的模块。其中SDK的下载和部署这里不再赘述,详细的可以参考上一篇博客。

        这里我们同样需要在百度AI控制台里创建一个人脸识别的应用,下面是代码,只需要将需要比对的图片放在py文件所在目录下执行py文件即可。

# -*- coding: UTF-8 -*-  

from aip import AipFace
import cv2
import base64
import sys

sys.setrecursionlimit(1000000) 

#初始化aipFace

APP_ID = '11222528'
API_KEY = 'l1CAmQTvS5IAgWeurQv2WPyF'
SECRET_KEY = 'dmZ9zDvoVqImWkoSxGdYKHaF6Zwh4vaG'

aipFace = AipFace(APP_ID, API_KEY, SECRET_KEY)


#人脸检测部分
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

options = {
    'max_face_num': 1, # 人脸数量
    'face_fields': "expression,faceshape",
}		
		
result1 = aipFace.detect(get_file_content('test1.jpg'), options)
# 解析位置信息1
location1=result1['result']['face_list'][0]['location']
left_top1=(int(location1['left']),int(location1['top']))
right_bottom1=(int(left_top1[0]+location1['width']),int(left_top1[1]+location1['height']))

img1=cv2.imread('test1.jpg')
cv2.rectangle(img1,left_top1,right_bottom1,(0,255,0),2)
cv2.imshow('img1',img1)	

result2 = aipFace.detect(get_file_content('test2.jpg'), options)
# 解析位置信息2
location2=result2['result']['face_list'][0]['location']
left_top2=(int(location2['left']),int(location2['top']))
right_bottom2=(int(left_top2[0]+location2['width']),int(left_top2[1]+location2['height']))

img2=cv2.imread('test2.jpg')
cv2.rectangle(img2,left_top2,right_bottom2,(0,255,0),2)
cv2.imshow('img2',img2)		
		
		
		
#人俩比对部分		
result = aipFace.match([
    {
        'image' : base64.b64encode(open('test1.jpg', 'rb').read()).decode(),
        'image_type': 'BASE64',
    },
    {
       'image' : base64.b64encode(open('test2.jpg', 'rb').read()).decode(),
        'image_type': 'BASE64',
    }
])

#print(result)
print('\n')
aaa=result['result']['score']
print ("相似得分为:"),print (aaa)
if aaa>80:
	print ("图1和图2是同一个人.\n")
else:
	print ("图1和图2不是同一个人.\n")
cv2.waitKey(100000)

     我们在powershell运行.py文件,执行结果如下图所示:

       下面我们来简单分析一下代码,前面的初始化aipFace就不多说了,我们直接从人脸检测说起,首先是读取图片内容,然后传入aipFace.detect函数,参数options是api返回信息的参数,在options里可以添加参数,这样result1和result2的返回信息里会有对应参数的信息,(有兴趣的朋友也可以自己打印出result1和result2的值看看里面是什么内容。)result1,2里包含了检测到的人脸位置信息,这时候我们提取出这些信息,即人脸的位置,然后利用openCV里的画矩形函数框出人脸。

       下面就是人脸的比对部分代码,同样将图片转为base64后传入match函数,返回值中会包含相似得分,可以根据自己的实际情况来设置阈值,从而判断是否为同一个人。

 

识别结果大概是这样 {'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
评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值