Exemplo n.º 1
0
def FaceRecogniseInImage(request, filename):
    file_path = os.path.join(MEDIA_ROOT, 'images/' + filename)
    handle_uploaded_file(request.FILES['file'], file_path)
    file = request.FILES['file']

    if file and allowed_file(filename=filename, allowed_set=allowed_set):
        try:
            file_form = InputImage(title=filename)
            file_form.save()
        except Exception as e:
            return (e)

        img = imread(fname=file, mode='RGB')
        if (img.shape[2] == 4):
            img = img[..., :3]

        try:
            all_faces, all_bb = get_face(img=img,
                                         pnet=pnet,
                                         rnet=rnet,
                                         onet=onet,
                                         image_size=image_size)
            all_face_dict = {}
            if all_faces is not None:
                for img, bb in zip(all_faces, all_bb):
                    embedding = embed_image(
                        img=img,
                        session=facenet_persistent_session,
                        images_placeholder=images_placeholder,
                        embeddings=embeddings,
                        phase_train_placeholder=phase_train_placeholder,
                        image_size=image_size)

                    if embedding_dict:
                        id_name = identify_face(embedding=embedding,
                                                embedding_dict=embedding_dict)
                        bounding_box = {
                            "top": bb[1],
                            "bottom": bb[3],
                            "left": bb[0],
                            "right": bb[2]
                        }
                        all_face_dict[id_name] = {
                            "Bounding Boxes": bounding_box
                        }
                # try:
                #     with open(os.path.join(MEDIA_ROOT, 'output/image',filename.split('.')[0]+'.json'), 'w') as fp:
                #         json.dump(all_face_dict, fp)
                # except Exception as e:
                #     print(e)
                # pass
                file_form.isProcessed = True
                file_form.save()
                return all_face_dict
            else:
                return 'error no faces'
        except Exception as e:
            return e
    else:
        return {"Error": 'bad file format'}
Exemplo n.º 2
0
def createEmbedding(request, filename):
    """      To create face embedding

    Args:
            *   request: Post https request containing a image file
            *   filename: filename of the video

    Workflow
            *   First it checks whether is there any image file in the post request.

            *   Image information is saved in the database

            *   face and corresponding bounding box is extracted followed by creating and saving bounding box.

    Returns:
            *   success flag
    """
    file = request.FILES['file']
    if file and allowed_file(filename=filename, allowed_set=allowed_set):
        filename = secure_filename(filename=filename).replace(
            '_', ' ').split('.')[0].title()
        unid = uuid.uuid4().hex
        try:
            filepath = "/media/face/" + str(unid) + '.jpg'
            file_form = InputEmbed(id=unid, title=filename, fileurl=filepath)
            file_form.save()
        except Exception as e:
            return (e)

        img = imread(fname=file, mode='RGB')
        if (img.shape[2] == 4):
            img = img[..., :3]

        try:
            face, bb = get_face(img=img,
                                pnet=pnet,
                                rnet=rnet,
                                onet=onet,
                                image_size=image_size)
            if face is not None:
                embedding = embed_image(
                    img=face[0],
                    session=facenet_persistent_session,
                    images_placeholder=images_placeholder,
                    embeddings=embeddings,
                    phase_train_placeholder=phase_train_placeholder,
                    image_size=image_size)
                save_face(img=face[0], where='face', filename=unid)
                save_embedding(embedding=embedding,
                               filename=filename,
                               embeddings_path=embeddings_path)

                return 'success'
            else:
                return {"Error": 'No face found'}
        except Exception as e:
            return e
    else:
        return {"Error": 'bad file format'}
Exemplo n.º 3
0
def createEmbedding(request, filename):
    file = request.FILES['file']
    if file and allowed_file(filename=filename, allowed_set=allowed_set):
        filename = secure_filename(filename=filename).replace(
            '_', ' ').split('.')[0].title()
        unid = uuid.uuid4().hex
        try:
            filepath = "/media/face/" + str(unid) + '.jpg'
            file_form = InputEmbed(id=unid, title=filename, fileurl=filepath)
            file_form.save()
        except Exception as e:
            return (e)

        img = imread(fname=file, mode='RGB')
        if (img.shape[2] == 4):
            img = img[..., :3]

        try:
            face, bb = get_face(img=img,
                                pnet=pnet,
                                rnet=rnet,
                                onet=onet,
                                image_size=image_size)
            if face is not None:
                embedding = embed_image(
                    img=face[0],
                    session=facenet_persistent_session,
                    images_placeholder=images_placeholder,
                    embeddings=embeddings,
                    phase_train_placeholder=phase_train_placeholder,
                    image_size=image_size)
                save_face(img=face[0], filename=unid)
                save_embedding(embedding=embedding,
                               filename=filename,
                               embeddings_path=embeddings_path)

                return 'success'
            else:
                return {"Error": 'No face found'}
        except Exception as e:
            return e
    else:
        return {"Error": 'bad file format'}
