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)
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.
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.
This is the image to use. (Quote: https://generated.photos/)
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…
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/)
output:
... 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.
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