face++ API接口调用

人脸识别

  首先我想描述一下,在学校的时候一直好奇人脸识别与人脸检测这个技术,之后做了很多实验,曾经使用过很多简单的算法来做人脸相似度对比,比如:夹角余弦算法、判断两个矩阵之间对应位置元素相同来做统计、直方图比对、欧氏距离、绝对值距离等等很多这种低级的实验我都做过,一次次的识别让我感到万分难过。之后我不在最求这种算法的研究了,改成了看别人如何实现的,你也实现出来就好。很多人说这是it从业者的征兆,这样我彻底相信了,因为作为一个it开发的从业工作者,不能什么东西都需要自己开发,公司的要求是尽快的把项目赶出来,才能到达利益最大化。下面我为大家带来我在学校一直实验的人脸识别实现吧。当然我是在调用别人的接口。

1、导入所需要的包

import requests
from json import JSONDecoder
import cv2
导包

2、定义请求返回数据画出人脸

def drawFace(face_rectangle,img):
    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)
定义函数

3、准备好post请求的地址、key、password。

compare_url = "https://api-cn.faceplusplus.com/facepp/v3/compare"
key = "5Ut_EUtu3dG8Q60UBQdj8_LICgc4KByR"
secret = "cWXtsKOMx62m8zHUx810MG-0oGoOnhSO"

faceId1 = "img_test/tong1.jpg"  # 图片地址
faceId2 = "img_test/tong2.jpg"  # 图片地址

data = {"api_key": key, "api_secret": secret}
files = {"image_file1": open(faceId1, "rb"), "image_file2": open(faceId2, "rb")}
response = requests.post(compare_url, data=data, files=files)
发送请求

4、对http返回数据进行转换提取

req_con = response.content.decode('utf-8')
req_dict = JSONDecoder().decode(req_con)

print(req_dict)

#置信度,越高说明越像
confindence = req_dict['confidence']
print(confindence)

#将人脸框出来
face_rectangle_1 = req_dict['faces1'][0]['face_rectangle']
# print(face_rectangle_1)
face_rectangle_2 = req_dict['faces2'][0]['face_rectangle']
View Code

5、画出检测对比的人脸

img1 = cv2.imread(faceId1)
img2 = cv2.imread(faceId2)

if confindence>=80:
    drawFace(face_rectangle_1,img1)
    drawFace(face_rectangle_2,img2)

#图片过大,调整下大小
img1 = cv2.resize(img1,(500,500))
img2 = cv2.resize(img2,(500,500))

cv2.imshow("img1",img1)
cv2.imshow("img2",img2)

cv2.waitKey(0)
cv2.destroyAllWindows()
View Code

6、下面是完整代码:

# coding:utf-8
import
requests from json import JSONDecoder import cv2 def drawFace(face_rectangle,img): 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) compare_url = "https://api-cn.faceplusplus.com/facepp/v3/compare" key = "5Ut_EUtu3dG8Q60UBQdj8_LICgc4KByR" secret = "cWXtsKOMx62m8zHUx810MG-0oGoOnhSO" faceId1 = "img_test/tong1.jpg" # 图片地址 faceId2 = "img_test/tong2.jpg" # 图片地址 data = {"api_key": key, "api_secret": secret} files = {"image_file1": open(faceId1, "rb"), "image_file2": open(faceId2, "rb")} response = requests.post(compare_url, data=data, files=files) req_con = response.content.decode('utf-8') req_dict = JSONDecoder().decode(req_con) print(req_dict) #置信度,越高说明越像 confindence = req_dict['confidence'] print(confindence) #将人脸框出来 face_rectangle_1 = req_dict['faces1'][0]['face_rectangle'] # print(face_rectangle_1) face_rectangle_2 = req_dict['faces2'][0]['face_rectangle'] img1 = cv2.imread(faceId1) img2 = cv2.imread(faceId2) if confindence>=80: drawFace(face_rectangle_1,img1) drawFace(face_rectangle_2,img2) #图片过大,调整下大小 img1 = cv2.resize(img1,(500,500)) img2 = cv2.resize(img2,(500,500)) cv2.imshow("img1",img1) cv2.imshow("img2",img2) cv2.waitKey(0) cv2.destroyAllWindows()

 

  

posted @ 2018-08-13 23:08  洺剑残虹  阅读(4021)  评论(0编辑  收藏  举报