Exemplo n.º 4
0
def FaceRecogniseInImage(request, filename):
    """     Face Recognition in image

    Args:
            *   request: Post https request containing a image file
            *   filename: filename of the video

    Workflow:
            *   Image file is first saved into images which is subfolder of MEDIA_ROOT directory.

            *   If there is any file in the post request and the file extension is mentioned in allowed_set then it is
                allowed for further processing else the returns an error.

            *   Then, the image is converted into numpy array, followed by reshaping the image dimension if it contains 4 channels.
                It is actually done to process .png files.

            *   Then, all the faces present in an image is extracted along with corresponding boundingbox using method get_face.

            *   if number of faces is greater than 0 then for each face and corresponding bounding box is taken for further processing.

            *   embedding for each face is created with the help of embed_image and returned to the vairable embedding

            *   Now this embedding is compared with already available embeddings, It returns the name of the embedding which has the
                lowest difference in them else if it crosses the limit then 'unknown' id returned.

            *   Next step is to get the facial expression of the face using the method FaceExp.

            *   Then, all the information which currently includes face id, bounding box and facial expression is saved into a dictionary.

            *   Information about the face is saved into database.


    Returns:
            *   Dictionary having all the faces and corresponding bounding boxes with facial expression
    """
    file_path = os.path.join(MEDIA_ROOT, 'images/' + filename)
    handle_uploaded_file(request.FILES['file'], file_path)
    file = request.FILES['file']

    if file and allowed_file(filename=filename, allowed_set=allowed_set):
        try:
            file_form = InputImage(title=filename)
            file_form.save()
        except Exception as e:
            return (e)

        img = imread(fname=file, mode='RGB')
        if (img.shape[2] == 4):
            img = img[..., :3]

        try:
            all_faces, all_bb = get_face(img=img,
                                         pnet=pnet,
                                         rnet=rnet,
                                         onet=onet,
                                         image_size=image_size)
            all_face_dict = {}
            if all_faces is not None:
                for img, bb in zip(all_faces, all_bb):
                    embedding = embed_image(
                        img=img,
                        session=facenet_persistent_session,
                        images_placeholder=images_placeholder,
                        embeddings=embeddings,
                        phase_train_placeholder=phase_train_placeholder,
                        image_size=image_size)

                    if embedding_dict:
                        id_name = identify_face(embedding=embedding,
                                                embedding_dict=embedding_dict)
                        facial_expression = FaceExp(img)
                        bounding_box = {
                            "top": bb[1],
                            "bottom": bb[3],
                            "left": bb[0],
                            "right": bb[2]
                        }
                        all_face_dict[id_name] = {
                            "Bounding Boxes": bounding_box,
                            "Facial Expression": facial_expression
                        }
                # try:
                #     with open(os.path.join(MEDIA_ROOT, 'output/image',filename.split('.')[0]+'.json'), 'w') as fp:
                #         json.dump(all_face_dict, fp)
                # except Exception as e:
                #     print(e)
                # pass
                file_form.isProcessed = True
                file_form.save()
                return {"Faces": all_face_dict}
            else:
                return 'error no faces'
        except Exception as e:
            return e
    else:
        return {"Error": 'bad file format'}
Exemplo n.º 5
0
def facerecogniseinimage(request, filename, network):
    """     Face Recognition in image
    Args:
            *   request: Post https request containing a image file
            *   filename: filename of the video
            *   network: Network architecture to be used for face detection
    Workflow:
            *   Image file is first saved into images which is subfolder of
                MEDIA_ROOT directory.
            *   If there is any file in the post request and file extension is
                mentioned in allowed_set then it is allowed for further
                processing else the returns an error.
            *   Then, the image is converted into numpy array, followed by
                reshaping the image dimension if it contains 4 channels.
                It is actually done to process .png files.
            *   Then, all the faces present in an image is extracted along
                with corresponding boundingbox using method get_face.
            *   if number of faces is greater than 0 then for each face and
                corresponding bounding box is taken for further processing.
            *   embedding for each face is created with help of embed_image
                and returned to the vairable embedding
            *   Now this embedding is compared with already available
                embeddings, It returns the name of the embedding which has the
                lowest difference in them else if it crosses the limit then
                'unknown' id returned.
            *   Next step is to get the facial expression of the face using
                the method faceexp.
            *   Then, all the information which currently includes face id,
                bounding box and facial expression is saved into a dictionary.
            *   Information about the face is saved into database.
    Returns:
            *   Dictionary having all the faces and corresponding bounding
                boxes with facial expression
    """
    file_path = os.path.join(MEDIA_ROOT, 'images/' + filename)
    handle_uploaded_file(request.FILES['file'], file_path)
    file = request.FILES['file']

    if file and allowed_file(filename=filename, allowed_set=allowed_set):
        try:
            file_form = InputImage(title=filename)
            file_form.save()
        except IntegrityError as eri:
            return {"Error": "Integrity Error"}
        except DatabaseError as erd:
            return {"Error": "Database Error"}
        except Exception as e:
            return {"Error": e}

        img = imread(fname=file, mode='RGB')
        if (img.shape[2] == 4):
            img = img[..., :3]

        try:

            if network == ImageFrNetworkChoices.RetinaFace:
                all_faces, all_bb = FaceDetectionRetina().get_face(file_path)

            else:
                all_faces, all_bb = get_face(img=img, pnet=pnet,
                                             rnet=rnet, onet=onet,
                                             image_size=image_size)

            all_face_arr = []

            if all_faces is not None:
                for img, bb in zip(all_faces, all_bb):
                    embedding = embed_image(img=img,
                                            session=facenet_persistent_session,
                                            images_placeholder=images_placeholder,
                                            embeddings=embeddings,
                                            phase_train_placeholder=phase_train_placeholder,
                                            image_size=image_size)

                    if embedding_dict:
                        id_name = identify_face(embedding=embedding,
                                                embedding_dict=embedding_dict)
                        facial_expression = faceexp(img)

                        bounding_box = {"top": bb[1], "bottom": bb[3],
                                        "left": bb[0], "right": bb[2]}
                        face_dict = {"Identity": id_name,
                                     "Bounding Boxes": bounding_box,
                                     "Facial Expression": facial_expression, }
                        all_face_arr.append(face_dict)
                file_form.is_processed = True
                file_form.save()
                return {"Faces": all_face_arr, }

            else:
                return {"Error": "No Faces"}
        except Exception as e:
            raise e
            return {"Error": e}
    else:
        return {"Error": 'bad file format'}