Movatterモバイル変換


[0]ホーム

URL:


Open In App
Next Article:
Text Detection and Extraction using OpenCV and OCR
Next article icon

Prerequisites:Face detection usingdlib andopenCV

In this article, we will use image processing to detect and count the number of faces. We are not supposed to get all the features of the face. Instead, the objective is to obtain the bounding box through some methods i.e. coordinates of the face in the image, depending on different areas covered by the number of the coordinates, number faces that will be computed.

Required libraries:

  • OpenCV library in python is a computer vision library, mostly used for image processing, video processing, and analysis, facial recognition and detection, etc.
  • Dlib library in python contains the pre-trained facial landmark detector, that is used to detect the (x, y) coordinates that map to facial structures on the face.
  • Numpy is a general-purpose array-processing package. It provides a high-performance multidimensional array object and tools for working with these arrays.

Below is the step-wise approach to Count the Number of faces:

Step 1:Import required libraries. 

Python3
# Import librariesimportcv2importnumpyasnpimportdlib

Step 2: Open the default camera to capture faces and use the dlib library to get coordinates.

Python3
# (0) in VideoCapture is used to# connect to your computer's default cameracap=cv2.VideoCapture(0)# Get the coordinatesdetector=dlib.get_frontal_face_detector()

Step 3:Count the number of faces.

  • Capture the frames continuously.
  • Convert the frames to grayscale(not necessary).
  • Take an iterator i and initialize it to zero.
  • Each time you get the coordinates to the face structure in the frame, increment the iterator by 1.
  • Plot the box around each detected face along with its face count.
Python3
whileTrue:# Capture frame-by-frameret,frame=cap.read()frame=cv2.flip(frame,1)# Our operations on the frame come heregray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)faces=detector(gray)# Counter to count number of facesi=0forfaceinfaces:x,y=face.left(),face.top()x1,y1=face.right(),face.bottom()cv2.rectangle(frame,(x,y),(x1,y1),(0,255,0),2)# Increment the iterartor each time you get the coordinatesi=i+1# Adding face number to the box detecting facescv2.putText(frame,'face num'+str(i),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)print(face,i)# Display the resulting framecv2.imshow('frame',frame)

Step 4:Terminate the loop.

Python3
# Enter key 'q' to break the loopifcv2.waitKey(1)&0xFF==ord('q'):break

Step 5:Clear windows.

Python3
# When everything done, release# the capture and destroy the windowscap.release()cv2.destroyAllWindows()

Below is the complete program of the above approach:

Python3
# Import required librariesimportcv2importnumpyasnpimportdlib# Connects to your computer's default cameracap=cv2.VideoCapture(0)# Detect the coordinatesdetector=dlib.get_frontal_face_detector()# Capture frames continuouslywhileTrue:# Capture frame-by-frameret,frame=cap.read()frame=cv2.flip(frame,1)# RGB to grayscalegray=cv2.cvtColor(frame,cv2.COLOR_BGR2GRAY)faces=detector(gray)# Iterator to count facesi=0forfaceinfaces:# Get the coordinates of facesx,y=face.left(),face.top()x1,y1=face.right(),face.bottom()cv2.rectangle(frame,(x,y),(x1,y1),(0,255,0),2)# Increment iterator for each face in facesi=i+1# Display the box and facescv2.putText(frame,'face num'+str(i),(x-10,y-10),cv2.FONT_HERSHEY_SIMPLEX,0.7,(0,0,255),2)print(face,i)# Display the resulting framecv2.imshow('frame',frame)# This command let's us quit with the "q" button on a keyboard.ifcv2.waitKey(1)&0xFF==ord('q'):break# Release the capture and destroy the windowscap.release()cv2.destroyAllWindows()

Output:


Count number of Faces using Python – OpenCV
Improve
Article Tags :
Practice Tags :

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