fix: 人脸识别先这样

This commit is contained in:
caoqianming 2023-09-11 16:40:54 +08:00
parent bfdc5d66ca
commit 51f55258d6
3 changed files with 14 additions and 15 deletions

View File

@ -266,8 +266,8 @@ class FaceLoginView(CreateAPIView):
from apps.hrm.services import HrmService
base64_data = base64.urlsafe_b64decode(tran64(request.data.get('base64').replace(' ', '+')))
ep, msg = HrmService.face_find_from_base64(base64_data)
user = ep.user
if user:
if ep and ep.user:
user = ep.user
refresh = RefreshToken.for_user(ep.user)
# # 可设为在岗
# now = timezone.now()
@ -292,4 +292,4 @@ class FaceLoginView(CreateAPIView):
'username':user.username,
'name':user.name
})
return Response(msg, status=400)
raise ParseError(msg)

View File

@ -16,16 +16,9 @@ from apps.third.models import TDevice
from apps.third.tapis import dhapis
from apps.utils.tools import rannum, ranstr
from apps.utils.face import face_find
from apps.utils.tools import singleton
myLogger = logging.getLogger('log')
@singleton
class FaceDb():
def __init__(self, face_df=None) -> None:
self.face_df = face_df
facedb = FaceDb()
class HrmService:
@ -412,13 +405,18 @@ class HrmService:
@classmethod
def face_find_from_base64(cls, base64_data):
from deepface import DeepFace
img_name = str(uuid.uuid4())
img_path = settings.BASE_DIR +'/temp/face_' + img_name +'.jpg'
with open(img_path, 'wb') as f:
f.write(base64_data)
# db_path = os.path.join(settings.BASE_DIR, 'media/face')
# cache_face_db = cache
dfs = face_find(img_path=img_path, global_df=facedb.face_df, model_name='Facenet512')
face_df = cache.get('global_face_df', None)
if face_df is None:
from apps.hrm.tasks import update_global_face_pd
face_df = update_global_face_pd()
dfs = face_find(img_path=img_path, global_df=face_df, model_name='Facenet512')
# dfs = DeepFace.find(img_path=img_path, db_path=db_path)
df = dfs[0]
if not df.empty:
matched = df.iloc[0].identity

View File

@ -8,7 +8,7 @@ from dateutil import tz
from django.core.cache import cache
from apps.hrm.models import Employee
from apps.hrm.services import HrmService, facedb
from apps.hrm.services import HrmService
from apps.third.dahua import dhClient
from apps.third.tapis import dhapis
from apps.utils.tasks import CustomTask
@ -131,5 +131,6 @@ def update_global_face_pd():
import pandas as pd
facedata = Employee.objects.filter(facenet512_data__isnull=False,
user__is_active=True).values_list('id', 'facenet512_data')
cache.set('global_face_data', facedata, timeout=None)
facedb.face_df = pd.DataFrame(list(facedata), columns=["identity", "Facenet512_representation"])
face_df = pd.DataFrame(list(facedata), columns=["identity", "Facenet512_representation"])
cache.set('global_face_df', face_df, timeout=None)
return face_df