Try face recognition with python + OpenCV

I was asked "Can't you do face recognition quickly?", So I tried it. I can't do fine tuning.

Installation

It's pretty easy on a Mac. Install OpenCV using brew. python uses what I put in with brew. It seems that numpy is also needed, but it is omitted because it was already installed. Apparently, when installing OpenCV, the package cv2 for Python is copied to site-package, so the installation order is Python → numpy → OpenCV.

bash


brew tap homebrew/science
brew install opencv

Run with Python

For the time being, expose the code first. I decided to leave the comments when I used it for trial and error, though it was dirty. I have left the explanation for parameter tuning, but www

recognize.py


# -*- coding: utf-8 -*-

import cv2

#Features for face detection of HAAR classifier
#cascade_path = "/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_default.xml"
cascade_path = "/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_alt.xml"
#cascade_path = "/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_alt2.xml"
#cascade_path = "/usr/local/opt/opencv/share/OpenCV/haarcascades/haarcascade_frontalface_alt_tree.xml"


image_path = "lena.jpg "

color = (255, 255, 255) #White
#color = (0, 0, 0) #black

#File reading
image = cv2.imread(image_path)
#Grayscale conversion
image_gray = cv2.cvtColor(image, cv2.cv.CV_BGR2GRAY)

#Acquire the features of the cascade classifier
cascade = cv2.CascadeClassifier(cascade_path)

#Execution of object recognition (face recognition)
#image – CV_8U type matrix. Objects are detected in the images stored here
#objects – A vector whose elements are rectangles. Each rectangle contains the detected object
#scaleFactor – Represents the amount of reduction at each image scale
#minNeighbors – Candidate rectangles must contain at least this number of neighbor rectangles
#flags – This parameter is not used in the new cascade. For older cascades, it has the same meaning as for the cvHaarDetectObjects function.
#minSize – The minimum size an object can take. Objects smaller than this are ignored
facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=1, minSize=(1, 1))
#facerect = cascade.detectMultiScale(image_gray, scaleFactor=1.1, minNeighbors=3, minSize=(10, 10), flags = cv2.cv.CV_HAAR_SCALE_IMAGE)

print "face rectangle"
print facerect

if len(facerect) > 0:
	#Create a rectangle that surrounds the detected face
	for rect in facerect:
		cv2.rectangle(image, tuple(rect[0:2]),tuple(rect[0:2]+rect[2:4]), color, thickness=2)
	
	#Saving recognition results
	cv2.imwrite("detected.jpg ", image)

Since we have not prepared an image for learning this time, we are using the features of the HAAR classifier that comes with OpenCV. See here for the HAAR classifier ([10th CV Study Group OpenCV Festival Thorough Explanation of Object Detection!](Http://www.slideshare.net/takmin/opecv-object-detectiontakmin opencv)). I could recognize any feature amount in the famous Lena's image, but when I tried it with other images, haarcascade_frontalface_default.xml could not be recognized very well, so it is better to try various things.

Original image lena.jpg

Image after recognition detected.jpg

Recommended Posts

Try face recognition with python + OpenCV
Try face recognition with Python
Face recognition with camera with opencv3 + python2.7
[python, openCV] base64 Face recognition with images
Face recognition with Python's OpenCV
Face detection with Python + OpenCV
Face recognition / cutting with OpenCV
[Python3] [Ubuntu16] [Docker] Try face recognition with OpenFace
Try face recognition with Generated Photos
Cut out face with Python + OpenCV
I tried face recognition with OpenCV
Face detection with Python + OpenCV (rotation invariant)
Serverless face recognition API made with Python
Try scraping with Python.
Face recognition with Edison
Binarization with OpenCV / Python
Hello World and face detection with OpenCV 4.3 + Python
Performance comparison of face detector with Python + OpenCV
Try to detect fish with python + OpenCV2.4 (unfinished)
"Apple processing" with OpenCV3 + Python3
Try edge detection with OpenCV
Image editing with python OpenCV
Camera capture with Python + OpenCV
Try Python output with Haxe 3.2
Face recognition with Amazon Rekognition
[Python] Using OpenCV with Python (Basic)
Face detection with Python + dlib
Try running Python with Try Jupyter
Try OpenCV with Google Colaboratory
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
Anime face detection with OpenCV
Try projective transformation of images using OpenCV with Python
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)
Number recognition in images with Python
Shining life with Python and OpenCV
Try scraping with Python + Beautiful Soup
Face detection with YOLO Face (Windows10, Python3.6)
[Python] Using OpenCV with Python (Image Filtering)
Neural network with OpenCV 3 and Python 3
[Super easy] Simultaneous face recognition and facial expression recognition in real time with Python and OpenCV!
[Python] Using OpenCV with Python (Image transformation)
Try to operate Facebook with Python
[Python] Using OpenCV with Python (Edge Detection)
Try singular value decomposition with Python
Try python
A memo when face is detected with Python + OpenCV quickly
Easy Python + OpenCV programming with Canopy
I tried face recognition from the video (OpenCV: python version)
[OpenCV] Personal identification with face photo
python x tensoflow x image face recognition
First Anime Face Recognition with Chainer
Load gif images with Python + OpenCV
Try frequency control simulation with Python
Find image similarity with Python + OpenCV
Try blurring the image with opencv2
Use OpenCV with Python 3 in Window
Draw an illustration with Python + OpenCV
Track baseball balls with Python + OpenCV