Пример #1
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'}
Пример #2
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'}