From 51f55258d6ab3e38652aa2cae55e8d720407b0ca Mon Sep 17 00:00:00 2001 From: caoqianming Date: Mon, 11 Sep 2023 16:40:54 +0800 Subject: [PATCH] =?UTF-8?q?fix:=20=E4=BA=BA=E8=84=B8=E8=AF=86=E5=88=AB?= =?UTF-8?q?=E5=85=88=E8=BF=99=E6=A0=B7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- apps/auth1/views.py | 6 +++--- apps/hrm/services.py | 16 +++++++--------- apps/hrm/tasks.py | 7 ++++--- 3 files changed, 14 insertions(+), 15 deletions(-) diff --git a/apps/auth1/views.py b/apps/auth1/views.py index cb679e9e..0fa6ef09 100755 --- a/apps/auth1/views.py +++ b/apps/auth1/views.py @@ -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) diff --git a/apps/hrm/services.py b/apps/hrm/services.py index 072df66d..a443ca6b 100755 --- a/apps/hrm/services.py +++ b/apps/hrm/services.py @@ -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 diff --git a/apps/hrm/tasks.py b/apps/hrm/tasks.py index abb74c00..722d13e6 100755 --- a/apps/hrm/tasks.py +++ b/apps/hrm/tasks.py @@ -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 \ No newline at end of file