# 校验请求参数
if 'file1' not in request.files or 'file2' not in request.files:
return build_api_result(1010, "请求参数错误", {})
 
# 获取请求参数
file1 = request.files['file1']
file2 = request.files['file2']
print("Reqest params: {'file1': '%s', 'file2': '%s'}" % (file1.filename, file2.filename))
 
# 检查文件扩展名
if not allowed_file(file1.filename) or not allowed_file(file2.filename):
return build_api_result(1020, "存在格式不正确的文件", {})
 
# 加载图片
img1 = face_recognition.load_image_file(file1)
img2 = face_recognition.load_image_file(file2)
 
# 获取人脸编码
code1 = face_recognition.face_encodings(img1)
code2 = face_recognition.face_encodings(img2)
 
# 校验是否识别到人脸
if len(code1) == 0 or len(code2) == 0:
return build_api_result(1030, "存在未识别到人脸的图像", {})
 
# 匹配
match_results = face_recognition.compare_faces([code2[0]], code1[0], tolerance=tolerance)
recognition_result = 1 if match_results[0] else 0
 
# 测试两张图像中人脸比对的差值(不相似度)
face_distances = face_recognition.face_distance([code2[0]], code1[0])