I was asked "Can't you do face recognition quickly?", So I tried it. I can't do fine tuning.
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
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
Image after recognition
Recommended Posts