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

asked 2020-09-04 12:19:50 -0600

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/...) 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 :

 import the necessary packages
 from __future__ import print_function
 from imutils.video import WebcamVideoStream
 from imutils.video import FPS
 import argparse
 import imutils
 import cv2

# construct the argument parse and parse the arguments
ap = argparse.ArgumentParser()
ap.add_argument("-n", "--num-frames", type=int, default=100,
help="# of frames to loop over for FPS test")
ap.add_argument("-d", "--display", type=int, default=-1,
help="Whether or not frames should be displayed")
args = vars(ap.parse_args())
# grab a pointer to the video stream and initialize the FPS counter
# created a *threaded* video stream, allow the camera sensor to warmup,
# and start the FPS counter
print("[INFO] sampling THREADED frames from webcam...")
recognizer = cv2.face.LBPHFaceRecognizer_create()
recognizer.read('/home/pi/FacialRecognitionProject/trainer/trainer.yml')
font = cv2.FONT_HERSHEY_SIMPLEX
id = 0
# names related to ids: example ==> Marcelo: id=1,  etc
names = ['None', 'Amruta', 'Paula', 'Ilza', 'Z', 'W'] 
vs = WebcamVideoStream(src=0).start()
faceCascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
fps = FPS().start()
# loop over some frames...this time using the threaded stream
while True:
    # grab the frame from the threaded video stream and resize it
# to have a maximum width of 400 pixels
frame = vs.read()   
frame = imutils.resize(frame, width=400)
faces = faceCascade.detectMultiScale(
    frame,     
    scaleFactor=1.2,
    minNeighbors=5,     
    minSize=(20, 20)
)

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

    # Check if confidence is less them 100 ==> "0" is perfect match 
    if (confidence < 100):
        id = names[id]
        confidence = "  {0}%".format(round(100 - confidence))
    else:
        id = "unknown"
        confidence = "  {0}%".format(round(100 - confidence))

    cv2.putText(img, str(id), (x+5,y-5), font, 1, (255,255,255), 2)
    cv2.putText(img, str(confidence), (x+5,y+h-5), font, 1, (255,255,0), 1)  

# check to see if the frame should be displayed to our screen
if args["display"] > 0:
    cv2.imshow("Frame", frame)
    key = cv2.waitKey(1) & 0xFF
# update the FPS counter
fps.update()
k = cv2.waitKey(30) & 0xff
if k == 27: # press 'ESC' to quit
    break
# stop the timer and display FPS information
fps.stop()
print("[INFO] elasped time: {:.2f}".format(fps.elapsed()))
print("[INFO] approx. FPS: {:.2f}".format(fps.fps()))
# do a bit of cleanup
cv2.destroyAllWindows()
vs.stop()
edit retag flag offensive close merge delete