增加人脸识别登录通过文件地址
This commit is contained in:
parent
309b9f3b20
commit
e8f5002aa2
|
@ -21,6 +21,8 @@ class EmployeeNotWorkRemarkSerializer(ModelSerializer):
|
||||||
class FaceLoginSerializer(serializers.Serializer):
|
class FaceLoginSerializer(serializers.Serializer):
|
||||||
base64 = serializers.CharField()
|
base64 = serializers.CharField()
|
||||||
|
|
||||||
|
class FaceLoginPathSerializer(serializers.Serializer):
|
||||||
|
path = serializers.CharField()
|
||||||
|
|
||||||
class FaceClockCreateSerializer(serializers.Serializer):
|
class FaceClockCreateSerializer(serializers.Serializer):
|
||||||
base64 = serializers.CharField()
|
base64 = serializers.CharField()
|
||||||
|
|
|
@ -9,6 +9,32 @@ from django.core.cache import cache
|
||||||
|
|
||||||
class HRMService:
|
class HRMService:
|
||||||
|
|
||||||
|
@classmethod
|
||||||
|
def face_compare_from_path(cls, path):
|
||||||
|
filepath = settings.BASE_DIR +path
|
||||||
|
try:
|
||||||
|
unknown_picture = face_recognition.load_image_file(filepath)
|
||||||
|
unknown_face_encoding = face_recognition.face_encodings(unknown_picture)[0]
|
||||||
|
# os.remove(filepath)
|
||||||
|
except:
|
||||||
|
# os.remove(filepath)
|
||||||
|
return None, '识别失败,请调整位置'
|
||||||
|
|
||||||
|
# 匹配人脸库
|
||||||
|
face_datas = cache.get('face_datas')
|
||||||
|
if face_datas is None:
|
||||||
|
update_all_user_facedata_cache()
|
||||||
|
face_datas = cache.get('face_datas')
|
||||||
|
face_users = cache.get('face_users')
|
||||||
|
results = face_recognition.compare_faces(face_datas,
|
||||||
|
unknown_face_encoding, tolerance=0.38)
|
||||||
|
for index, value in enumerate(results):
|
||||||
|
if value:
|
||||||
|
# 识别成功
|
||||||
|
user = User.objects.get(id=face_users[index])
|
||||||
|
return user, ''
|
||||||
|
return None, '人脸未匹配,请调整位置'
|
||||||
|
|
||||||
@classmethod
|
@classmethod
|
||||||
def face_compare_from_base64(cls, base64_data):
|
def face_compare_from_base64(cls, base64_data):
|
||||||
filename = str(uuid.uuid4())
|
filename = str(uuid.uuid4())
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
from rest_framework import urlpatterns
|
from rest_framework import urlpatterns
|
||||||
from apps.hrm.views import ClockRecordViewSet, EmployeeViewSet, FaceLogin, NotWorkRemarkViewSet
|
from apps.hrm.views import ClockRecordViewSet, EmployeeViewSet, FaceLogin, FacePathLogin, NotWorkRemarkViewSet
|
||||||
from django.urls import path, include
|
from django.urls import path, include
|
||||||
from rest_framework.routers import DefaultRouter
|
from rest_framework.routers import DefaultRouter
|
||||||
|
|
||||||
|
@ -10,6 +10,7 @@ router.register('clock_record', ClockRecordViewSet, basename='clock_record')
|
||||||
router.register('not_work_remark', NotWorkRemarkViewSet, basename='not_work_reamrk')
|
router.register('not_work_remark', NotWorkRemarkViewSet, basename='not_work_reamrk')
|
||||||
urlpatterns = [
|
urlpatterns = [
|
||||||
path('facelogin/', FaceLogin.as_view()),
|
path('facelogin/', FaceLogin.as_view()),
|
||||||
|
path('facelogin_path/', FacePathLogin.as_view()),
|
||||||
path('', include(router.urls)),
|
path('', include(router.urls)),
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
|
@ -10,7 +10,7 @@ from apps.hrm.services import HRMService
|
||||||
from apps.hrm.tasks import update_all_user_facedata_cache
|
from apps.hrm.tasks import update_all_user_facedata_cache
|
||||||
from apps.system.mixins import CreateUpdateModelAMixin, OptimizationMixin
|
from apps.system.mixins import CreateUpdateModelAMixin, OptimizationMixin
|
||||||
from apps.hrm.models import ClockRecord, Employee, NotWorkRemark
|
from apps.hrm.models import ClockRecord, Employee, NotWorkRemark
|
||||||
from apps.hrm.serializers import ClockRecordListSerializer, EmployeeNotWorkRemarkSerializer, EmployeeSerializer, FaceClockCreateSerializer, FaceLoginSerializer, NotWorkRemarkListSerializer
|
from apps.hrm.serializers import ClockRecordListSerializer, EmployeeNotWorkRemarkSerializer, EmployeeSerializer, FaceClockCreateSerializer, FaceLoginPathSerializer, FaceLoginSerializer, NotWorkRemarkListSerializer
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@ -161,6 +161,44 @@ class FaceLogin(CreateAPIView):
|
||||||
"""
|
"""
|
||||||
base64_data = base64.urlsafe_b64decode(tran64(request.data.get('base64').replace(' ', '+')))
|
base64_data = base64.urlsafe_b64decode(tran64(request.data.get('base64').replace(' ', '+')))
|
||||||
user, msg = HRMService.face_compare_from_base64(base64_data)
|
user, msg = HRMService.face_compare_from_base64(base64_data)
|
||||||
|
if user:
|
||||||
|
refresh = RefreshToken.for_user(user)
|
||||||
|
# 可设为在岗
|
||||||
|
now = timezone.now()
|
||||||
|
now_local = timezone.localtime()
|
||||||
|
if 8<=now_local.hour<=17:
|
||||||
|
ins, created = ClockRecord.objects.get_or_create(
|
||||||
|
create_by = user, create_time__hour__range = [8,18],
|
||||||
|
create_time__year=now_local.year, create_time__month=now_local.month,
|
||||||
|
create_time__day=now_local.day,
|
||||||
|
defaults={
|
||||||
|
'type':ClockRecord.ClOCK_WORK1,
|
||||||
|
'create_by':user,
|
||||||
|
'create_time':now
|
||||||
|
})
|
||||||
|
# 设为在岗
|
||||||
|
if created:
|
||||||
|
Employee.objects.filter(user=user).update(is_atwork=True, last_check_time=now)
|
||||||
|
|
||||||
|
return Response({
|
||||||
|
'refresh': str(refresh),
|
||||||
|
'access': str(refresh.access_token),
|
||||||
|
'username':user.username,
|
||||||
|
'name':user.name
|
||||||
|
})
|
||||||
|
return Response(msg, status=status.HTTP_400_BAD_REQUEST)
|
||||||
|
|
||||||
|
class FacePathLogin(CreateAPIView):
|
||||||
|
authentication_classes = []
|
||||||
|
permission_classes = []
|
||||||
|
serializer_class = FaceLoginPathSerializer
|
||||||
|
|
||||||
|
|
||||||
|
def create(self, request, *args, **kwargs):
|
||||||
|
"""
|
||||||
|
人脸识别登录-文件地址
|
||||||
|
"""
|
||||||
|
user, msg = HRMService.face_compare_from_path(request.data.get('path'))
|
||||||
if user:
|
if user:
|
||||||
refresh = RefreshToken.for_user(user)
|
refresh = RefreshToken.for_user(user)
|
||||||
# 可设为在岗
|
# 可设为在岗
|
||||||
|
|
Binary file not shown.
After Width: | Height: | Size: 146 KiB |
Binary file not shown.
After Width: | Height: | Size: 134 KiB |
Binary file not shown.
After Width: | Height: | Size: 140 KiB |
Loading…
Reference in New Issue