Detecting And Tracking Face in Python Using Haarcascade Framework

 Detecting And Tracking Face in Python Using Haarcascade Classifier


In this blog we are going to learn how to detect and track human face using haarcascade framework
 
Before we start make sure you save the Haarcascade File in Xml format . Make sure you save the file in the same location where your python file or jupyter notebook.

import cv2
import numpy as np

#first we are going to call our face detection classifier the same file we save in xml format it contains the code to detect face

face =cv2.CascadeClassifier('haarcascade_frontalface_alt.xml')

#take input from our webcam
cap = cv2.VideoCapture(0)
sf = 0.5 # scaling factor lower the value lower the resolution of the video
while True:
    ret, frame = cap.read()
    frame = cv2.resize(frame, None, fx=sf, fy=sf,interpolation=cv2.INTER_AREA)
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    face_rects = face.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)
    cv2.imshow('Face Detector', frame)
    c = cv2.waitKey(1)
    if c == 27:
        break
cap.release()
cv2.destroyAllWindows()








Post a Comment

0 Comments