https://github.com/opencv/opencv
・ Allow ʻimport cv2` ・ Make it possible to read feature files
See here How to set up the development environment of OpenCV 3 + Python 2/3 on Mac OS X
import os
import cv2
#Create a classifier based on the feature file
classifier = cv2.CascadeClassifier('lbpcascade_animeface.xml')
#Face detection
image = cv2.imread('newGame.jpg')
#Speed up processing with grayscale
gray_image = cv2.cvtColor(image,cv2.COLOR_BGR2GRAY)
faces = classifier.detectMultiScale(gray_image)
Confirmed that the position and size of the faces of 6 people could be detected by print (faces)
[[485 148 134 134]
[456 313 193 193]
[380 58 98 98]
[649 227 127 127]
[373 245 108 108]
[637 54 104 104]]
#Create directory
output_dir = 'faces'
if not os.path.exists(output_dir):
os.makedirs(output_dir)
for i, (x,y,w,h) in enumerate(faces):
#Cut out the face one by one
face_image = image[y:y+h, x:x+w]
output_path = os.path.join(output_dir,'{0}.jpg'.format(i))
cv2.imwrite(output_path,face_image)
cv2.imwrite('face.jpg',image)
for x,y,w,h in faces:
#Draw a square
cv2.rectangle(image, (x,y), (x+w,y+h), color=(0,0,255), thickness=3)
cv2.imwrite('faces.jpg',image)
https://github.com/opencv/opencv/tree/master/data/haarcascades
haarcascade_eye.xml haarcascade_eye_tree_eyeglasses.xml haarcascade_frontalcatface.xml haarcascade_frontalcatface_extended.xml haarcascade_frontalface_alt.xml haarcascade_frontalface_alt2.xml haarcascade_frontalface_alt_tree.xml haarcascade_frontalface_default.xml haarcascade_fullbody.xml haarcascade_lefteye_2splits.xml haarcascade_licence_plate_rus_16stages.xml haarcascade_lowerbody.xml haarcascade_profileface.xml haarcascade_righteye_2splits.xml haarcascade_russian_plate_number.xml haarcascade_smile.xml haarcascade_upperbody.xml
Click here for lbpcascade_animeface.xml used this time https://github.com/nagadomi/lbpcascade_animeface
Reference http://gihyo.jp/book/2017/978-4-7741-8367-1
Recommended Posts