current position:Home>Only 15 lines of code is needed for face detection! (using Python and openCV)

Only 15 lines of code is needed for face detection! (using Python and openCV)

2022-01-29 19:12:54 Haiyong

This article has participated in 「 Digging force Star Program 」, Win a creative gift bag , Challenge creation incentive fund .

Whether you've recently started exploring OpenCV Or have you been using it for a long time , In any case , You must have met “ Face detection ” The word . As machines become more and more intelligent , Their ability to imitate human behavior seems to be increasing , Face detection is one of the advances of artificial intelligence .

So today , We'll quickly learn what the next test is , Why is it useful , And how to use only 15 Line of code can actually implement face detection on your system !

Let's start by understanding face detection .

What is face detection ?

Face detection is a computer technology based on artificial intelligence , Be able to recognize and locate the presence of human faces in digital photos and videos . In short , The ability of a machine to detect faces in an image or video .

Due to the great progress of artificial intelligence , Now you can detect faces in images or videos , Regardless of light conditions 、 Skin colour 、 How about head posture and background .

Face detection is the starting point for several face related applications , For example, face recognition or face verification . Now , Most cameras in digital devices use face detection technology to detect the position of the face and adjust the focal length accordingly .

So how does face detection work ? I'm glad you asked ! The backbone of any face detection application is an algorithm ( Simple step-by-step instructions for the machine ), It can help determine whether the image is a positive image ( Face image ) Or negative image ( An image without a face ).

In order to do this accurately , The algorithm is trained on a massive data set containing hundreds of thousands of face images and non face images . This trained machine learning algorithm can detect whether there is a face in the image , If a face is detected , A bounding box will also be placed .

Use OpenCV Face detection

Computer vision is one of the most exciting and challenging tasks in artificial intelligence , There are several software packages that can be used to solve problems related to computer vision .OpenCV It is by far the most popular open source library for solving problems based on computer vision .

OpenCV Library downloads exceeded 1800 Ten thousand times , An active user community has 47000 Members of the team . It has a 2500 An optimization algorithm , It includes a complete set of classic and most advanced computer vision and machine learning algorithms , Make it one of the most important libraries in the field of machine learning .

Face detection in images is a simple problem 3 Step process :

First step : Install and import open-cv modular :

pip install opencv-python
 Copy code 
import cv2
import matplotlib.pyplot as plt #  Used to draw images 
 Copy code 

The first 2 Step : take XML The file is loaded into the system

download Haar-cascade Classifier XML File and load it into the system :

Haar-cascade Classifier It's a machine learning algorithm , We train cascading functions with a large number of images . There are different types of cascading classifiers according to different target objects , Here we will use the classifier considering face to recognize it as the target object .

You can Click here to Find a trained classifier for face detection XML file

#  Load cascade 
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
 Copy code 

The first 3 Step : Detect the face and draw a bounding box around it

Use Haar-cascade In the classifier detectMultiScale() Function detects a face and draws a bounding box around it :

#  Read input image 
img = cv2.imread('test.png')

#  Face detection 
faces = face_cascade.detectMultiScale(image = img, scaleFactor = 1.1, minNeighbors = 5)

#  Draw a bounding box around the face 
for (x, y, w, h) in faces:
      cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)

#  Displays the number of faces detected in the image 
print(len(faces),"faces detected!")

#  Draw the image of the detected face 
finalimg = cv2.cvtColor(img, cv2.COLOR_RGB2BGR)
plt.figure(figsize=(12,12))
plt.imshow(finalimg) 
plt.axis("off")
plt.show()
 Copy code 

detectMultiScale() Parameters :

  • image: CV_8U Type of matrix , It contains the image of the detected object .
  • scaleFactor: A parameter that specifies how much the image size is reduced at each image scale .
  • minNeighbors: Parameter specifies how many neighbors should be reserved for each candidate rectangle .

You may need to adjust these values for the best results .

 Insert picture description here

Just like this. , You can implement one of the most unique applications of computer vision . It can be in GitHub Find the detailed code template of the whole face detection implementation .

github.com/wanghao221/…

Be careful : This tutorial is only applicable to face detection in image files , It is not suitable for real-time camera source or video .

It feels great ? You just learned how to implement one of the most interesting applications of artificial intelligence and machine learning . I hope you like my blog . Thank you for reading !

I've been writing a technology blog for a long time , And mainly through the Nuggets , This is my article Python Face detection . I like to share technology and happiness through articles . You can visit my blog : juejin.cn/user/204034… To learn more . I hope you will like !

You are welcome to put forward your opinions and suggestions in the comment area !

Nuggets officials will be in Digging force Star Program After the event , Draw... In the comment area 100 Around the Nuggets , For details of the lucky draw, see the activity article

copyright notice
author[Haiyong],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/01/202201291912529262.html

Random recommended