Python+OpenCV face detection - CascadeClassifier cascade classifier implementation
1. CascadeClassifier—cascade classifier
Cascade classifier: CascadeClassifier is a class of the cascade classifier used for target detection in the objdetect module under opencv. It can help us detect objects such as license plates, eyes, and faces. Its general principle is to judge whether an object belongs to a certain category. Taking the human face as an example, we can define attributes such as eyes, nose, eyebrows, and mouth as a classifier. If a model is detected that meets all the attributes that define a human face, then it is considered a human face. Next we will implement it step by step.
1. Import the classifier file
For the face recognition function we implemented this time, we need to import a classifier file with the suffix .xml. It is a classifier that has been created by the predecessors. We can directly use the
code as follows
faceCascade = cv2.CascadeClassifier("XML/haarcascade_frontalface_default.xml")
2. Read in the picture
img1 = cv2.imread('Photos/MHT.png')
3. Convert to grayscale image
imgGray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
4. Call the detectMultiScale() function for detection
face1 = faceCascade.detectMultiScale(imgGray1, 1.1, 4)
detectMultiScale(image, scaleFactor, minNeighbors);
My understanding of the parameters is:
image: the image to be processed
scaleFactor: the minimum size of the detection frame
minNeighbors: equivalent to the detection threshold, if it is too small, there will be false detection, that is, some other elements will be misjudged as a human face, if it is too large, the target may not be detected
The output of the function is the x, y coordinate values, width, and height of each face in the detected target image.
5. Draw a rectangular frame to mark the face
for (x, y, w, h) in face1:
cv2.rectangle(img1, (x, y), (x+w, y+h), (255, 0, 0), 2)
6. Output image
cv2.imshow("Result1", img1)
2. Operation effect
Let's take a photo first and
increase the number of people to three people:
four people:
try a little more people:
you can see that most of the faces have been detected. In the case of a large number of people, one of them was not detected, but the requirements can be basically completed in simple scenarios. The complete code is attached below:
import cv2
faceCascade = cv2.CascadeClassifier("XML/haarcascade_frontalface_default.xml")
img1 = cv2.imread('Photos/MHT.png')
img2 = cv2.imread('Photos/Three.png')
img3 = cv2.imread('Photos/BLACKPINK.jpg')
img4 = cv2.imread('Photos/101.png')
imgGray1 = cv2.cvtColor(img1, cv2.COLOR_BGR2GRAY)
imgGray2 = cv2.cvtColor(img2, cv2.COLOR_BGR2GRAY)
imgGray3 = cv2.cvtColor(img3, cv2.COLOR_BGR2GRAY)
imgGray4 = cv2.cvtColor(img4, cv2.COLOR_BGR2GRAY)
face1 = faceCascade.detectMultiScale(imgGray1, 1.1, 4)
face2 = faceCascade.detectMultiScale(imgGray2, 1.2, 4)
face3 = faceCascade.detectMultiScale(imgGray3, 1.3, 5)
face4 = faceCascade.detectMultiScale(imgGray4, 1.2, 8)
# print(face1)
# print('***************************************************')
# print(face2)
# print('***************************************************')
# print(face3)
# print('***************************************************')
# print(face4)
for (x, y, w, h) in face1:
cv2.rectangle(img1, (x, y), (x+w, y+h), (255, 0, 0), 2)
for (x, y, w, h) in face2:
cv2.rectangle(img2, (x, y), (x+w, y+h), (0, 255, 0), 2)
for (x, y, w, h) in face3:
cv2.rectangle(img3, (x, y), (x+w, y+h), (0, 0, 255), 2)
for (x, y, w, h) in face4:
cv2.rectangle(img4, (x, y), (x+w, y+h), (245, 148, 15), 2)
cv2.imshow("Result1", img1)
cv2.imshow("Result2", img2)
cv2.imshow("Result3", img3)
cv2.imshow("Result4", img4)
cv2.waitKey(0)