[PYTHON] Face recognition using OpenCV (Haar-like feature classifier)

Haar-like feature classifier

"Cascade of boosted classifiers using Haar-like features" is a method called Haar-like that uses a rectangular pattern consisting of light and dark for detection. The Haar-like feature and the target image are overlaid to determine if there is a pattern.  haar.png

The following Haar-like feature classifiers are provided in OpenCV in advance. [OpenCV Install Dir]/Library/etc/haarcascades/

file name Contents
haarcascade_eye.xml Eye
haarcascade_eye_tree_eyeglasses.xml glasses
haarcascade_frontalcatface.xml Cat face (front)
haarcascade_frontalcatface_extended.xml Cat face (front)
haarcascade_frontalface_alt.xml Face (front)
haarcascade_frontalface_alt2.xml Face (front)
haarcascade_frontalface_alt_tree.xml Face (front)
haarcascade_frontalface_default.xml Face (front)
haarcascade_fullbody.xml Whole body
haarcascade_lefteye_2splits.xml left eye
haarcascade_licence_plate_rus_16stages.xml Russian license plate (whole)
haarcascade_lowerbody.xml Lower body
haarcascade_profileface.xml Face (ID photo)
haarcascade_righteye_2splits.xml right eye
haarcascade_russian_plate_number.xml Russian license plate (number)
haarcascade_smile.xml Smile
haarcascade_upperbody.xml Upper body

This time, I will try face recognition using OpenCV's Haar-like feature classifier.

OpenCV OpenCV (Open Source Computer Vision Library) is a collection of BSD-licensed video / image processing libraries. There are many algorithms for image filtering, template matching, object recognition, video analysis, machine learning, and more.

■ Example of motion tracking using OpenCV (OpenCV Google Summer of Code 2015) https://www.youtube.com/watch?v=OUbUFn71S4s

■ Click here for installation and easy usage Install OpenCV 3 (core + contrib) in Python 3 environment & Difference between OpenCV 2 and OpenCV 3 & simple operation check ★ Please install core + opencv_contrib to run the motion template.

■ Click here for still image filtering Try edge detection with OpenCV Perform various filters with OpenCV (gradient, high-pass, Laplacian, Gaussian) Extract feature points with OpenCV (AgastFeature, FAST, GFTT, MSER, AKAZE, BRISK, KAZE, ORB, SimpleBlob)

■ Click here for processing video files Try converting videos in real time with OpenCV Try converting webcam / camcorder video in real time with OpenCV Draw optical flow in real time with OpenCV (Shi-Tomasi method, Lucas-Kanade method) Object tracking using OpenCV (tracking feature points specified by mouse by Lucas-Kanade method Motion template analysis using OpenCV (recognizes objects and their moving directions in real time)

program

The flow is as follows.

  1. Create a Haar-like feature classifier with cv2.CascadeClassifier ([Haar-like feature file.xml])
  2. Specify an image for the Haar-like feature classifier
  3. Enclose the detected area in the image with a rectangle

face.py


import cv2

# Haar-like loading feature classifier
face_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_frontalface_default.xml')
eye_cascade = cv2.CascadeClassifier('haarcascades/haarcascade_eye.xml')

#Loading image file
img = cv2.imread('face.png')

#Grayscale conversion
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

#Face detection
faces = face_cascade.detectMultiScale(gray)
for (x,y,w,h) in faces:
    #Enclose the detected face in a rectangle
    cv2.rectangle(img,(x,y),(x+w,y+h),(255,0,0),2)
    #Face image (grayscale)
    roi_gray = gray[y:y+h, x:x+w]
    #Face g increase (color scale)
    roi_color = img[y:y+h, x:x+w]
    #Detect eyes from inside the face
    eyes = eye_cascade.detectMultiScale(roi_gray)
    for (ex,ey,ew,eh) in eyes:
        #Enclose the detected eye in a rectangle
        cv2.rectangle(roi_color,(ex,ey),(ex+ew,ey+eh),(0,255,0),2)

#Image display
cv2.imshow('img',img)

#Press any key to finish
cv2.waitKey(0)
cv2.destroyAllWindows()

Execution result

If it is such an image, it will be recognized like this. I recognize all the faces, but the eyes don't recognize it very much, probably because of the vague image. result.png

If it is such an image, it will be recognized like this. The whole face is quite recognizable. On the other hand, the eyes recognize if the line of sight is facing the front. result12.png

If you understand the detection patterns of your eyes and profile to some extent, you will be able to guess what you are looking at and what is ahead of you.

Recommended Posts

Face recognition using OpenCV (Haar-like feature classifier)
Face recognition with Python's OpenCV
Feature detection using opencv (corner detection)
I tried face recognition using Face ++
Face recognition / cutting with OpenCV
Face recognition using principal component analysis
Try face recognition with python + OpenCV
Face recognition with camera with opencv3 + python2.7
Face detection using a cascade classifier
I tried face recognition with OpenCV
[python, openCV] base64 Face recognition with images
Estimate who's face using OpenCV (Eigenface, Fisherface, LBPH)
Face recognition with Edison
Real-time face recognition with video acquired by getUserMedia [HTML5, openCV]
I tried face recognition of the laughter problem using Keras.
I tried face recognition from the video (OpenCV: python version)
Similar face image detection using face recognition and PCA and K-means clustering