1. 写在前面
本文章是对Github上基于Python的动漫人脸检测(Anime Face Detection)算法汇总!
2. 测试样例
3. 测试设备
- CPU:
12 Intel(R) Xeon(R) CPU E5-2603 v4 @ 1.70GHz
- GPU:
8 NVIDIA GeForce GTX 1080 Ti
4. 人脸检测
4.1. 基于LBP的动漫人脸检测
4.1.1. 仓库地址
4.1.2. 环境配置
- 依赖模块:
pip install opencv-python
- 检测模型:lbp_anime_face_detect.xml
4.1.3. 示例代码
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])
4.1.4. 检测结果
实检 | 漏检 | 误检 | 耗时 |
---|---|---|---|
13 | 1 | 1 | 1.20s |
4.2. 基于MLP的动漫人脸检测
4.2.1. 仓库地址
4.2.2. 环境配置
- 系统要求:仅限
Linux
系统 - 依赖模块:
pip install pillow opencv-python animeface
注意:如果pip install animeface
报错,请下载已经编译好的animeface-1.1.0-cp37-cp37m-manylinux1_x86_64.whl.whl文件,然后使用如下命令安装:(不过用这种方法安装,强制要求Python版本是3.7)
pip install animeface-1.1.0-cp37-cp37m-manylinux1_x86_64.whl
4.2.3. 示例代码
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])
4.2.4. 检测结果
实检 | 漏检 | 误检 | 耗时 |
---|---|---|---|
17 | 0 | 4 | 28.28s |
4.3. 基于HOG的动漫人脸检测
4.3.1. 仓库地址
4.3.2. 环境配置
- 依赖模块:
pip install opencv-python dlib
- 检测模型:hog_anime_face_detect.svm
注意:对于Windwos
系统,dlib
库下载后还需要用C++
编译器编译,因此你需要安装Visual Studio
并配置C++
编译环境,如果你已经安装并配置,那么请忽略;如果没有,请看这篇教程。
4.3.3. 示例代码
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])
4.3.4. 检测结果
实检 | 漏检 | 误检 | 耗时 |
---|---|---|---|
10 | 3 | 0 | 2.42s |
4.4. 基于SSD的动漫人脸检测
4.4.1. 仓库地址
4.4.2. 环境配置
- 依赖模块:
pip install opencv-python numpy torch
- 检测模型:ssd_anime_face_detect.pth
4.4.3. 示例代码
ssd_anime_face_detect.py(代码太长,不宜展示,请下载后查看)
4.4.4. 检测结果
实检 | 漏检 | 误检 | 耗时 |
---|---|---|---|
13 | 0 | 0 | 0.72s |
5. 其它实验
5.1. 测试样例
5.2. 检测结果
6. 结果分析
基于MLP的动漫人脸检测算法速度太慢,达不到实际应用的要求。而其它三个算法都各有千秋,目标图片不同,检测的精度也不同。
7. 完整代码
本文所有代码均可在Github上查看:https://github.com/XavierJiezou/anime-face-detection
8. 下步计划
- 基于Faster-RCNN的动漫人脸检测:https://github.com/qhgz2013/anime-face-detector/
- 基于CNN的轻量级的动漫人脸检测:https://github.com/ShiqiYu/libfacedetection
- 其它相关项目:https://github.com/search?p=1&q=anime+face+detection&type=Repositories
9. 引用参考
https://github.com/nagadomi/lbpcascade_animeface
https://github.com/nya3jp/python-animeface
https://github.com/marron-akanishi/AFD
https://github.com/WynMew/AnimeFaceBoxes