1

I am new at Django and I'm making a demo chat website through Django 2.0. My motive here is to save people's photos when they sign up, and run a face authentication python script on the backend (which I have already done, through the open source face_recognition library on Python) to recognise user's when they login. My script uses cv2 right now to click a photo and sends it over to the face recognition engine.

I have to save the photos of the users in a directory on the server-side, with the name of the image as the Name of the user when they sign up, so I can run a face authenticator to for-loop over the list of faces in my folder to find the matching face. And when it finds that, I can return the name to query over my database and create a session for that particular User. (I know it is time taking and resource intensive, but since it is a demo, I think i can get by it. Also kindly suggest if there can be a faster way for face based authentication)

My User Model looks like this

class User(models.Model):
    username = models.CharField(max_length=100)
    name = models.CharField(max_length=100)
    age = models.CharField(max_length=100)
    image = models.ImageFile()

Can anybody experienced in Django guide me the pathway and required steps through which I can do this?

Vitor Freitas
  • 3,550
  • 1
  • 24
  • 35
AkshatP
  • 544
  • 4
  • 14

1 Answers1

0

I assume that you installed OpenCV, cv2 for python, numpy for sure , face_recognition

utils.py

def recognize_face(id_image, q_image):
    """

    :param image: image of the face in the id
    :param q_image: image of the face from the cam
    :return:
    """
    q_face_encoding = face_recognition.face_encodings(id_image)[0]

    face_locations = face_recognition.face_locations(q_image)
    face_encodings = face_recognition.face_encodings(q_image, face_locations)

    # Loop through each face in this image
    for face_encoding in face_encodings:
        # See if the face is a match for the known face(s)
        match = face_recognition.compare_faces([q_face_encoding], face_encoding)

        result = False
        if match[0]:
            result = True
        return result

def _grab_image(path=None, stream=None, url=None):
    # if the path is not None, then load the image from disk
    if path is not None:
        image = cv2.imread(path)

    # otherwise, the image does not reside on disk
    else:
        # if the URL is not None, then download the image
        if url is not None:
            resp = requests.get(url)
            data = resp.text()

        # if the stream is not None, then the image has been uploaded
        elif stream is not None:
            data = stream.read()

        # convert the image to a NumPy array and then read it into
        # OpenCV format
        image = np.asarray(bytearray(data), dtype="uint8")
        image = cv2.imdecode(image, cv2.IMREAD_COLOR)

    # return the image
    return image

views.py

class FaceVerifyAPIView(APIView):
    http_method_names = ['get', 'post', 'options']
    parser_classes = (parsers.MultiPartParser, parsers.FormParser)
    renderer_classes = (renderers.JSONRenderer, )

    def post(self, request, *args, **kwargs):
        serializer = FaceDectSerializer(data=request.data)
        serializer.is_valid(raise_exception=True)
        data = serializer.validated_data
        id_image = data['id_image']
        q_image = data['q_image']  # OR from request.user.image
        id_image = _grab_image(stream=id_image)
        q_image = _grab_image(stream=q_image)
        result = recognize_face(id_image, q_image)
        return JsonResponse({'message': str(result)}, status=200)


    def get(self, request, *args, **kwargs):
        return JsonResponse({'message': 'You\'re here but use post method'}, status=201)

Capture a photo for his face, take this reference

A.Raouf
  • 2,171
  • 1
  • 24
  • 36