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 from apps.hrm.services import HrmService
base64_data = base64.urlsafe_b64decode(tran64(request.data.get('base64').replace(' ', '+'))) base64_data = base64.urlsafe_b64decode(tran64(request.data.get('base64').replace(' ', '+')))
ep, msg = HrmService.face_find_from_base64(base64_data) ep, msg = HrmService.face_find_from_base64(base64_data)
if ep and ep.user:
user = ep.user user = ep.user
if user:
refresh = RefreshToken.for_user(ep.user) refresh = RefreshToken.for_user(ep.user)
# # 可设为在岗 # # 可设为在岗
# now = timezone.now() # now = timezone.now()
@ -292,4 +292,4 @@ class FaceLoginView(CreateAPIView):
'username':user.username, 'username':user.username,
'name':user.name '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.third.tapis import dhapis
from apps.utils.tools import rannum, ranstr from apps.utils.tools import rannum, ranstr
from apps.utils.face import face_find from apps.utils.face import face_find
from apps.utils.tools import singleton
myLogger = logging.getLogger('log') myLogger = logging.getLogger('log')
@singleton
class FaceDb():
def __init__(self, face_df=None) -> None:
self.face_df = face_df
facedb = FaceDb()
class HrmService: class HrmService:
@ -412,13 +405,18 @@ class HrmService:
@classmethod @classmethod
def face_find_from_base64(cls, base64_data): def face_find_from_base64(cls, base64_data):
from deepface import DeepFace
img_name = str(uuid.uuid4()) img_name = str(uuid.uuid4())
img_path = settings.BASE_DIR +'/temp/face_' + img_name +'.jpg' img_path = settings.BASE_DIR +'/temp/face_' + img_name +'.jpg'
with open(img_path, 'wb') as f: with open(img_path, 'wb') as f:
f.write(base64_data) f.write(base64_data)
# db_path = os.path.join(settings.BASE_DIR, 'media/face') # db_path = os.path.join(settings.BASE_DIR, 'media/face')
# cache_face_db = cache face_df = cache.get('global_face_df', None)
dfs = face_find(img_path=img_path, global_df=facedb.face_df, model_name='Facenet512') 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] df = dfs[0]
if not df.empty: if not df.empty:
matched = df.iloc[0].identity matched = df.iloc[0].identity

View File

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