Kalman filter with Haar face detection

asked 2016-01-04 14:21:15 -0500

wattar gravatar image

updated 2016-01-05 05:19:02 -0500

Hi, I'm a beginner in OpenCV and python, and I'm trying to use Kalman with Haar cascade face detection, in addition to showing the location and the time, I managed to show the location and time with the face detection, but when I tried to use Kalman I had this problem (AttributeError) and some time different problem, now I comment the code (I couldnt mange to run Kalman therefore its commented now # for Kalman 2), could anyone help in this???

Thank you ...

The code is attached C:\fakepath\Kalman with face.png

   import cv2
   import itertools
   import time  # time
   import numpy as np
   ### for Kalman 1 
   class Pedestrian():
"""Pedestrian class
each pedestrian is composed of a ROI, an ID and a Kalman filter so we create a Pedestrian class to hold the object state
"""
def __init__(self, id, frame, track_window):
    """init the pedestrian object with track window coordinates"""
    # set up the roi
    self.id = int(id)
    x,y,w,h = track_window
    self.track_window = track_window
    self.roi = cv2.cvtColor(frame[y:y+h, x:x+w], cv2.COLOR_BGR2HSV)
    roi_hist = cv2.calcHist([self.roi], [0], None, [16], [0, 180])
    self.roi_hist = cv2.normalize(roi_hist, roi_hist, 0, 255,cv2.NORM_MINMAX)
    # set up the kalman
    self.kalman = cv2.KalmanFilter(4,2)
    self.kalman.measurementMatrix = np.array([[1,0,0,0],[0,1,0,0]],np.float32)
    self.kalman.transitionMatrix = np.array([[1,0,1,0],[0,1,0,1],[0,0,1,0],[0,0,0,1]],np.float32)
    self.kalman.processNoiseCov = np.array([[1,0,0,0],[0,1,0,0],[0,0,1,0],[0,0,0,1]],np.float32) * 0.03
    self.measurement = np.array((2,1), np.float32)
    self.prediction = np.zeros((2,1), np.float32)
    self.term_crit = ( cv2.TERM_CRITERIA_EPS | cv2.TERM_CRITERIA_COUNT, 10,1 )
    self.center = None
    self.update(frame)



   time_count = 0  # time
   face_cascade =cv2.CascadeClassifier('/home/wattar/opencv/opencv-   3.0.0/data/haarcascades/haarcascade_frontalface_alt.xml')
 cap = cv2.VideoCapture(0)
 scaling_factor = 1
 start = time.time() # time

while True:
   ret, frame = cap.read()
   frame = cv2.resize(frame, None, fx=scaling_factor, fy=scaling_factor, interpolation=cv2.INTER_AREA)
   gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
   face_rects = face_cascade.detectMultiScale(gray, 1.3, 5)
   for (x,y,w,h) in face_rects:
        cv2.rectangle(frame, (x,y), (x+w,y+h), (0,255,0), 3)
        print ('X,Y is : %s, %s' % (x,y))
       font = cv2.FONT_HERSHEY_SIMPLEX
       cv2.putText(frame,'X,Y is : %s, %s' % (x,y),(x,y), font, 1,(255,255,255),2)

  for i, p in Pedestrian.iteritems():
      print ("%s", Pedestrian.iteritmes)
      p.update(frame)
      print (p.update(frame))   

cv2.imshow('Face Detector', frame)
c = cv2.waitKey(1)
end = time.time()
print(end - start)

if c == 27:
    break
cap.release()
cv2.destroyAllWindows()
edit retag flag offensive close merge delete

Comments

please show us a text version of your code (not a screenshot), so folks here can actually try it.

berak gravatar imageberak ( 2016-01-05 04:36:07 -0500 )edit

good ! now what error do you get exactly, and where ?

berak gravatar imageberak ( 2016-01-05 04:47:26 -0500 )edit

Thank you for answering, I'm trying to track the face with Kalman, and to be honest till now I didn't understand how. I read the tracking the mouse example and tried to implement it on the face but once I uncomment these lines

# for i, p in Pedestrian.iteritems():
       # print ("%s", Pedestrian.iteritmes)
    #        p.update(frame)
     #       print (p.update(frame)

I have an error (now I'm just trying to track the face with Kalman). Thank you

wattar gravatar imagewattar ( 2016-01-05 04:53:43 -0500 )edit
  • please use the "10101" button to format code properly
  • you fail to show the p.update() function
berak gravatar imageberak ( 2016-01-05 04:57:35 -0500 )edit

Can you check the code now

wattar gravatar imagewattar ( 2016-01-05 05:19:58 -0500 )edit
  • still, where is the error ?
  • where is p.update() ?
berak gravatar imageberak ( 2016-01-05 05:21:36 -0500 )edit

This is the error Traceback (most recent call last): File "Kalman with Haar face .py", line 62, in <module> for i, p in Pedestrian.iteritems():

in this lines:

    for i, p in Pedestrian.iteritems():
         print ("%s", Pedestrian.iteritmes)
         p.update(frame)
         print (p.update(frame))
wattar gravatar imagewattar ( 2016-01-05 05:24:24 -0500 )edit