6

I'm working with face recognition using Python.

I have a following code:

from sklearn.externals import joblib
clf = joblib.load('model/svm.pkl')
pca = joblib.load('model/pca.pkl')
face_cascade = cv2.CascadeClassifier("classifier/haarcascade_frontalface_alt.xml")
webcam = cv2.VideoCapture(0)
ret, frame = webcam.read()
while ret:
    start = time()
    origin = frame
    gray = cv2.cvtColor(origin, cv2.COLOR_BGR2GRAY)
    cv2.equalizeHist(gray,gray)

    faces = face_cascade.detectMultiScale(
        origin,
        scaleFactor=1.1,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )
    for (x, y, w, h) in faces:
        cv2.rectangle(origin, (x, y), (x+w, y+h), (0, 255, 0), 2)
        face = gray[y:y+h , x:x+w]
        cv2.equalizeHist(face,face)

        face_to_predict = cv2.resize(face,(100, 100),interpolation = cv2.INTER_AREA)

        img = face_to_predict.ravel()
        principle_components = pca.transform(img)
        proba = clf.predict_proba(principle_components) # probability
        pred = clf.predict(principle_components)
        if proba[0][pred]>0.4:
            name = face_profile_names[pred[0]]

So, this code works fine and from time to time it recognizes faces as I expected. But there are also a lot of weakness here: if I'm twisting my head the accuracy is too low for me. I've found Kalman's filter to improve my face recognition, but I didn't realize how to use it with my existing code.

I've found a few post with using Kalman's filter, but it's not clear enough how it may be used in current case. Some of posts are here: Is there any example of cv2.KalmanFilter implementation?

OpenCV Kalman Filter python

So, my principle_components is a Matrix of values and hopefully it may be used for initialization of my Kalman filter, but I'm not sure about that and how this filter may be used after..

Any thoughts?

Community
  • 1
  • 1
smart
  • 1,975
  • 5
  • 26
  • 46
  • This line seems doubtful to me `Kalman's filter to improve my face recognition`, Kalman filter can be used to smooth out the small variances in data, it cannot help in increasing accuracy, It is not an Image filter, instead it takes some random points as input and tries to make a smooth curve out of them, https://www.google.co.in/search?espv=2&biw=1280&bih=597&tbm=isch&sa=1&q=kalman+filter&oq=kalman+filter&gs_l=img.3..0j0i67k1j0l8.2219.2219.0.2922.1.1.0.0.0.0.114.114.0j1.1.0....0...1c.1.64.img..0.1.113.6nct0qh9jKs#imgrc=LydPDl-i9rPIOM: – ZdaR Apr 23 '17 at 05:42
  • @ZdaR, so as I wrote, I'm getting matrix of numbers from my image, that's why I expect to have some 'better' values – smart Apr 23 '17 at 08:52
  • Best Answer I found here to use the Kalman filter easily. https://stackoverflow.com/a/53017661/7060530 – Sayed Mohsin Reza Oct 27 '18 at 00:16

1 Answers1

4

Opencv Python Documentation on Kalman filter is terrible. A good example of an implementation can be found here: https://raw.githubusercontent.com/tobybreckon/python-examples-cv/master/kalman_tracking_live.py

One aspect that confuses a lot of people is that the Kalman filter has no initialization function, which is just lame. So the filter is a "delta". What I mean by that is that you will always need to normalize with an initial value. The measure should be corrected as measure = measure - initial and the prediction = prediction + initial.

I hope this give you some help.

Fred Guth
  • 1,537
  • 1
  • 15
  • 27