Site icon Machine Learning Projects

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

Machine Learning Projects

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()

Final results for the face and eye detection in cv2…

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.

Exit mobile version