Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Real time object color detection using OpenCV
Next article icon

Face detection is a important task in computer vision and Haar Cascade classifiers play an important role in making this process fast and efficient. Haar Cascades are used for detecting faces and other objects by training a classifier on positive and negative images.

  1. Positive Images: These images contain the objects that the classifier is trained to detect.
  2. Negative Images: These images contain everything else which do not contain the object we want to detect.

In this article, we will learn how to perform face detection using Haar Cascades classifier for detecting faces and eyes using OpenCV.

Face Detection using Cascade Classifier Implementation

We will go through the step-by-step procedure to implement object detection using Haar Cascades.

1. Importing required Libraries

Here, we will use NumpyOpenCV andMatplotlib.

Python
importcv2importnumpyasnpimportmatplotlib.pyplotasplt

2. Loading Haar Cascade Classifiers

Next we will load the pre-trained Haar Cascade classifiers for detecting faces and eyes. You can download these classifier from thislink.

Python
face_cascade=cv2.CascadeClassifier("/content/haarcascade_frontalface_default.xml")eye_cascade=cv2.CascadeClassifier('/content/haarcascade_eye.xml')

3. Creating Function to Detect Faces

Now we’ll create a functionadjusted_detect_face() to detect faces in an image. This function uses the face cascade classifier to identify face rectangles and draws rectangles around the detected faces.

Python
defadjusted_detect_face(img):face_img=img.copy()face_rect=face_cascade.detectMultiScale(face_img,scaleFactor=1.2,minNeighbors=5)for(x,y,w,h)inface_rect:cv2.rectangle(face_img,(x,y),(x+w,y+h),(255,255,255),10)returnface_img

4. Creating Function to Detect Eyes

Similarly we create a functiondetect_eyes() to detect eyes using the eye cascade classifier.

Python
defdetect_eyes(img):eye_img=img.copy()eye_rect=eye_cascade.detectMultiScale(eye_img,scaleFactor=1.2,minNeighbors=5)for(x,y,w,h)ineye_rect:cv2.rectangle(eye_img,(x,y),(x+w,y+h),(255,255,255),10)returneye_img

5. Loading a Image

Now let’s load an image and apply both face and eye detection on it. The image which we are using can be downloaded from thislink.

Python
img=cv2.imread('/content/andrew.jpg')img_copy1=img.copy()img_copy2=img.copy()img_copy3=img.copy()plt.imshow(cv2.cvtColor(img,cv2.COLOR_BGR2RGB))

Output:

Sample-image
sample image

6. Detecting Faces and Eyes

After running the code you will see three imagesFace Detection,Eyes Detection andFace and Eyes Detection. These images will also be saved asface.jpg,eyes.jpg andface+eyes.jpg respectively.

Face Detection:

Python
face=adjusted_detect_face(img_copy1)plt.imshow(cv2.cvtColor(face,cv2.COLOR_BGR2RGB))plt.show()cv2.imwrite('face.jpg',face)

Output:

face
face detection

Eyes Detection:

Python
eyes=detect_eyes(img_copy2)plt.imshow(cv2.cvtColor(eyes,cv2.COLOR_BGR2RGB))plt.show()cv2.imwrite('eyes.jpg',eyes)

Output:

eyes
eyes detection

Face and Eyes Detection:

Python
eyes_face=adjusted_detect_face(img_copy3)plt.imshow(cv2.cvtColor(eyes_face,cv2.COLOR_BGR2RGB))plt.show()cv2.imwrite('face+eyes.jpg',eyes_face)

Output:

faceandeyes
face and eyes detection

In this article we explored Haar Cascades classifier for face detection by using pre-trained XML file. We can create custom XML files tailored to detect specific objects and can be used for wide range of computer vision applications.


How to Detect Faces using Haar-Cascade in OpenCV?
Visit Courseexplore course icon
Improve

Similar Reads

We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood ourCookie Policy &Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences

[8]ページ先頭

©2009-2025 Movatter.jp