Home>

I tried to perform face recognition using OpenCV, but I don't know how to recognize the faces of multiple people.

The sample image is a photograph of the faces of three people, but only the leftmost face is recognized.

Corresponding source code
import matplotlib.pyplot as plt
import urllib.request as req
import cv2
url = "destination url of sample image"
req.url retrieve (url, "sample image.png")
img = cv2.imread ("Sample image.png")
img_gray = cv2.cvtColor (img, cv2.COLOR_BGR2RGB)
url2 = "https://raw.githubusercontent.com/opencv/opencv/master/data/haarcascades/haarcascade_frontalface_alt.xml"
req.urlretrieve (url2, "cascade.xml")
cascade_file = "cascade.xml"
cascade = cv2.CascadeClassifier (cascade_file)
face_list = cascade.detectMultiScale (img_gray, minSize = (100, 100))
if len (face_list) == 0:
    print ("failure")
    quit ()
for (x, y, w, h) in face_list:
    print ("Face position =", x, y, w, h)
    red = (0, 0, 255)
    cv2.rectangle (img, (x, y,), (x + w, y + h), red, thickness = 10)
cv2.imwrite ("sample image-face-detect.png", img)
plt.imshow (cv2.cvtColor (img, cv2.COLOR_BGR2RGB))
plt.show ()
What I tried

I wondered if I could change the argument of the detectMultiScale method, but it didn't work.
I thought about how to divide the image, but I thought it was not a good method because it would not be very versatile.
I'm not sure how to recognize the faces of multiple people in one process.

Supplementary information (FW/tool version, etc.)

The execution environment is Google Colaboratory.