Skip to content

An OpenCV project which gets frames from your webcam and outputs as a video in real time with faces detected.

License

Notifications You must be signed in to change notification settings

tekdogan/real-time-face-detection

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

11 Commits
 
 
 
 
 
 
 
 

Repository files navigation

real-time-face-detection

This is an OpenCV project which gets frames from your webcam and outputs as a video in real time with faces detected. We will be using Haar Feature-based Cascade Classifiers for face detection. You can learn more about it by clicking here.

Tutorial

Firstly we have to import the necessary libraries.

import cv2, sys

And then denote the path to the library which is located in our folder.

cascPath = "haarcascade_frontalface_default.xml"
faceCascade = cv2.CascadeClassifier(cascPath)

Capturing frames from webcam and assigning it to a variable for processing is done by cv2.VideoCapture() function. (parameter 0 for default webcam and 1,2... for USB devices)

cap = VideoCapture(0)

Now we have to use the detectMultiScale function to detect face(s). Let's take a look at this function's parameters:

  • image: Matrix of the type CV_8U containing an image where objects are detected. Our type is gray.
  • scaleFactor: Parameter specifying how much the image size is reduced at each image scale. In this case, we use 1.2.
  • minNeighbors: Parameter specifying how many neighbors each candidate rectangle should have to retain it. We will be using 5 neighbors.
  • minSize: Minimum possible object size. Objects smaller than that are ignored. In this projects, we will be having size of (30,30).

And now the functions looks like:

    faces = faceCascade.detectMultiScale(
        gray,
        scaleFactor=1.2,
        minNeighbors=5,
        minSize=(30, 30),
        flags=cv2.cv.CV_HAAR_SCALE_IMAGE
    )

For every face detected by detectMultiScale function, we draw a red rectangle around it.

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

And yield the output as a frame.

    cv2.imshow('Face Detection Program', frame)

Press esc key to close the window and shut down the program.

   if cv2.waitKey(1) & 0xFF == 27:
        break

cap.release()
cv2.destroyAllWindows()

About

An OpenCV project which gets frames from your webcam and outputs as a video in real time with faces detected.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages