How can I use Haar cascade for face detection and kalman filter for tracking?

Chirag Atha chiragatha07 at gmail.com
Fri Sep 4 14:22:05 EDT 2020


I am new to computer vision and I am trying to use haar cascade for face detection and kalman filtering for tracking I intend to run the code on raspberry pi 3B. Hence cannot use any deep learning methods for tracking. How can I use cv2.kalmanfilter() (https://docs.opencv.org/trunk/dd/d6a/classcv_1_1KalmanFilter.html) in my code to do the tracking and draw a line for the traversal path? It would be great help if any one can guide me for it 
My code is :

from __future__ import print_function
import numpy as np
import cv2
from imutils.video import WebcamVideoStream
from imutils.video import FPS
import argparse
import imutils
import cv2
 
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
#faceCascade = cv2.CascadeClassifier('haarcascade_profileface.xml')
fps = FPS().start()
cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height

while True:
    ret, img = cap.read()
    #img = cv2.flip(img, -1)
    gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    faces = faceCascade.detectMultiScale(
        gray,     
        scaleFactor=1.2,
        minNeighbors=5,     
        minSize=(20, 20)
    )

    for (x,y,w,h) in faces:
        cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
        roi_gray = gray[y:y+h, x:x+w]
        roi_color = img[y:y+h, x:x+w]  

    cv2.imshow('video',img)
    fps.update()
    k = cv2.waitKey(30) & 0xff
    if k == 27: # press 'ESC' to quit
        break
fps.stop()
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
cap.release()
cv2.destroyAllWindows()


More information about the Python-list mailing list