face++实现人脸识别及人脸相似度对比

转自https://blog.csdn.net/qq_38181012/article/details/81124217

  1. 使用face++,先获取key和secret
  2. 下方是人脸识别,还添加了画出人脸轮廓的正方形
import requests#网络访问控件
from json import JSONDecoder#互联网数据交换标准格式
import cv2 as cv#图像处理控件

http_url ="https://api-cn.faceplusplus.com/facepp/v3/detect"#face++apiDETECT模块
key ="自己官网弄"#开发人员识别码
secret ="同上"
gender="gender,age"#性别变量
filepath1 ="c:\\python\\image\\f2.jpg"#图像位置
data = {"api_key":key, "api_secret": secret, "return_attributes":gender}
#数据格式化准备发送到face,词典格式json

files = {"image_file": open(filepath1, "rb")}#准备打开
response = requests.post(http_url, data=data, files=files)#用post方式(还有get)发送数据到网站
req_con = response.content.decode('utf-8')#网页解码
req_dict = JSONDecoder().decode(req_con)#把json解码成python词典格式
print(req_dict)
w=req_dict["faces"][0]["face_rectangle"]["width"]
t=req_dict["faces"][0]["face_rectangle"]["top"]
l=req_dict["faces"][0]["face_rectangle"]["left"]
h=req_dict["faces"][0]["face_rectangle"]["height"]
src=cv.imread(filepath1)#打开jpg文件       
cv.namedWindow('input_image', cv.WINDOW_NORMAL)#弹出窗口命名,窗口自动大小
cv.rectangle(src,(l,t),(l+w,t+h), (255,0,0),1)#画框
# height, width, channel=(360, 480, 3)
h,w,c = src.shape
cv.resizeWindow('input_image',int(w/3),int(h/3))
cv.imshow('input_image', src)#显示图形
#img: 图像,起始坐标,终点坐标,颜色,线宽。 
cv.waitKey(0)#等键盘动作
cv.destroyAllWindows() #  关闭所有窗口
print(req_dict["faces"][0]["attributes"]["gender"]["value"])
  1. 人脸对比代码如下:
import requests#网络访问控件
from json import JSONDecoder#互联网数据交换标准格式
import cv2 as cv#图像处理控件
 
http_url ="https://api-cn.faceplusplus.com/facepp/v3/compare"#face++apiDETECT模块
key =""#开发人员识别码
secret =""
image_file1="c:\\python\\image\\f1.jpg"
image_file2="c:\\python\\image\\f2.jpg"#图像位置
data = {"api_key":key, "api_secret": secret}
#数据格式化准备发送到face,词典格式json
files = {"image_file1": open(image_file1, "rb"),"image_file2": open(image_file2, "rb")}#准备打开
response = requests.post(http_url, data=data, files=files)#用post方式(还有get)发送数据到网站
req_con = response.content.decode('utf-8')#网页解码
req_dict = JSONDecoder().decode(req_con)#把json解码成python词典格式
print(req_dict)
 
image1=cv.imread(image_file1)
image2=cv.imread(image_file2)       
cv.namedWindow('image1', cv.WINDOW_NORMAL)#弹出窗口命名,窗口自动大小
cv.namedWindow('image2', cv.WINDOW_NORMAL)
h1,w1,c1=image1.shape
h2,w2,c2=image2.shape
cv.resizeWindow('image1',int(w1/3),int(h1/3))
cv.resizeWindow('image2',int(w2/3),int(h2/3))
cv.moveWindow('image2',int(w1/3),0)
 
cv.imshow('image1',image1)#显示图形
cv.imshow('image2',image2)
 
#img: 图像,起始坐标,终点坐标,颜色,线宽。 
cv.waitKey(0)#等键盘动作
cv.destroyAllWindows() #  关闭所有窗口

猜你喜欢

转载自blog.csdn.net/coco_link/article/details/84562466