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 = '人力资源管理'
def ready(self):
if cache.get('update_global_face_pd_task', True):
from apps.hrm.tasks import update_global_face_pd
update_global_face_pd.delay()
cache.set('update_global_face_pd_task', False, timeout=30)
# if cache.get('update_global_face_pd_task', True):
# from apps.hrm.tasks import update_global_face_pd
# update_global_face_pd.delay()
# cache.set('update_global_face_pd_task', False, timeout=30)
return super().ready()

View File

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

View File

@ -405,18 +405,18 @@ class HrmService:
@classmethod
def face_find_from_base64(cls, base64_data):
from deepface import DeepFace
# 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')
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()
face_df = cache.get('global_face_df')
dfs = face_find(img_path=img_path, global_df=face_df)
global_face = cache.get('global_face', None)
if global_face is None:
from apps.hrm.tasks import update_global_face
global_face = update_global_face()
global_face = cache.get('global_face')
dfs = face_find(img_path=img_path, global_face=global_face)
# dfs = DeepFace.find(img_path=img_path, db_path=db_path)
df = dfs[0]
if not df.empty:

View File

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

View File

@ -5,7 +5,7 @@ import time
def face_find(
img_path,
global_df,
global_face: list,
model_name="VGG-Face",
distance_metric="cosine",
enforce_detection=True,
@ -17,7 +17,7 @@ def face_find(
tic = time.time()
target_size = functions.find_target_size(model_name=model_name)
# 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
target_objs = functions.extract_faces(