feat: 尝试优化人脸识别速度

This commit is contained in:
caoqianming 2023-09-12 07:42:18 +08:00
parent 4448eb2a68
commit 42760f1501
5 changed files with 17 additions and 19 deletions

View File

@ -7,8 +7,8 @@ class HrmConfig(AppConfig):
verbose_name = '人力资源管理' verbose_name = '人力资源管理'
def ready(self): def ready(self):
if cache.get('update_global_face_pd_task', True): # if cache.get('update_global_face_pd_task', True):
from apps.hrm.tasks import update_global_face_pd # from apps.hrm.tasks import update_global_face_pd
update_global_face_pd.delay() # update_global_face_pd.delay()
cache.set('update_global_face_pd_task', False, timeout=30) # cache.set('update_global_face_pd_task', False, timeout=30)
return super().ready() return super().ready()

View File

@ -77,8 +77,8 @@ class EmployeeCreateUpdateSerializer(CustomModelSerializer):
instance.save() instance.save()
else: else:
raise ParseError(msg) raise ParseError(msg)
from apps.hrm.tasks import update_global_face_pd from apps.hrm.tasks import update_global_face
update_global_face_pd.delay() update_global_face.delay()
if instance.user and instance != old_name: if instance.user and instance != old_name:
instance.user.name = instance.name instance.user.name = instance.name
instance.user.save() instance.user.save()

View File

@ -405,18 +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 # 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')
face_df = cache.get('global_face_df', None) global_face = cache.get('global_face', None)
if face_df is None: if global_face is None:
from apps.hrm.tasks import update_global_face_pd from apps.hrm.tasks import update_global_face
face_df = update_global_face_pd() global_face = update_global_face()
face_df = cache.get('global_face_df') global_face = cache.get('global_face')
dfs = face_find(img_path=img_path, global_df=face_df) dfs = face_find(img_path=img_path, global_face=global_face)
# dfs = DeepFace.find(img_path=img_path, db_path=db_path) # 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:

View File

@ -127,9 +127,7 @@ def delete_face_pkl(epId):
@shared_task(base=CustomTask) @shared_task(base=CustomTask)
def update_global_face_pd(): def update_global_face():
import pandas as pd
facedata = Employee.objects.filter(face_data__isnull=False, facedata = Employee.objects.filter(face_data__isnull=False,
user__is_active=True).values_list('id', 'face_data') user__is_active=True).values_list('id', 'face_data')
face_df = pd.DataFrame(list(facedata), columns=["identity", "VGG-Face_representation"]) cache.set('global_face', list(facedata), timeout=None)
cache.set('global_face_df', face_df, timeout=None)

View File

@ -5,7 +5,7 @@ import time
def face_find( def face_find(
img_path, img_path,
global_df, global_face: list,
model_name="VGG-Face", model_name="VGG-Face",
distance_metric="cosine", distance_metric="cosine",
enforce_detection=True, enforce_detection=True,
@ -17,7 +17,7 @@ def face_find(
tic = time.time() tic = time.time()
target_size = functions.find_target_size(model_name=model_name) target_size = functions.find_target_size(model_name=model_name)
# now, we got representations for facial database # now, we got representations for facial database
df = global_df df = pd.DataFrame(global_face, columns=["identity", f"{model_name}_representation"])
# img path might have more than once face # img path might have more than once face
target_objs = functions.extract_faces( target_objs = functions.extract_faces(