使用face++接口实现人脸相似度比对

#其中的ket 和secret 可以去网上注册
# coding:utf-8
import requests
from json import JSONDecoder
import cv2
import os

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 = "" #注册一个免费用
secret="" #注册一个免费用

# faceId1 = "img_test/tong1.jpg"  # 图片地址
# faceId2 = "img_test/tong2.jpg"  # 图片地址
faceId1 = str(os.getcwd()) + r"\images\1.jpg"#拼接出图片的存放地址
faceId2 = str(os.getcwd()) + r"\images\2.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)
if confindence>=80:
    print("数据比对成功,这两个人是同一个人,他们的相似度为%s"%confindence)
elif confindence>=70:
    print("相似度为%s不确定是不是同一个人"%confindence)
else:
    print("相似度为%s,两个人不是同一个人"%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()

你可能感兴趣的:(python,机器学习,人工智能)