Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python opencv: How to use Kalman filter

Tags:

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?

like image 914
smart Avatar asked Apr 22 '17 18:04

smart


1 Answers

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.

like image 99
Fred Guth Avatar answered Sep 24 '22 10:09

Fred Guth