When I was reading the document diagonally while investigating image processing with Python + OpenCV, there was Tutorial on face recognition. It's a technology that seems to help me who can't remember people's faces these days.
I'm not sure about machine learning around here in detail, but to put it simply, I train to be able to distinguish faces from Haar features using non-face images and face images. As a result, identification data is generated, but as a method to efficiently use the identification data for identification, a Cascade classifier has been devised and seems to be included in the OpenCV distribution.
For Mac Anaconda, it's in "anaconda3 / share / OpenCV / haarcascades", and for Windows version Anaconda, it's in "Anaconda3 \ Library \ etc \ haarcascades".
--haarcascade_eye.xml ... Eye recognition --haarcascade_frontalface_default.xml ... Face recognition --haarcascade_smile.xml ... Smile recognition
For the time being, I made a copy of this. So, make it usable as a classifier.
face_cascade = cv2.CascadeClassifier(‘haarcascades/haarcascade_frontalface_default.xml’)
Next, read the image file. cv2.imread () reads in grayscale mode when 0 is specified as the second argument, but without using that function, after reading in color as img, use cv2.cvtColor () Have a copy converted to grayscale. This is because face recognition is performed using a grayscale image and a color image is displayed.
img = cv2.imread(file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
Find the face in the image and return the coordinates of the position of the face. Only one line, as you only use the classifier based on the already trained identification data.
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
The face is searched for in the grayscale image (gray), but a rectangle surrounding the face is drawn on the original color image (img) based on the position of the face coordinates.
for (x, y, w, h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
x and y indicate the x and y coordinates of the upper left of the rectangle, respectively, and w and h indicate the width and height respectively, but when drawing a rectangle in the image (img) with OpenCV, the upper left Since the x, y coordinates of and the x, y coordinates of the lower right are required respectively, they are specified as (x, y), (x + w, y + h).
plt.imshow( cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
The image with the rectangle written at the end is displayed, but for some reason the color image read by imread () of OpenCV holds the data in the order of BGR, but this time, in the pyplot of matplotlib used for image display Since it is assumed that the data is held in the order of RGB, when passing the data to plt.imshow (), it is converted by cv2.cvtColor (img, cv2.COLOR_BGR2RGB).
By the way, it's OK to have multiple faces, so I receive it like for (x, y, w, h) in faces :, but the copyright and portrait rights are just right. I don't have something like that, so I can't try it.
import os
import cv2
import numpy as np
from matplotlib import pyplot as plt
%matplotlib inline
def facedetect(file):
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
img = cv2.imread(file)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x, y, w, h) in faces:
cv2.rectangle(img, (x,y), (x+w, y+h), (255,0,0), 2)
plt.imshow( cv2.cvtColor(img, cv2.COLOR_BGR2RGB))
plt.show()
if __name__ == '__main__':
lenna = "4.2.04.tiff"
if os.path.exists(lenna):
facedetect(lenna)
Tutorial also contains a sample of eye recognition after face recognition. If there is a face, there are eyes, and there are no eyes in places other than the face, so it is devised to run eye recognition in the rectangle that recognized the face.
faces = face_cascade.detectMultiScale(gray, 1.3, 5)
for (x,y,w,h) in faces:
cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
roi_gray = gray[y:y+h, x:x+w]
roi_color = img[y:y+h, x:x+w]
eyes = eye_cascade.detectMultiScale(roi_gray)
for (ex,ey,ew,eh) in eyes:
cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)
The problem is that I don't know anything about "Cascade classifiers based on Haar features" and I can't understand the explanation of the arguments of the detectMultiScale () method at all. That said, it's easy and great.
Now, if you want to identify something from an image as well, you have to make this Cascade classifier properly, but that's another story.
Recommended Posts