[PYTHON] I tried face recognition with OpenCV

OpenCV is an open source library that can be used for image processing. It can also be used in Python, so it can also be used in machine learning. This time, I will try face recognition using this OpenCV.

This operating environment

What to do this time

Load the image, recognize the face in the image, and display the coordinates and size of the face. After that, the recognized face is displayed as a red square. OpenCV has several feature data files (cascade files) that can be used for face recognition. This time, we will use `haarcascade_frontalface_alt.xml``` as a data file that recognizes the face when facing the front. In addition, this time, it is recognized using the following image ( before.jpg `` `in the source code). before.jpg

Reference source: https://www.pakutaso.com/assets_c/2016/03/SAYA151005380I9A8403-thumb-autox1600-21549.jpg

Source code

front_face.py


#coding: utf-8
import cv2

cascade_file = "haarcascade_frontalface_alt.xml"
image_file = "before.jpg "

img = cv2.imread(image_file)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

cascade = cv2.CascadeClassifier(cascade_file)
face_list = cascade.detectMultiScale(img_gray, minSize=(150, 150))

if len(face_list) == 0:
  print("Fail recognise")
  quit()

for (x, y, w, h) in face_list:
  print("Face coordinates=", x, y, w, h)
  color = (0, 0, 225)
  pen_w = 8
  cv2.rectangle(img, (x, y), (x+w, y+h), color, thickness = pen_w)

cv2.imwrite("after.jpg ", img)

When I ran it, I got the following error.

OpenCV Error: Assertion failed (!empty()) in detectMultiScale, file /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/objdetect/src/cascadedetect.cpp, line 1639
Traceback (most recent call last):
  File "face_front.py", line 11, in <module>
    face_list = cascade.detectMultiScale(img_gray, minSize=(150, 150))
cv2.error: /home/travis/miniconda/conda-bld/conda_1486587069159/work/opencv-3.1.0/modules/objdetect/src/cascadedetect.cpp:1639: error: (-215) !empty() in function detectMultiScale

--Cause

When I looked it up, it seems that the error is caused by the wrong path of the cascade file.

As a first aid, I was able to copy this cascade file to the directory containing the source code and use it.

result

The result of the execution is the following image. after.jpg

reference

From installing OpenCV 3.1 on Ubuntu16.04 LTS to face recognition test

Recommended Posts

I tried face recognition with OpenCV
Face recognition with Python's OpenCV
I tried face recognition using Face ++
Face recognition / cutting with OpenCV
I tried trimming efficiently with OpenCV
Try face recognition with python + OpenCV
I tried face detection with MTCNN
Face recognition with camera with opencv3 + python2.7
I tried face recognition from the video (OpenCV: python version)
I tried non-photorealistic rendering with Python + opencv
[python, openCV] base64 Face recognition with images
I tried simple image recognition with Jupyter
Face recognition with Edison
I tried handwriting recognition of runes with scikit-learn
I tried "smoothing" the image with Python + OpenCV
I tried image recognition of CIFAR-10 with Keras-Learning-
I tried "differentiating" the image with Python + OpenCV
I tried image recognition of CIFAR-10 with Keras-Image recognition-
I tried to detect motion quickly with OpenCV
I tried fp-growth with python
I tried scraping with Python
I tried Learning-to-Rank with Elasticsearch!
Face recognition with Amazon Rekognition
Face detection with Python + OpenCV
Try face recognition with Python
I tried gRPC with Python
I tried scraping with python
Image recognition with Keras + OpenCV
Anime face detection with OpenCV
[OpenCV / Python] I tried image analysis of cells with OpenCV
I tried "morphology conversion" of images with Python + OpenCV
I tried "gamma correction" of the image with Python + OpenCV
Real-time face recognition with video acquired by getUserMedia [HTML5, openCV]
I tried summarizing sentences with summpy
I tried machine learning with liblinear
I tried moving food with SinGAN
I tried face recognition of the laughter problem using Keras.
Try face recognition with Generated Photos
I tried using GrabCut of OpenCV
I tried implementing DeepPose with PyTorch
[OpenCV] Personal identification with face photo
First Anime Face Recognition with Chainer
Cut out face with Python + OpenCV
I tried handwriting recognition of runes with CNN using Keras
I tried running prolog with python 3.8.2.
I tried SMTP communication with Python
I tried sentence generation with GPT-2
I tried learning LightGBM with Yellowbrick
Object recognition with openCV by traincascade
I tried to process the image in "sketch style" with OpenCV
I tried to process the image in "pencil style" with OpenCV
Replace your face with Twitter icon with openCV face recognition and do ZOOM
I tried to make an image similarity function with Python + OpenCV
Face recognition using OpenCV (Haar-like feature classifier)
Face detection with Python + OpenCV (rotation invariant)
I want to detect objects with OpenCV
OpenCV AI Kit (OAK-D) I tried @ windows10
I tried multiple regression analysis with polynomial regression
I tried sending an SMS with Twilio
I tried using Amazon SQS with django-celery
I tried to implement Autoencoder with TensorFlow