Python3.6--face++人脸识别开源平台使用说明

xiaoxiao2021-02-28  64

一.face++说明

    Face++旷视科技有限公司旗下的新型视觉服务平台,他提供了大量及功能丰富开源的人脸,手势等识别的API接口。我在做人脸检测相关项目时接触到这个开源平台,为我项目的完成提供了很大的支持。但face++官网上的python相关的例程编写复杂,还是python2.7的老版本,不适合新手入门,在此写下一些调用相关API的说明,以供参考。

    Face++官网:https://www.faceplusplus.com.cn/

二.face++账号及调用规则

    当你登陆上网站后,在应用管理里创建自己的API Key,

创建的过程比较简单,不在这里赘述,创建的类型有两种,正式和启用,正式用户需要充值,可以获得稳定的传输,试用用户可能会出现

CONCURRENCY_LIMIT_EXCEEDED

的错误,并发控制数超出限制,是指该 API Key 的 QPS 已经达到上限

API Key 和API Secret 是你调用API的身份标识。

三.传入自己的图片并分析,调用Detect API

# -*- coding: utf-8 -*- """ Created on Sat Jun 9 15:30:59 2018 @author: lenovo """ import requests key ="........." secret ="..........." filepath1 ="yun2.jpg" filepath2 = "mayun2.jpg" filepath3 = "mayun.jpg" def detect_face(filepath):#传入图片文件 http_url ="https://api-cn.faceplusplus.com/facepp/v3/detect" files = {"image_file": open(filepath, "rb")} data = {"api_key":key, "api_secret": secret} response = requests.post(http_url, data=data, files=files) req_dict = response.json() print(req_dict) return req_dict if __name__ == "__main__": detect_face(filepath3)

代码分析:

1  key和secret是你创建API Key是key和secret,filepath是图片的目录

2  http_url是调用url的地址,见官网detect API说明文档。

3.files和data的格式按照官网的’请求参数‘中要求的去设置,这里我使用的图片传输格式是image_file ,http请求中的multipart/form-data,它会将表单的数据处理为一条消息,以标签为单元,用分隔符分开。既可以上传键值对,也可以上传文件。

4.Requests是用python语言基于urllib编写的,采用的是Apache2 Licensed开源协议的HTTP库。Requests 允许你发送 HTTP请求,代码这里用request把需要的请求参数发送到http_url,

5.request.json返回的数据

每个返回值的含义见官网API说明

四.把face_token保存在face_set

接上部分代码:创建faceset存储face_token

def set_face():#创建face_set url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/create' params = { 'api_key':key, 'api_secret':secret, } response = requests.post(url,data = params) req_dict = response.json() print(req_dict) return req_dict if __name__ == "__main__": set_face()

faceset_token就是这个人脸库的唯一标识,当然你也可以加上outer_id等信息。

def addface(faceset,facetokens):#将face加入到faceset url = 'https://api-cn.faceplusplus.com/facepp/v3/faceset/addface' params = { 'api_key':key, 'api_secret':secret, 'faceset_token':faceset, 'face_tokens':facetokens } r = requests.post(url,data = params) req_dict = r.json() print(req_dict) return req_dict if __name__ == "__main__": image1 = detect_face(filepath3) faceId1 = image1['faces'][0]['face_token'] addface('f3af21b2595296c2c287c2640fb3af95',faceId1) #返回的是一个字典型,所以读取信息也要用字典的方式

face++有其他功能的API,都是按照类似的方式调用API,只要阅读官方的API说明,可以很好调用它的资源,完成有趣的任务。

五.识别图片并显示唯一标识,示例

1.在识别图片前,我们为它添加唯一标识user_id.查看说明文档:

在这里,我们为之前已经写进face_set中的face_token添加user_id,这里userid是一个字符串,我们在这里可以备注图片的姓名等其他信息 。比如我为我的图片添加id是‘mayun’

def face_SetUserID(face_token,user_id):#为检测出的某一个人脸添加标识信息,该信息会在Search接口结果中返回,用来确定用户身份。 url = 'https://api-cn.faceplusplus.com/facepp/v3/face/setuserid' params = { 'api_key':key, 'api_secret':secret, 'face_token':face_token, 'user_id':user_id } r = requests.post(url,data = params) req_dict = r.json() print(req_dict) return req_dict face_SetUserID('your face_token','Mr.mayun')

2.调用Search API 在faceset中匹配人脸信息,说明文档:

def face_search(image_file1,faceset_token): url = 'https://api-cn.faceplusplus.com/facepp/v3/search' files = {"image_file": open(image_file1, "rb")} params = { 'api_key':key, 'api_secret':secret, 'faceset_token':faceset_token } r = requests.post(url,files = files,data = params) req_dict = r.json() print(req_dict) return req_dict if __name__ == "__main__":     face_search(filepath3(图片地址),'your face_set_token')

返回的数据有置信度,人脸坐标位置等信息,利用这些信息画出人脸矩形框并标注id。

if __name__ == "__main__": img = cv2.imread(filepath2) face_information = face_search(filepath2,'e55232f11a305f9165caf50ef16ae053')#该帧与faceset中人脸进行匹配 if face_information['faces'] :#[faces]数组不能为空,能在图像中找到脸 confidence = face_information['results'][0]['confidence'] thresholds = face_information['thresholds']['1e-5'] if confidence > 75 and thresholds < confidence: #置信度阈值判断 user_id = face_information['results'][0]['user_id'] #获得唯一人脸id w = face_information['faces'][0]['face_rectangle']['width'] h = face_information['faces'][0]['face_rectangle']['top'] x = face_information['faces'][0]['face_rectangle']['left'] y = face_information['faces'][0]['face_rectangle']['height'] cv2.rectangle(img, (x, y), (x + w, y + h), (255, 0, 0), 2)#人脸矩形框 font = cv2.FONT_HERSHEY_SIMPLEX#字体设置 cv2.putText(img, user_id, (x,y-5), font,1, (0,0,255),1)#照片/添加的文字/左上角坐标/字体/字体大小/颜色/字体粗细 cv2.imwrite('D:\\PYTHON0\\my_face_handsome\\video_face\\1.jpg',img)

使用opencv对图片进行读取存储画图等处理,关于putText,暂时只能使用英文字符,不能写入汉子,以后有时间再解决这个问题。以下是结果:

六.其他

如果你不知道你的face_set的信息,可以使用辅助API中faceset getfeceset 获取你所有创建的信息。Ok,如果你学会了,就可以利用这套开源平台搞事情了。

github:https://github.com/hfut-xc-yun/hfut-my-face/blob/master/face++_1.py

转载请注明原文地址: https://www.6miu.com/read-2650026.html

最新回复(0)