Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

Kalman filter with Haar face detection

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 ...

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)

end of for kalman 1

import cv2

import time # time

import numpy as np

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 Kalman 2

# for i, p in frame.iteritems(): # p.update(frame)

# 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)
'''
end_time = time.time()  
if(face_rects):
    time_count+= end_time - start_time
    if(time_count > 2):
        print('time')
else:
    time_count = 0

''' if c == 27: break cap.release() cv2.destroyAllWindows()

Kalman filter with Haar face detection

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 ...

import cv2 import itertools import time # time import numpy as np

for Kalman 1

class Pedestrian(): """Pedestrian class each pedestrian The code 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 attached C:\fakepath\Kalman 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)

end of for kalman 1

import cv2

import time # time

import numpy as np

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 Kalman 2

# for i, p in frame.iteritems(): # p.update(frame)

# 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)
'''
end_time = time.time()  
if(face_rects):
    time_count+= end_time - start_time
    if(time_count > 2):
        print('time')
else:
    time_count = 0

''' if c == 27: break cap.release() cv2.destroyAllWindows()face.png

Kalman filter with Haar face detection

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 class Pedestrian():

def __init__(self, id, frame, track_window):

    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 Kalman 2

# for i, p in frame.iteritems(): # p.update(frame)

# 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)
'''
end_time = time.time()  
if(face_rects):
    time_count+= end_time - start_time
    if(time_count > 2):
        print('time')
else:
    time_count = 0

''' if c == 27: break cap.release() cv2.destroyAllWindows()

click to hide/show revision 4
No.4 Revision

updated 2016-01-05 04:45:58 -0600

berak gravatar image

Kalman filter with Haar face detection

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
class Pedestrian():

Pedestrian():
def __init__(self, id, frame, track_window):
 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

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)

1,(255,255,255),2) # for Kalman 2

2 # for i, p in frame.iteritems(): # p.update(frame)

p.update(frame) # 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)
 '''
 end_time = time.time()
 if(face_rects):
  time_count+= end_time - start_time
 if(time_count > 2):
 print('time')
 else:
 time_count = 0

0 ''' if c == 27: break cap.release() cv2.destroyAllWindows()

cv2.destroyAllWindows()

Kalman filter with Haar face detection

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 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) self.update(frame)

end of for kalman 1

import cv2

import time # time

import numpy as np

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 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) # 1,(255,255,255),2)

for Kalman 2 2

# for i, p in frame.iteritems(): # p.update(frame) p.update(frame)

# 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)
 '''
 end_time = time.time()  
 if(face_rects):
     time_count+= end_time - start_time
     if(time_count > 2):
         print('time')
 else:
     time_count = 0  

0

''' if c == 27: break cap.release() cv2.destroyAllWindows() cv2.destroyAllWindows()

click to hide/show revision 6
No.6 Revision

updated 2016-01-05 05:16:46 -0600

berak gravatar image

Kalman filter with Haar face detection

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

np ### for Kalman 1

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)

self.update(frame) ### end of for kalman 1

import cv2

import 1 #import cv2 #import time # time

import time #import numpy as np

np 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

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)

1,(255,255,255),2) # for Kalman 2

2 # for i, p in frame.iteritems(): # p.update(frame)

p.update(frame) # 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)
 '''
 end_time = time.time()  
 if(face_rects):
     time_count+= end_time - start_time
     if(time_count > 2):
         print('time')
 else:
     time_count = 0

0 ''' if c == 27: break cap.release() cv2.destroyAllWindows()

cv2.destroyAllWindows()

Kalman filter with Haar face detection

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)




### end of for kalman 1


#import cv2
#import time    time_count = 0  # time
#import numpy as np
time_count = 0  # time
 face_cascade =cv2.CascadeClassifier('/home/wattar/opencv/opencv-3.0.0/data/haarcascades/haarcascade_frontalface_alt.xml')
=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 Kalman 2
   # for i, p in frame.iteritems():
    #    p.update(frame)

   #  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)
    '''
    end_time = time.time()  
    if(face_rects):
        time_count+= end_time - start_time
        if(time_count > 2):
            print('time')
    else:
        time_count = 0  

'''
     if c == 27:
     break
cap.release()
cv2.destroyAllWindows()

cap.release() cv2.destroyAllWindows()

Kalman filter with Haar face detection

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()

cap.release() cv2.destroyAllWindows()