Face recognition / cutting with OpenCV

Introduction

I modified the code that recognizes the face in the image and encloses it in a square frame, and created a code that cuts out as a file.

Library import

Import libraries such as OpenCV and Pillow.

import os
import subprocess
from PIL import Image
import cv2 as cv

dir0 = 'org' 
dir1 = 'png'
dir2 = 'png_resize'
dir3 = 'face'

Image preparation

Place the original image (jpg, size 500x334px) in the org folder. Convert this to a png file, adjust the size width to 600px, and store it in the png_resize folder.

files0 = os.listdir(dir0)
files0.sort()

for file in files0:

    if '.jpg'  in file:        
        command = 'sips --setProperty format png ' + dir0 +'/' + file +  ' --out ' + dir1 +'/' +  file.replace('.jpg','.png') 
        subprocess.call(command, shell=True)
        print(file) 

files1 = os.listdir(dir1)
files1.sort()

# aaa.jpg  

for file in files1:   
    if '.png' in file:   
        img0 = os.path.join(dir1, file)
        img0_img = Image.open(img0)
        h = img0_img.height
        w = img0_img.width
        img1_img = img0_img.resize((600,round(600*h/w)))
        img1 = os.path.join(dir2, file) 
        img1_img.save(img1)
        print(file) 

# aaa.png

files2 = os.listdir(dir2)
files2.sort()    

aaa.png

Face recognizer installation

Face classifiers are available at here.

face_cascade = cv.CascadeClassifier('haarcascade_frontalface_default.xml')
#face_cascade = cv.CascadeClassifier('haarcascade_profileface.xml')

Face recognizer execution, face image cutout

Cut out the face image. The cutout range is 10px wider up, down, left and right than the detection range.

for file in files2:
    if '.png' in file:
        dirfile = os.path.join(dir2, file) 
        img = cv.imread(dirfile)
        gray = cv.cvtColor(img, cv.COLOR_BGR2GRAY)
        
        faces = face_cascade.detectMultiScale(gray, 1.3, 5)
        
        for (x,y,w,h) in faces:
            face = img[y-10:y+h+10, x-10:x+w+10]
            face_name = str(file.strip('.png'))+'_'+str(x)+'_'+str(y)+'.png'
            dirface = os.path.join(dir3,face_name)
            facefile = cv.imwrite(dirface, face) 
            #cv.rectangle(img,(x-10,y-10),(x+w+10,y+h+10),(255,0,0),2)
            print(face_name)  

# aaa_152_22.png
# aaa_11_70.png
# aaa_438_41.png
# aaa_79_106.png
# aaa_385_140.png
# aaa_190_175.png
# aaa_269_171.png
# aaa_76_206.png
# aaa_527_257.png
# aaa_91_277.png
# aaa_254_330.png
# aaa_446_348.png  

A total of 12 face images were cut out from the above images. If you can see most of the face, you know that the cutout was successful.

aaa_91_277.pngaaa_79_106.pngaaa_76_206.pngaaa_527_257.pngaaa_446_348.pngaaa_438_41.png aaa_385_140.pngaaa_269_171.pngaaa_254_330.pngaaa_190_175.pngaaa_152_22.pngaaa_11_70.png

At the end

It seems that it can be used when making a list with a photo from a group photo. It would be convenient for anyone to use the trained classifier if it was published as a cascade file.

reference

https://docs.opencv.org/4.1.0/d7/d8b/tutorial_py_face_detection.html https://github.com/opencv/opencv/tree/master/data/haarcascades

Recommended Posts

Face recognition / cutting with OpenCV
Face recognition with Python's OpenCV
Try face recognition with python + OpenCV
Face recognition with camera with opencv3 + python2.7
I tried face recognition with OpenCV
Face recognition with Edison
[python, openCV] base64 Face recognition with images
Face recognition with Amazon Rekognition
Try face recognition with Python
Image recognition with Keras + OpenCV
Anime face detection with OpenCV
[OpenCV] Personal identification with face photo
First Anime Face Recognition with Chainer
Cut out face with Python + OpenCV
Object recognition with openCV by traincascade
Real-time face recognition with video acquired by getUserMedia [HTML5, openCV]
Serverless face recognition API made with Python
Replace your face with Twitter icon with openCV face recognition and do ZOOM
Hello World and face detection with OpenCV 4.3 + Python
Performance comparison of face detector with Python + OpenCV
[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
Resize, mosaic, face detection with OpenCV, sometimes Zojirushi
Detect stoop with OpenCV
Binarization with OpenCV / Python
Image recognition with keras
Data Augmentation with openCV
Easy TopView with OpenCV
Stumble with homebrew opencv3
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)
[Super easy] Simultaneous face recognition and facial expression recognition in real time with Python and OpenCV!
Improve detection accuracy quickly by specifying parameters with openCV face detection
"Apple processing" with OpenCV3 + Python3
Try edge detection with OpenCV
Image editing with python OpenCV
Camera capture with Python + OpenCV
I tried face recognition using Face ++
[Python] Using OpenCV with Python (Basic)
A memo when face is detected with Python + OpenCV quickly
Binarize photo data with OpenCV
Loop video loading with opencv
I tried face recognition from the video (OpenCV: python version)
Object co-localization for face recognition
Get image features with OpenCV
Face detection with Haar Cascades
Try OpenCV with Google Colaboratory
Cascade classifier creation with opencv
Using OpenCV with Python @Mac
I want to check the position of my face with OpenCV!
Shining life with Python and OpenCV
Real-time image processing basics with opencv
Face detection with YOLO Face (Windows10, Python3.6)
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
I tried trimming efficiently with OpenCV
[Python] Using OpenCV with Python (Image transformation)
Face detection with Lambda (Python) + Rekognition
[Python] Using OpenCV with Python (Edge Detection)
Erase certain colors with OpenCV + PySimpleGUI
Introducing OpenCV on Mac with homebrew