Face recognition with Python's OpenCV

Overview

This article is for people who want to recognize faces with Python's OpenCV. In this article, I will explain how to detect faces from images, camera images, mp4 files, and how to cut and save only faces from images.

environment

macOS Catalina 10.15.4 Python 3.7.5 opencv-python 4.2.0.34 numpy 1.18.2

OpenCV installation

$ pip install opencv-python

Directory structure

.
├── cascades
│  └── haarcascade_frontalface_default.xml
├── image_detect.py
├── images
│  └── 001.jpg
├── trimmed
└── venv

The directory structure is like this. The face detector in the cascades folder is located in lib / python3.7 / site-packages / cv2 / data in the directory where Python is installed or in the virtual environment directory.

Detect a face and enclose it in a rectangle

Detected from image

`` image_detect.py`


import cv2

cascade_path =  "./cascades/haarcascade_frontalface_default.xml"
img_path = "./images/001.jpg "
color = (255, 255, 255) #The color of the rectangle that surrounds the detected face

src = cv2.imread(img_path,0)
gray = cv2.cvtColor(src,cv2.cv2.COLOR_BAYER_BG2GRAY)
cascade = cv2.CascadeClassifier(cascade_path)
rect = cascade.detectMultiScale(gray)
if len(rect) > 0:
    for x, y, w, h in rect:
        cv2.rectangle(src, (x, y), (x+w, y+h), color)

cv2.imshow('detected', src)
cv2.waitKey(0)
cv2.destroyAllWindows()


x, y, w, h correspond to the x coordinate of the upper left of the face, the y coordinate of the upper left of the face, the width of the face, and the height of the face. Also, although not limited to OpenCV, the origin of the xy coordinates is the upper left of the image.

Detected from camera image

`` image_detect.py`


import cv2

cascade_path = "./cascades/haarcascade_frontalface_default.xml"
cascade = cv2.CascadeClassifier(cascade_path)
color = (255, 255, 255) #The color of the rectangle that surrounds the detected face
cap = cv2.VideoCapture(0)

while True:
    ret, frame = cap.read()
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    rect = cascade.detectMultiScale(gray, scaleFactor=1.1, minNeighbors=2, minSize=(30, 30))
    if len(rect) > 0:
        for x, y, w, h in rect:
            cv2.rectangle(frame, (x, y), (x+w, y+h), color)
    cv2.imshow('detected', frame)
    key = cv2.waitKey(1)
    if key == ord('q'):
        break
cap.release()
cv2.destroyAllWindows()

You can get the camera image by setting the argument of cv2.VideoCapture () to the device number of the camera, but you can also handle the video file by entering the path to the mp4 file.

Detect, crop and save faces

When there is only one image

`` image_detect.py`


import cv2

cascade_path =  "./cascades/haarcascade_frontalface_default.xml"
img_path = "./images/001.jpg "
out_path = "./trimmed/"


src = cv2.imread(img_path,0)
gray = cv2.cvtColor(src,cv2.cv2.COLOR_BAYER_BG2GRAY)
cascade = cv2.CascadeClassifier(cascade_path)
rect = cascade.detectMultiScale(gray)
if len(rect) > 0:
    for i,[x, y, w, h] in enumerate(rect):
        img_trimmed = src[y:y + h, x:x + w]
        file_name = "{}.jpg ".format(i)
        file_path = out_path + file_name
        cv2.imwrite(file_path, img_trimmed)

You can get the loop count and the contents of rect at the same time by using enumerate in the for statement. When trimming, take out with slices.

When you want to detect from multiple images at once

`` image_detect.py`


import cv2
import os

cascade_path =  "./cascades/haarcascade_frontalface_default.xml"
img_path = "./images/"
out_path = "./trimmed/"

files = os.listdir(img_path)
cascade = cv2.CascadeClassifier(cascade_path)

for file in files:
    src = cv2.imread(img_path+file,0)
    gray = cv2.cvtColor(src,cv2.cv2.COLOR_BAYER_BG2GRAY)
    rect = cascade.detectMultiScale(gray)
    if len(rect) > 0:
        for i,[x, y, w, h] in enumerate(rect):
            img_trimmed = src[y:y + h, x:x + w]
            file_name = "{}_{}".format(i,file)
            file_path = out_path + file_name
            cv2.imwrite(file_path, img_trimmed)

If you put the image you want to crop in the images folder and execute it, it will be saved in the trimmed folder in the format of "{face index number} _ {original file name}".

Summary

OpenCV is convenient

Recommended Posts

Face recognition with Python's OpenCV
Face recognition / cutting with 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
Face detection with Python + OpenCV
Try face recognition with Python
Image recognition with Keras + OpenCV
Anime face detection with OpenCV
Try face recognition with Generated Photos
[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]
Face recognition using OpenCV (Haar-like feature classifier)
Face detection with Python + OpenCV (rotation invariant)
Try using the camera with Python's OpenCV
Face recognition of anime characters with Keras
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
Rotate sprites with OpenCV
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
Implement subcommands with Python's argparse
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
Face detection with Python + dlib
Binarize photo data with OpenCV
Loop video loading with opencv
I tried face recognition from the video (OpenCV: python version)
Real-time edge detection with OpenCV
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!
Number recognition in images with Python