匹配多张人脸情况
This commit is contained in:
parent
6ef6fdc62f
commit
eac3a51c4c
|
@ -14,7 +14,7 @@ class HRMService:
|
|||
filepath = settings.BASE_DIR +path
|
||||
try:
|
||||
unknown_picture = face_recognition.load_image_file(filepath)
|
||||
unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]
|
||||
unknown_face_encoding = face_recognition.face_encodings(unknown_picture, num_jitters=2)[0]
|
||||
# os.remove(filepath)
|
||||
except:
|
||||
# os.remove(filepath)
|
||||
|
@ -27,7 +27,7 @@ class HRMService:
|
|||
face_datas = cache.get('face_datas')
|
||||
face_users = cache.get('face_users')
|
||||
results = face_recognition.compare_faces(face_datas,
|
||||
unknown_face_encoding, tolerance=0.42)
|
||||
unknown_face_encoding, tolerance=0.4)
|
||||
for index, value in enumerate(results):
|
||||
if value:
|
||||
# 识别成功
|
||||
|
@ -43,7 +43,7 @@ class HRMService:
|
|||
f.write(base64_data)
|
||||
try:
|
||||
unknown_picture = face_recognition.load_image_file(filepath)
|
||||
unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]
|
||||
unknown_face_encoding = face_recognition.face_encodings(unknown_picture, num_jitters=2)[0]
|
||||
os.remove(filepath)
|
||||
except:
|
||||
os.remove(filepath)
|
||||
|
@ -56,13 +56,39 @@ class HRMService:
|
|||
face_datas = cache.get('face_datas')
|
||||
face_users = cache.get('face_users')
|
||||
results = face_recognition.compare_faces(face_datas,
|
||||
unknown_face_encoding, tolerance=0.42)
|
||||
unknown_face_encoding, tolerance=0.43)
|
||||
for index, value in enumerate(results):
|
||||
if value:
|
||||
# 识别成功
|
||||
user = User.objects.get(id=face_users[index])
|
||||
return user, ''
|
||||
return None, '人脸未匹配,请调整位置'
|
||||
return user, ''
|
||||
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):
|
||||
|
|
|
@ -18,7 +18,7 @@ def update_all_user_facedata_cache():
|
|||
更新人脸数据缓存
|
||||
"""
|
||||
facedata_queyset = Employee.objects.filter(face_data__isnull=False,
|
||||
user__is_active=True).values('user', 'face_data')
|
||||
user__is_active=True, user__is_deleted = False).values('user', 'face_data')
|
||||
face_users = []
|
||||
face_datas = []
|
||||
for i in facedata_queyset:
|
||||
|
|
Loading…
Reference in New Issue