调用face++平台api进行人脸识别

xiaoxiao2021-02-28  106

转载请注明出处:http://blog.csdn.net/hongbin_xu 或 http://hongbin96.com/ 文章链接:http://blog.csdn.net/hongbin_xu/article/details/74981819 或 http://hongbin96.com/125

Face++介绍:

Face++平台提供一整套世界领先的人脸检测,人脸识别,面部分析的视觉技术服务。通过提供云端API、离线SDK等供用户进行开发,像支付宝人脸支付使用的技术就是Face++。(face++的介绍)

每个人在Face++的官网注册账号后可以申请新建API,填写相关信息后,随后会分配API key和 API Secrect。我们可以选择试用的服务,由于是免费的有的功能不支持。

分配的API key和 API Secrect,有了这两个东西才能调用api。

官网提供了API文档和演示。

打开api文档可以查看详细说明,很详细不多说了。

实验平台:

我的测试程序是在Ubuntu下自带的Python环境下编写,用到了Python-OpenCV,所以要装一下Python-OpenCV。

console下输入:

sudo apt-get install python-opencv

很快就会安装完成,并且会自动配置好环境变量。

import cv包和cv2包看看,发现没有报错,安装成功。

代码:

# -*- coding:utf-8 -*- import cv2 import urllib2 import urllib import time #读取原图,并显示 img = cv2.imread("football_players.jpeg") cv2.namedWindow("原图") cv2.imshow("原图", img) #URL http_url='https://api-cn.faceplusplus.com/facepp/v3/detect' #用户信息 key = "RU8VkInUd4zpcCo2GbKxPz90rPoaY5O0" secret = "01YadiHNX_Fpqw6saBYa2POD6ozL6gWu" #图片存储路径 filepath = r"/home/xhb/Study/FaceRecognition/python-opencv-face++/football_players.jpeg" #这后面的都是给的示例代码,调用API接口 boundary = '----------%s' % hex(int(time.time() * 1000)) data = [] data.append('--%s' % boundary) data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_key') data.append(key) data.append('--%s' % boundary) data.append('Content-Disposition: form-data; name="%s"\r\n' % 'api_secret') data.append(secret) data.append('--%s' % boundary) fr=open(filepath,'rb') data.append('Content-Disposition: form-data; name="%s"; filename=" "' % 'image_file') data.append('Content-Type: %s\r\n' % 'application/octet-stream') data.append(fr.read()) fr.close() data.append('--%s--\r\n' % boundary) http_body='\r\n'.join(data) #buld http request req=urllib2.Request(http_url) #header req.add_header('Content-Type', 'multipart/form-data; boundary=%s' % boundary) req.add_data(http_body) try: #req.add_header('Referer','http://remotserver.com/') #post data to server resp = urllib2.urlopen(req, timeout=5) #get response qrcont=resp.read() print qrcont #打印出得到的结果 except urllib2.HTTPError as e: print e.read() #进过测试前面的程序会返回一个字典,其中指出了人脸所在的矩形的位置和大小等,所以直接进行标注 mydict = eval(qrcont) faces = mydict["faces"] faceNum = len(faces) print("识别到了%d个人脸"%(faceNum)) for i in range(faceNum): face_rectangle = faces[i]['face_rectangle'] width = face_rectangle['width'] top = face_rectangle['top'] left = face_rectangle['left'] height = face_rectangle['height'] start = (left, top) end = (left+width, top+height) color = (55,255,155) thickness = 3 cv2.rectangle(img, start, end, color, thickness) cv2.namedWindow("识别后") cv2.imshow("识别后", img) cv2.waitKey(0) cv2.destroyAllWindows() # print type(resp)

程序不复杂,按照程序思路简单解释下: 1、指定图片的名称,读取图片,并显示。

#读取原图,并显示 img = cv2.imread("football_players.jpeg") cv2.namedWindow("原图") cv2.imshow("原图", img)

把图片直接放在当前目录下即可(图片是巴萨的<( ̄︶ ̄)>)。

2、填一些调用api相关的信息,根据需要自己改就行。

#URL http_url='https://api-cn.faceplusplus.com/facepp/v3/detect' #用户信息 key = "RU8VkInUd4zpcCo2GbKxPz90rPoaY5O0" secret = "01YadiHNX_Fpqw6saBYa2POD6ozL6gWu" #图片存储路径 filepath = r"/home/xhb/Study/FaceRecognition/python-opencv-face++/football_players.jpeg"

从api文档可以查到,detect的URL: https://api-cn.faceplusplus.com/facepp/v3/detect

用户信息要填上自己在前面申请的API Key和API Secret。

要传送图片到face++的服务器去进行识别,填上图片所在的目录的路径:

当前目录路径+图片名。

#图片存储路径 filepath = r"/home/xhb/Study/FaceRecognition/python-opencv-face++/football_players.jpeg"

3、中间的程序其实就是把信息封装一下,建立网络链接,然后跟服务器通信。调用urlopen()访问服务器,返回resp,打印结果。

try: #req.add_header('Referer','http://remotserver.com/') #post data to server resp = urllib2.urlopen(req, timeout=5) #get response qrcont=resp.read() print qrcont #打印出得到的结果 except urllib2.HTTPError as e: print e.read()

4、resp是返回的数据。调用read()方法,转换成qrcont,这是个字符串,然后在终端打印出来。 终端的全部打印信息: 很明显,返回的resp是一个字典,其中记录了一些图片的信息,还有识别出的人脸的位置。 qrcont是一组字符串,调用eval()函数将其转换回字典类型,取出来再处理一下,在图片上标识出人脸的位置。

#进过测试前面的程序会返回一个字典,其中指出了人脸所在的矩形的位置和大小等,所以直接进行标注 mydict = eval(qrcont) faces = mydict["faces"] faceNum = len(faces) print("识别到了%d个人脸"%(faceNum)) for i in range(faceNum): face_rectangle = faces[i]['face_rectangle'] width = face_rectangle['width'] top = face_rectangle['top'] left = face_rectangle['left'] height = face_rectangle['height'] start = (left, top) end = (left+width, top+height) color = (55,255,155) thickness = 3 cv2.rectangle(img, start, end, color, thickness)

运行结果:

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

最新回复(0)