-
Notifications
You must be signed in to change notification settings - Fork 1
/
backends.py
54 lines (44 loc) · 1.91 KB
/
backends.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
from django.core.exceptions import PermissionDenied
from .utils import get_face_recognition_cnn
from .models import User
from PIL import Image
import numpy as np
import logging
import time
logger = logging.getLogger(__name__)
class FaceAuthenticationBackend(object):
def authenticate(self, request, **kwargs):
if 'face_img' not in request.FILES:
raise PermissionDenied
try:
user = User.objects.get(email=kwargs["username"])
except User.DoesNotExist:
raise PermissionDenied
logger.debug("Retrieving face recognition CNN")
cnn = get_face_recognition_cnn()
try:
logger.debug("Converting image to numpy array")
face_img = np.array(Image.open(request.FILES['face_img'])).astype(np.float)
except Exception as e:
logger.error("Exception in face recognition: {} ({})".format(str(e), type(e)))
raise PermissionDenied
if len(face_img.shape) != 3 or face_img.shape[0] != cnn.input_height or face_img.shape[1] != cnn.input_width or face_img.shape[2] != cnn.input_channels:
logger.info("Dimensions mismatch")
raise PermissionDenied
try:
before = time.time()
class_probabilities = cnn.inference(face_img[None, :])[0]
after = time.time()
logger.debug("Inference took {} seconds ...".format(after - before))
most_likely_class = np.argmax(class_probabilities)
if class_probabilities[most_likely_class] <= 0.5 or user.id != most_likely_class:
raise PermissionDenied
return user
except Exception as e:
logger.error("Exception in face recognition: {} ({})".format(str(e), type(e)))
def get_user(self, user_id):
try:
user = User.objects.get(id=user_id)
return user
except User.DoesNotExist:
return None