使用基于边缘的混合感兴趣区域 (MROI) 方法的人脸检测包装器。”
项目描述
自动安装
您可以使用以下方法安装混合 MROI 人脸检测包装器pip:
pip install mroi-fd
手动安装
这个项目是在 python 版本上开发和测试的3.7.4。请使用 . 检查您的 python 版本python --version。如果您的系统有不同的 python 版本,您可能需要考虑使用
pyenv(请参阅使用 pyenv)
首先,克隆并cd进入存储库:
git clone https://gitlab.com/ailabuser/bacha_hybrid_mroi_face_detection
cd bacha_hybrid_mroi_face_detection
在 Windows 上:
创建python虚拟环境:
mkvirtualenv venv
激活虚拟环境(deactivate停用虚拟环境):
workon venv
安装所有必需的依赖项,同时保持虚拟环境处于活动状态:
pip install -r requirements.txt
在 Linux 上:
创建python虚拟环境:
virtualenv -p /usr/bin/python3 venv
激活虚拟环境(deactivate停用虚拟环境):
source venv/bin/activate
安装所有必需的依赖项,同时保持虚拟环境处于活动状态:
pip install -r requirements.txt
使用 pyenv(Linux、Windows、MacOS)
如果您的 python 版本不是 3.7.4,您可能需要使用
pyenv。安装后pyenv,安装特定的python版本。在 Linux 上,这可以通过运行以下命令来完成:
env PYTHON_CONFIGURE_OPTS="--enable-shared" pyenv install 3.7.4
然后,创建一个虚拟环境:
pyenv virtualenv 3.7.4 bacha_mroi_face_detection
您可以使用如下方式激活虚拟环境pyenv:
pyenv activate bacha_mroi_face_detection
描述
人脸检测技术使用基于边缘的混合感兴趣区域 (MROI) 方法。从某种意义上说,它是混合的,实现运行一个主例程来检测人脸,但当主例程失败时切换到转义例程。使用 MROI 可以提高人脸检测速度,因为选择的人脸检测算法只考虑子区域(之前检测到人脸的地方)而不是全帧。
您可以使用三种预定义的主要例程选择:
- 哈尔级联分类器
- 联合级联
- 多任务卷积神经网络 (MTCNN)
当主程序未能检测到人脸时,实现切换到运行模板匹配算法的逃生程序。
例子
使用混合 MROI 进行人脸检测实施
为了将您的人脸检测算法与混合 MROI 人脸检测器一起使用,您需要创建一个继承该类的子FaceDetector类,并覆盖其detect方法。实现要求该detect
方法返回人脸 ROI 或None; 否则,混合 MROI 人脸检测器可能会失败。
这是一个示例,其中我们使用MTCNN 的 python 实现:
import cv2
from mtcnn.mtcnn import MTCNN
from mroi_fd import FaceDetector
class MROI_MTCNN(FaceDetector):
def __init__(self):
# The main routine face detector object used to detect faces.
fd_obj = MTCNN()
# Initialize using base class constructor. We pass the face detector
# object (fd_obj) and use the MROI with fixed-margin approach with
# a template matching escape routine.
super().__init__(fd_obj, mode=FaceDetector.FMTM)
@staticmethod
def detect(fd_obj, image):
rgb_src = cv2.cvtColor(image, cv2.COLOR_BGR2RGB)
result = fd_obj.detect_faces(rgb_src)
if len(result) > 0:
return result[0]['box']
else:
return None
在内部,这基本上是如何定义预定义的混合 MROI 面部检测器(即MROI_HaarCascade和MROI_MTCNN)。只需导入它们
from mroi_fd import MROI_HaarCascade, MROI_MTCNN
运行人脸检测器
要使用人脸检测器,只需实例化混合 MROI 人脸检测器并通过调用其run方法来运行它。下面是一个简单的脚本,它运行人脸检测器并循环输入图像。
fd = MROI_MTCNN()
fd.run() # This runs the face detector in the background.
cap = cv2.VideoCapture("/path/to/video/file")
while True:
ret, frame = cap.read()
# No more images; exit.
if not ret:
break
# Feed the image into the face detector.
fd.input_image(frame)
# Get the ROI containing the face. This will be `None` if no face is
# detected.
ROI = fd.get_face_region()
if ROI is not None:
x, y, w, h = ROI
cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
cv2.imshow("MROI_MTCNN Face Detector", frame)
if cv2.waitKey(1) == ord('q'):
break
fd.clean()
cap.release()
cv2.destroyAllWindows()
项目详情
下载文件
下载适用于您平台的文件。如果您不确定要选择哪个,请了解有关安装包的更多信息。