Face and Eye detection in cv2 using Haarcascades – with source code – easiest way – 2024

So in today’s very interesting blog, we will see how we can perform face and eye detection in cv2 using Haarcascades. This is going to be a very easy and fun project. Remember it’s only detection and not recognition.

This is going to be a very interesting blog, so without any further due, Let’s do it…

Checkout the video here – https://youtu.be/x6g-UvkwAzE

Code for Face and eye detection in cv2…

import cv2

face_detector = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
eye_detector = cv2.CascadeClassifier('haarcascade_eye.xml')

cam = cv2.VideoCapture(0)

ret = True

while ret:
    ret, frame = cam.read()

    if ret == True:
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        face_points = face_detector.detectMultiScale(gray, 1.3, 5)
        for (x, y, w, h) in face_points:
            cv2.rectangle(frame, (x, y), (x+w, y+h), (255, 0, 20), 2)
        
            face = frame[y:y+h,x:x+w]
            eyes = eye_detector.detectMultiScale(face,1.3,5)
            for (x, y, w, h) in eyes:
                cv2.rectangle(face, (x, y), (x+w, y+h), (155, 0, 120), 2)

        cv2.imshow('Live feed', frame)

    if cv2.waitKey(1) == 27:
        break

cam.release()
cv2.destroyAllWindows()
  • Line 1 – Importing required library cv2, for face and eye detection in cv2.
  • Line 3 – In this line, we are creating a Haarcascade object to detect faces in the frame.
  • Line 4 – In this line, we are creating a Haarcascade object to detect eyes in the frame.
  • Line 6 – Creating a VideoCapture object to access the webcam. Argument 0 is passed when we want to use the inbuilt webcam of PC/Laptop, use 1 if you want to use any external camera.
  • Line 8 – Let’s set ret=True (just a formality to start the infinite loop).
  • Let’s start the loop…
  • Line 11 – We are using cam.read() to read the current frame from the webcam.
  • Line 13 – This if statement says that if we are getting frames from the webcam without any error, then proceed further, because in that case, ret would be True.
  • Line 14 – Convert the image from BGR to grayscale because Haar Cascades detect faces in grayscale images efficiently.
  • Line 15 – Let’s detect the faces using detectMultiscale, Now we have our face coordinates as (x,y,w,h) where (x,y) are the coordinates of the top-left of the rectangle around the face, w is the width and h is the height of the rectangle.
  • Line 16-17 – Let’s traverse through the faces and draw rectangles around them.
  • Line 19 – Let’s just extract the face.
  • Line 20 – Detect eyes just in the same way as we did above for faces.
  • Line 21-22 – Let’s traverse through the eyes and draw rectangles around them.
  • Line 24 – Showing results.
  • Line 26-27 – If someone hits the ESC key, break the code.
  • Line 29-30 – Realease the cam object to close the webcam and destroy all open windows.

Final results for the face and eye detection in cv2…

face and eye detection Machine learning projects with source code in Python

Do let me know if there’s any query regarding face and eye detection in cv2 by contacting me via email or LinkedIn.

So this is all for this blog folks, thanks for reading it and I hope you are taking something with you after reading this and till the next time ?…

Download the source code…

Read my previous post: HOW TO PERFORM 5 MOST FAMOUS TYPES OF THRESHOLDING IN PYTHON USING OPENCV

Check out my other machine learning projectsdeep learning projectscomputer vision projectsNLP projectsFlask projects at machinelearningprojects.net.

Leave a Reply

Your email address will not be published. Required fields are marked *