[PYTHON] Try face recognition with Generated Photos

This article is a relay article of "2021 New Year Advent Calendar TechConnect" of Link Information Systems. Relayed by a group member of engineer.hanzomon. (For Facebook of the link information system, click here](https://ja-jp.facebook.com/lis.co.jp/))

2021 New Year Advent Calendar Tech Connect Index here

I'm shinmo, who is in charge of the article on the 15th. Thank you.


happy New Year. Personally, I recently replaced my smartphone with an iPhone 12. This iPhone 12 has a face identification function called Face ID, which can be unlocked.

That's why I would like to study face recognition (although it's just a touch) this year. (Last year was Character Recognition)

Did you know that around 2019, there was news that there was a site that created faces for people without AI? There was a similar site before that, but the site introduced at that time Generated Photos Has been talked about because it's pretty well done and doesn't have the unnaturalness that other sites used to have. (Actual site screen) genP_.png

You don't have to throw your face into the sea on the net, and there is no infringement of portrait rights. Isn't this just right for trying face recognition ...?

So I'm going to try face recognition with Generated Photos.

Preparation

The environment and library used this time are as follows. Python3.7.4 openCV

Install openCV with the following command.

command prompt


python -m pip install -U opencv-python

Next, prepare a classifier. To put it simply, a classifier is a filter that sorts out whether it is a face or not. There are some defaults in opencv below, so use that. https://github.com/opencv/opencv/tree/master/data/haarcascades This time we will use haarcascade_frontalface_alt_tree.xml.

Execute

This is the image to use. (Quote: https://generated.photos/) generate_2.jpg

After that, implement it as follows and try to actually move it.

faceRcg.py


import cv2

cascade_path = "./cascade/haarcascade_frontalface_alt_tree.xml"

#Files used and I / O directories
image_file = "generate_1.jpg "
image_path = "./in/" + image_file
output_path = "./out/" + image_file

#File reading
image = cv2.imread(image_path)

#Grayscale conversion
image_gray = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

#Acquisition of classifier
cascade = cv2.CascadeClassifier(cascade_path)

#Detection execution
facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=10, minSize=(30, 30))

#Border color specification: red
color = (0, 0, 255)

#When detected
if len(facerect) > 0:

    #Creating a border around the detected face
    for rect in facerect:
        cv2.rectangle(image, tuple(rect[0:2]),tuple(rect[0:2]+rect[2:4]), color, thickness=2)

#Output of recognition result
cv2.imwrite(output_path, image)

Result is… generate_2.jpg

It seems to have worked. By the way, it seems that you can also specify the orientation of the face in Generated Photos, so let's change it a little. input: (Quote: https://generated.photos/) generate_1.jpg

output: generate_1.jpg

... I want to see it. Is it because it doesn't face the front very much? However, even with the iPhone 12, if it is too slanted, the lock will not be released, so it may be difficult to judge the slanted face.

At the end

Last time and this time, I thought it would be difficult to make it, but when I looked it up, it was unexpectedly easy to implement. I'm surprised. The next time you do it, it would be interesting to be able to work on creating a model for machine learning.

Also, Generated Photos was so amazing that I played for about an hour.

Thank you for reading this far.

Recommended Posts

Try face recognition with Generated Photos
Try face recognition with Python
Try face recognition with python + OpenCV
Face recognition with Edison
[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
Face recognition with Python's OpenCV
Face recognition with Amazon Rekognition
Face recognition / cutting with OpenCV
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)
First Anime Face Recognition with Chainer
Face recognition with camera with opencv3 + python2.7
I tried face recognition with OpenCV
Face recognition of anime characters with Keras
[python, openCV] base64 Face recognition with images
Serverless face recognition API made with Python
Try montage your face with an AB test
Try scraping with Python.
Image recognition with keras
Try SNN with BindsNET
Try regression with TensorFlow
Real-time face recognition with video acquired by getUserMedia [HTML5, openCV]
Try to factorial with recursion
Try function optimization with Optuna
Try deep learning with TensorFlow
Try edge detection with OpenCV
Try Google Mock with C
Try using matplotlib with PyCharm
Try programming with a shell!
Try GUI programming with Hy
Try an autoencoder with Pytorch
Try Python output with Haxe 3.2
Try matrix operation with NumPy
I tried face recognition using Face ++
Try implementing XOR with PyTorch
Try running CNN with ChainerRL
Try various things with PhantomJS
Face detection with Python + dlib
Restore disjointed photos with optimization!
Try Deep Learning with FPGA
Face detection with Python + OpenCV
Try running Python with Try Jupyter
Try implementing perfume with Go
Object co-localization for face recognition
Try Selenium Grid with Docker
Face detection with Haar Cascades
Try OpenCV with Google Colaboratory
Try machine learning with Kaggle
Try TensorFlow MNIST with RNN
Try building JupyterHub with Docker
Try using folium with anaconda
Image recognition with Keras + OpenCV
Anime face detection with OpenCV
Replace your face with Twitter icon with openCV face recognition and do ZOOM