104 lines
3.9 KiB
Python
104 lines
3.9 KiB
Python
from django.conf import settings
|
|
import uuid
|
|
import face_recognition
|
|
import os
|
|
from apps.hrm.models import Employee
|
|
from apps.hrm.tasks import update_all_user_facedata_cache
|
|
from apps.system.models import User
|
|
from django.core.cache import cache
|
|
|
|
class HRMService:
|
|
|
|
@classmethod
|
|
def face_compare_from_path(cls, path, tolerance=0.38):
|
|
filepath = settings.BASE_DIR +path
|
|
try:
|
|
unknown_picture = face_recognition.load_image_file(filepath)
|
|
unknown_face_encoding = face_recognition.face_encodings(unknown_picture, num_jitters=2)[0]
|
|
# os.remove(filepath)
|
|
except:
|
|
# os.remove(filepath)
|
|
return None, '识别失败,请调整位置'
|
|
|
|
# 匹配人脸库
|
|
face_datas = cache.get('face_datas')
|
|
if face_datas is None:
|
|
update_all_user_facedata_cache()
|
|
face_datas = cache.get('face_datas')
|
|
face_users = cache.get('face_users')
|
|
results = face_recognition.compare_faces(face_datas,
|
|
unknown_face_encoding, tolerance=tolerance)
|
|
user_index = cls.get_user_index(results)
|
|
user_index_len = len(user_index)
|
|
if user_index_len == 1:
|
|
user = User.objects.get(id=face_users[user_index[0]])
|
|
return user, ''
|
|
elif user_index_len == 0:
|
|
return None, '人脸未匹配,请调整位置'
|
|
else:
|
|
user_ids = []
|
|
for i in user_index:
|
|
user_ids.append(face_users[i])
|
|
user_name_str = ','.join(list(User.objects.filter(id__in=user_ids).values_list('name', flat=True)))
|
|
return None, '匹配多张人脸:' + user_name_str
|
|
|
|
@classmethod
|
|
def face_compare_from_base64(cls, base64_data, tolerance=0.38):
|
|
filename = str(uuid.uuid4())
|
|
filepath = settings.BASE_DIR +'/temp/' + filename +'.png'
|
|
with open(filepath, 'wb') as f:
|
|
f.write(base64_data)
|
|
try:
|
|
unknown_picture = face_recognition.load_image_file(filepath)
|
|
unknown_face_encoding = face_recognition.face_encodings(unknown_picture, num_jitters=2)[0]
|
|
os.remove(filepath)
|
|
except:
|
|
os.remove(filepath)
|
|
return None, '识别失败,请调整位置'
|
|
|
|
# 匹配人脸库
|
|
face_datas = cache.get('face_datas')
|
|
if face_datas is None:
|
|
update_all_user_facedata_cache()
|
|
face_datas = cache.get('face_datas')
|
|
face_users = cache.get('face_users')
|
|
results = face_recognition.compare_faces(face_datas,
|
|
unknown_face_encoding, tolerance=tolerance)
|
|
user_index = cls.get_user_index(results)
|
|
user_index_len = len(user_index)
|
|
if user_index_len == 1:
|
|
user = User.objects.get(id=face_users[user_index[0]])
|
|
return user, ''
|
|
elif user_index_len == 0:
|
|
return None, '人脸未匹配,请调整位置'
|
|
else:
|
|
user_ids = []
|
|
for i in user_index:
|
|
user_ids.append(face_users[i])
|
|
user_name_str = ','.join(list(User.objects.filter(id__in=user_ids).values_list('name', flat=True)))
|
|
return None, '匹配多张人脸:' + user_name_str
|
|
|
|
|
|
@classmethod
|
|
def get_user_index(cls, results):
|
|
"""
|
|
返回user_index列表
|
|
"""
|
|
true_num = 0
|
|
user_index = []
|
|
for index, value in enumerate(results):
|
|
if value:
|
|
true_num = true_num + 1
|
|
user_index.append(index)
|
|
return user_index
|
|
|
|
@classmethod
|
|
def get_facedata_from_img(cls, img_path):
|
|
try:
|
|
photo_path = settings.BASE_DIR + img_path
|
|
picture_of_me = face_recognition.load_image_file(photo_path)
|
|
my_face_encoding = face_recognition.face_encodings(picture_of_me, num_jitters=2)[0]
|
|
face_data_list = my_face_encoding.tolist()
|
|
return face_data_list, ''
|
|
except:
|
|
return None, '人脸数据获取失败请重新上传图片' |