Example #1
0
def main(img_file: str):
    #tmp_path = Config.get('tmp_train_path')
    tmp_path = "F:\\School\\magic-mirror\\backend\\api\\MirrorOfErised\\MirrorOfErised\\wwwroot\\images"
    #tmp_path = os.path.join( "F:", "School", "magic-mirror","backend","api","MirrorOfErised","MirrorOfErised","wwwroot","images" )
    #train_data_path = Config.get('train_data_path')
    train_data_path = "F:\\School\\magic-mirror\\backend\\api\\MirrorOfErised\\MirrorOfErised\\wwwroot\\images\\test"

    if tmp_path is None:
        raise ('No temp path assigned')

    full_image_path = f'{tmp_path}\\{img_file}'
    #full_image_path=pathlib.Path(tmp_path, img_file)

    image = Image.open(full_image_path)
    image = utils.prepare_image(
        image)  # Check if gray scaled and resize, rotate, flip if needed
    if image is None:
        print('NOK')
        exit(0)

    detector = MTCNN()
    face = detector.forward(image)  # Check for faces
    if face is None:
        print('NOK')
        exit(0)

    try:
        os.renames(full_image_path,
                   f'{train_data_path}\\{img_file.split("_")[1]}\\{img_file}')
    except:
        print('OK - Failed to move file')

    print('OK')
def main(img_file: str):
    tmp_path = Config.get('tmp_train_path')
    train_data_path = Config.get('train_data_path')

    if tmp_path is None:
        raise ('No temp path assigned')

    full_image_path = f'{tmp_path}/{img_file}'
    image = Image.open(full_image_path)
    image = utils.prepare_image(
        image)  # Check if gray scaled and resize, rotate, flip if needed
    if image is None:
        print('NOK')
        exit(0)

    detector = MTCNN()
    face = detector.forward(image)  # Check for faces
    if face is None:
        print('NOK')
        exit(0)

    try:
        os.renames(full_image_path,
                   f'{train_data_path}/{img_file.split("_")[1]}/{img_file}')
    except:
        print('OK - Failed to move file')

    print('OK')
Example #3
0
def predict_one(image_file):
    # Train a model first with train.py
    facial_recognition = load_model()
    detector = MTCNN()

    image = Image.open(image_file)
    image = utils.prepare_image(image)
    face = detector.forward(image)
    prediction = facial_recognition.predict(face)

    print('Prediction:', prediction)
Example #4
0
    def __generate_tensor(self, image_file):
        """Creates a tensor of the faces based on the input image"""
        image = Image.open(image_file)
        image = utils.prepare_image(image)

        face = self.__detector.forward(image)
        if face is None:
            print(image_file, '- NOK')
            return None
        print(image_file, '- OK')
        return face
Example #5
0
def main(test_dir):
    # Train a model first with train.py
    facial_recognition = load_model()

    test_images = utils.load_images(test_dir)
    X_test = []
    y_test = []

    detector = MTCNN()

    for label, images in test_images.items():
        for image in images:
            img_file = Image.open(image)
            img_file = utils.prepare_image(img_file)

            face = detector.forward(img_file)

            X_test.append(face)
            y_test.append(label)

    y_pred = facial_recognition.predict(X_test)
    y_pred = np.array(y_pred)
    print('Report', metrics.classification_report(y_test, y_pred))
    print('Accuracy:', metrics.accuracy_score(y_test, y_pred))