This repository is the summary of anime face detection methods based on Python.
There are four methods for anime face detection: lbp
, mlp
, hog
and ssd
.
lbp
:pip install opencv-python
mlp
:pip install pillow opencv-python animeface
hog
:pip install opencv-python dlib
ssd
:pip install opencv-python numpy torch
For image detection:
python ./image_detect.py ./test/1.jpg lbp
For video detection:
python ./video_detect.py ./test/1.mp4 lbp
- Param one: path of images/videos
- Param two: method to be selected
- CPU:
12 Intel(R) Xeon(R) CPU E5-2603 v4 @ 1.70GHz
- GPU:
8 NVIDIA GeForce GTX 1080 Ti
- Module:
pip install opencv-python
- Model: lbp_anime_face_detect.xml
import cv2
import sys
def lbp_anime_face_detect(file_name):
img = cv2.imread(file_name)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.equalizeHist(img_gray)
face_cascade = cv2.CascadeClassifier('../model/lbp_anime_face_detect.xml')
faces = face_cascade.detectMultiScale(img_gray)
for x, y, w, h in faces:
img = cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 255), 5)
cv2.imwrite(f'../result/lbp_anime_face_detect_{file_name[-5]}.jpg', img)
if __name__ == '__main__':
lbp_anime_face_detect(sys.argv[1])
Total | Missing | Error | Time |
---|---|---|---|
13 | 1 | 1 | 1.20s |
- System Requirements: Only for
Linux
system - Module:
pip install pillow opencv-python animeface
Note: If pip install animeface
reports an error, please download the compiled file animeface-1.1.0-cp37-cp37m-manylinux1_x86_64.whl.whl, and then use the following command to install: (It is mandatory that the Python version is 3.7)
pip install animeface-1.1.0-cp37-cp37m-manylinux1_x86_64.whl
import cv2
import animeface
from PIL import Image
import sys
def mlp_anime_face_detect(file_name):
img = cv2.imread(file_name)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.equalizeHist(img_gray)
faces = animeface.detect(Image.fromarray(img_gray))
for each in faces:
temp = each.face.pos
x = temp.x
y = temp.y
w = temp.width
h = temp.height
img = cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 255), 5)
cv2.imwrite(f'../result/mlp_anime_face_detect_{file_name[-5]}.jpg', img)
if __name__ == '__main__':
mlp_anime_face_detect(sys.argv[1])
Total | Missing | Error | Time |
---|---|---|---|
17 | 0 | 4 | 28.28s |
- Module:
pip install opencv-python dlib
- Model: hog_anime_face_detect.svm
Note:The dlib
library needs to be compiled with the C++
compiler after downloading, so you need to install Visual Studio
and configure the C++
compilation environment. If C++
environment have been installed and configured, please ignore; If not, please see this Article.
import cv2
import dlib
import sys
def hog_anime_face_detect(file_name):
img = cv2.imread(file_name)
img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
img_gray = cv2.equalizeHist(img_gray)
face_detector = dlib.simple_object_detector('../model/hog_anime_face_detect.svm')
faces = face_detector(img_gray)
for face in faces:
left = face.left()
top = face.top()
right = face.right()
bottom = face.bottom()
cv2.rectangle(img, (left, top), (right, bottom), (255, 0, 255), 5)
cv2.imwrite(f'../result/hog_anime_face_detect_{file_name[-5]}.jpg', img)
if __name__ == '__main__':
hog_anime_face_detect(sys.argv[1])
Total | Missing | Error | Time |
---|---|---|---|
10 | 3 | 0 | 2.42s |
- Module:
pip install opencv-python numpy torch
- Model: ssd_anime_face_detect.pth
ssd_anime_face_detect.py (The code is too long to display, Please download and view)
Total | Missing | Error | Time |
---|---|---|---|
13 | 0 | 0 | 0.72s |
The anime face detection algorithm based on MLP is too slow to meet the requirements of practical applications. The other three algorithms behave differently on different sample image.
- Anime Face Detection Based on Faster-RCNN: https://github.com/qhgz2013/anime-face-detector/
- Anime Face Detection Based on CNN: https://github.com/ShiqiYu/libfacedetection
- Other Related Work: https://github.com/search?p=1&q=anime+face+detection&type=Repositories