question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

KNNClassifier for face detection data format issue

See original GitHub issue

Versions

river version: river==0.13.0 Python version: 3.8 Operating system: Ubuntu 20.04

Describe the bug

I’m trying to train and predict face detection using KNNClassifier

Steps/code to reproduce

Here is the training code.

import math
import pickle

import pandas as pd
from river import neighbors
from river import preprocessing
from river import compose
import os
import face_recognition
from face_recognition.face_recognition_cli import image_files_in_folder


def train(train_dir, model_save_path=None, n_neighbors=None, verbose=False):
    X = []
    y = []
    # Loop through each person in the training set
    for class_dir in os.listdir(train_dir):
        if not os.path.isdir(os.path.join(train_dir, class_dir)):
            continue

        # Loop through each training image for the current person
        for img_path in image_files_in_folder(os.path.join(train_dir, class_dir)):
            image = face_recognition.load_image_file(img_path)
            face_bounding_boxes = face_recognition.face_locations(image)
            # face_bounding_boxes = face_recognition.face_locations(image, number_of_times_to_upsample=2, model="cnn")

            if len(face_bounding_boxes) != 1:
                # If there are no people (or too many people) in a training image, skip the image.
                if verbose:
                    print("Image {} not suitable for training: {}".format(img_path, "Didn't find a face" if len(
                        face_bounding_boxes) < 1 else "Found more than one face"))
            else:
                # Add face encoding for current image to the training set
                # X.append(face_recognition.face_encodings(image, known_face_locations=face_bounding_boxes, num_jitters=100)[0])
                X.append(face_recognition.face_encodings(image, known_face_locations=face_bounding_boxes)[0])
                y.append(class_dir)

    if n_neighbors is None:
        n_neighbors = int(round(math.sqrt(len(X))))
        if verbose:
            print("Chose n_neighbors automatically:", n_neighbors)

    model = neighbors.KNNClassifier(n_neighbors=n_neighbors)
    # xdf = pd.DataFrame(X)
    # ydf = pd.Series(y)
    for encoding, name in zip(X, y):
        model.learn_one({"body": encoding.tolist()}, name)
    # model.learn_many(xdf, ydf)

    if model_save_path is not None:
        with open(model_save_path, 'wb') as f:
            pickle.dump(model, f)


if __name__ == "__main__":
    # STEP 1: Train the KNN classifier and save it to disk
    # Once the model is trained and saved, you can skip this step next time.
    print("Training KNN classifier...")
    train("corpus/train", model_save_path="models/trained_knn_model.clf", n_neighbors=2)
    print("Training complete!")

I have trained a model without any errors but when I tried to predict the face it throws the following error.

import face_recognition
import river
import pickle


def prediction(image):
    with open("models/trained_knn_model.clf", 'rb') as f:
        model = pickle.load(f)
    X_img = face_recognition.load_image_file(image)
    X_face_locations = face_recognition.face_locations(X_img)
    faces_encodings = face_recognition.face_encodings(X_img, known_face_locations=X_face_locations)
    print(model.predict_one({"body": faces_encodings[0].tolist()}))


if __name__ == "__main__":
    # STEP 1: Train the KNN classifier and save it to disk
    # Once the model is trained and saved, you can skip this step next time.
    print("Predicting ....")
    prediction("corpus/train/user_1/User 1.1.jpg")

error:

Predicting ....
Traceback (most recent call last):
  File "/home/iffi/PycharmProjects/FaceDetectionVerification/pred.py", line 19, in <module>
    prediction("corpus/train/user_1/User 1.1.jpg")
  File "/home/iffi/PycharmProjects/FaceDetectionVerification/pred.py", line 12, in prediction
    print(model.predict_one({"body": faces_encodings[0].tolist()}))
  File "/home/iffi/environments/FaceDetectionVerification/lib/python3.8/site-packages/river/base/classifier.py", line 68, in predict_one
    y_pred = self.predict_proba_one(x)
  File "/home/iffi/environments/FaceDetectionVerification/lib/python3.8/site-packages/river/neighbors/knn_classifier.py", line 150, in predict_proba_one
    nearest = self._nn.find_nearest((x, None), n_neighbors=self.n_neighbors)
  File "/home/iffi/environments/FaceDetectionVerification/lib/python3.8/site-packages/river/neighbors/base.py", line 142, in find_nearest
    return sorted(points, key=operator.itemgetter(-1))[:n_neighbors]
  File "/home/iffi/environments/FaceDetectionVerification/lib/python3.8/site-packages/river/neighbors/base.py", line 139, in <genexpr>
    points = ((*p, self.distance_func(item, p[0])) for p in self.window)
  File "/home/iffi/environments/FaceDetectionVerification/lib/python3.8/site-packages/river/neighbors/base.py", line 31, in __call__
    return self.distance_function(a[0], b[0])
  File "/home/iffi/environments/FaceDetectionVerification/lib/python3.8/site-packages/river/utils/math.py", line 163, in minkowski_distance
    return sum((abs(a.get(k, 0.0) - b.get(k, 0.0))) ** p for k in set([*a.keys(), *b.keys()]))
  File "/home/iffi/environments/FaceDetectionVerification/lib/python3.8/site-packages/river/utils/math.py", line 163, in <genexpr>
    return sum((abs(a.get(k, 0.0) - b.get(k, 0.0))) ** p for k in set([*a.keys(), *b.keys()]))
TypeError: unsupported operand type(s) for -: 'list' and 'list'

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (3 by maintainers)

github_iconTop GitHub Comments

1reaction
mirfan899commented, Sep 27, 2022

thanks it worked.

0reactions
mirfan899commented, Sep 27, 2022

Okay, I will test it.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Face Recognition Using Knn & OpenCV | by Manvi Tyagi
Through this project, a very basic form of face recognition has been implemented using the Haar Cascades Classifier, openCV & K-Nearest Neighbors Algorithm....
Read more >
The KNN Algorithm - Explanation, Opportunities, Limitations
KNN is a simple algorithm, based on the local minimum of the target function which is used to learn an unknown function of...
Read more >
FACE RECOGNITION USING BAGGING KNN
In this paper we proposed a novel ensemble based face recognition system which is based on K nearest neighbour classifier and bagging. The...
Read more >
face_recognition/face_recognition_knn.py at master - GitHub
This is an example of using the k-nearest-neighbors (KNN) algorithm for face recognition. When should I use this example?
Read more >
(PDF) Face Identification Based on K-Nearest Neighbor
Face recognition utilizes facial features for security purposes. The classification method in this paper is K-nearest Neighbor (KNN).
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found