Try face recognition with Python

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.

Face recognition with Haar Cascades

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)

20160901001.jpg

Can also recognize eyes

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.

This code

Recommended Posts

Try face recognition with Python
Try face recognition with python + OpenCV
[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
Try face recognition with Generated Photos
Face recognition with camera with opencv3 + python2.7
Try scraping with Python.
[python, openCV] base64 Face recognition with images
Serverless face recognition API made with Python
Face recognition with Python's OpenCV
Try Python output with Haxe 3.2
Face recognition with Amazon Rekognition
Face detection with Python + dlib
Face detection with Python + OpenCV
Try running Python with Try Jupyter
Face recognition / cutting with OpenCV
Try python
Now, let's try face recognition with Chainer (prediction phase)
Easy face recognition try with Jetson Nano and webcam
Now, let's try face recognition with Chainer (learning phase)
Number recognition in images with Python
Try scraping with Python + Beautiful Soup
Face detection with YOLO Face (Windows10, Python3.6)
Try to operate Facebook with Python
Face detection with Lambda (Python) + Rekognition
Try singular value decomposition with Python
First Anime Face Recognition with Chainer
Cut out face with Python + OpenCV
Try frequency control simulation with Python
I tried face recognition with OpenCV
Face detection with Python + OpenCV (rotation invariant)
FizzBuzz with Python3
Try to reproduce color film with Python
Scraping with Python
Try logging in to qiita with Python
English speech recognition with python [speech to text]
Try mathematical formulas using Σ with python
Scraping with Python
Python with Go
Twilio with Python
Integrate with Python
Easy introduction of speech recognition with Python
Try using Python with Google Cloud Functions
Play with 2016-Python
Face recognition of anime characters with Keras
Python> try: / except:
AES256 with python
Tested with Python
python starts with ()
with syntax (Python)
Quickly try Microsoft's Face API in Python
Try HTML scraping with a Python library
Bingo with python
Try calling Python from Ruby with thrift
Zundokokiyoshi with python
Try drawing a map with python + cartopy 0.18.0
[Continued] Try PLC register access with Python
Try assigning or switching with Python: lambda
Excel with Python
[For beginners] Try web scraping with Python
Microcomputer with Python
Cast with python