#!/usr/bin/env python
# coding=utf-8
"""
@author: shenzh
@contact: shenzh@yutong.com
@file: person_face_compare_api.py
@date: 2022-4-18 8:54
@desc: 根据输入两张图片进行比较判断是否是同一个人
"""
import json
from flask import Flask, request
import uuid
from concurrent.futures import ThreadPoolExecutor
import face_compare_config as config
import shutil
import comparePersonFace
executor = ThreadPoolExecutor()
app = Flask(__name__)
# 只接受POST方法访问
@app.route("/face_compare", methods=["POST"])
def face_compare_method():
return_dict = {'return_code': '200', 'return_info': 'success', 'result': False}
# 判断入参是否为空
if request.get_data() is None:
return_dict['return_code'] = '5004'
return_dict['return_info'] = '请求参数为空'
return json.dumps(return_dict, ensure_ascii=False)
# 获取传入的参数
try:
image_file_datas = request.get_data()
# print("读取的图片:", image_file)
image_file_datas_1 = json.loads(image_file_datas)
face_image_1_base64 = image_file_datas_1.get(config.face_image_1) ##face_image_1 = 'face_image_1'
face_image_2_base64 = image_file_datas_1.get(config.face_image_2) ##face_image_2 = 'face_image_2'
image_1_path = get_file_as_img(face_image_1_base64)
image_2_path = get_file_as_img(face_image_2_base64)
except Exception as e:
return_dict['return_code'] = '5000'
return_dict['return_info'] = '请求参数获取失败:' + str(e)
return json.dumps(return_dict, ensure_ascii=False)
face_compare_result = executor.submit(comparePersonFace.face_compare_method, image_1_path, image_2_path)
face_compare_result = face_compare_result.result()
return_dict['result'] = face_compare_result ##1:不是同一个人,0:是同一个人
# print("删除的图片=",image_path)
shutil.rmtree(image_1_path)
shutil.rmtree(image_2_path)
print("返回的结果=", return_dict)
return json.dumps(return_dict, ensure_ascii=False)
def get_file_as_img(image_file):
path = str(uuid.uuid4()) + ".jpg"
with open(path, 'wb') as f:
f.write(image_file)
return path
if __name__ == "__main__":
app.run(host='0.0.0.0', port=face_port, debug=True)
二、对比的方法
##coding=utf-8
import face_recognition
from PIL import Image
def detect_faces_in_image(file_stream1, file_stream2):
code1 = face_recognition.face_encodings(face_recognition.load_image_file(file_stream1))[0]
code2 = face_recognition.face_encodings(face_recognition.load_image_file(file_stream2))[0]
if len(code1) > 0 and len(code2) > 0:
match_results = face_recognition.face_distance([code1], code2)
print(match_results)
# print(match_results[0])
if match_results[0] > 0.6:
result = "不是同一个人"
else:
result = "同一个人"
return result
def face_compare_method(file_stream1, file_stream2):
code1 = face_recognition.face_encodings(face_recognition.load_image_file(file_stream1))[0]
code2 = face_recognition.face_encodings(face_recognition.load_image_file(file_stream2))[0]
if len(code1) > 0 and len(code2) > 0:
match_results = face_recognition.compare_faces([code1], code2)
print(match_results)
if match_results[0] == False:
result = "1"##不是同一个人
else:
result = "0"##同一个人
return result
def face_location_method(file_stream1):
image = face_recognition.load_image_file(file_stream1)
face_locations = face_recognition.face_locations(image, number_of_times_to_upsample=0, model="cnn")
print("I found {} face(s) in this photograph.".format(len(face_locations)))
for face_location in face_locations:
# Print the location of each face in this image
top, right, bottom, left = face_location
print("A face is located at pixel location Top: {}, Left: {}, Bottom: {}, Right: {}".format(top, left, bottom,
right))
# You can access the actual face itself like this:
face_image = image[top:bottom, left:right]
pil_image = Image.fromarray(face_image)
pil_image.show()
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)