[PYTHON] Anime face detection with OpenCV

https://github.com/opencv/opencv

Preparation

・ 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

Create classifier and detect face position

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]]

Cut out the face one by one

#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)

スクリーンショット 2017-08-13 3.33.43.png

Enclose the face in a square

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)

faces.jpg

Feature file list

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

Anime face detection with OpenCV
Face detection with Python + OpenCV
Face detection with Python + OpenCV (rotation invariant)
Hello World and face detection with OpenCV 4.3 + Python
Resize, mosaic, face detection with OpenCV, sometimes Zojirushi
Face recognition with Python's OpenCV
Try edge detection with OpenCV
Face detection with Python + dlib
Real-time edge detection with OpenCV
Face recognition / cutting with OpenCV
Face detection with Haar Cascades
Face detection with YOLO Face (Windows10, Python3.6)
Face detection with Lambda (Python) + Rekognition
[Python] Using OpenCV with Python (Edge Detection)
Try face recognition with python + OpenCV
First Anime Face Recognition with Chainer
I tried face detection with MTCNN
Cut out face with Python + OpenCV
Face recognition with camera with opencv3 + python2.7
Cat detection with OpenCV (model distribution)
I tried face recognition with OpenCV
Hello World and face detection with opencv-python 4.2
[Python] Face detection by OpenCV (Haar Cascade)
Face recognition of anime characters with Keras
[python, openCV] base64 Face recognition with images
Face detection from multiple image files with openCV, cut out and save
Performance comparison of face detector with Python + OpenCV
Categorize face images of anime characters with Chainer
Face recognition with Edison
Detect stoop with OpenCV
Binarization with OpenCV / Python
Rotate sprites with OpenCV
Data Augmentation with openCV
Easy TopView with OpenCV
Stumble with homebrew opencv3
[Ubuntu] [Python] Face detection comparison between dlib and OpenCV
Face detection from images taken with Raspberry Pi camera
"Apple processing" with OpenCV3 + Python3
Real-time face recognition with video acquired by getUserMedia [HTML5, openCV]
Image editing with python OpenCV
Camera capture with Python + OpenCV
Feature detection using opencv (corner detection)
Face recognition with Amazon Rekognition
[Python] Using OpenCV with Python (Basic)
A memo when face is detected with Python + OpenCV quickly
Binarize photo data with OpenCV
Loop video loading with opencv
Face detection summary in Python
Get image features with OpenCV
Blockchain tampering detection with Python
Try face recognition with Python
Try OpenCV with Google Colaboratory
Cascade classifier creation with opencv
Using OpenCV with Python @Mac
Image recognition with Keras + OpenCV
Draw a watercolor illusion with edge detection in Python3 and openCV3
I want to check the position of my face with OpenCV!
Replace your face with Twitter icon with openCV face recognition and do ZOOM
[Explanation for beginners] OpenCV face detection mechanism and practice (detect MultiScale)
Shining life with Python and OpenCV
Real-time image processing basics with opencv