使用混合边缘感兴趣区域(MROI)方法的人脸检测包装器
mroi-fd的Python项目详细描述
自动安装
您可以使用pip
安装混合MROI人脸检测包装:
pip install mroi-fd
手动安装
这个项目是在python版本3.7.4
上开发和测试的。请检查一下
您的python版本使用python --version
。如果你的系统有不同的
python版本,可以考虑使用
pyenv(见Using pyenv)
首先,克隆并cd
到存储库中:
在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通过选择人脸来提高人脸检测速度 只考虑子区域的检测算法 检测到)而不是整个帧。在
有三种预定义的主要例程可供您使用:
- 哈尔级联分类器
- 联合叶栅
- 多任务卷积神经网络
当主例程检测不到人脸时,实现将切换到 运行模板匹配算法的转义例程。在
示例
使用混合MROI实现人脸检测
为了使用混合MROI人脸检测算法
检测器,需要创建继承FaceDetector
类的子类,
并重写其detect
方法。实现需要detect
方法返回一个face ROI或None
;否则,将返回混合MROI face
探测器可能失效。在
下面是一个示例,其中我们使用python implementation of MTCNN:
importcv2frommtcnn.mtcnnimportMTCNNfrommroi_fdimportFaceDetectorclassMROI_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)@staticmethoddefdetect(fd_obj,image):rgb_src=cv2.cvtColor(image,cv2.COLOR_BGR2RGB)result=fd_obj.detect_faces(rgb_src)iflen(result)>0:returnresult[0]['box']else:returnNone
在内部,这基本上就是预先定义的hyrbid MROI面部探测器
(即,MROI_HaarCascade
和{
frommroi_fdimportMROI_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")whileTrue:ret,frame=cap.read()# No more images; exit.ifnotret: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()ifROIisnotNone:x,y,w,h=ROIcv2.rectangle(frame,(x,y),(x+w,y+h),(0,255,0),2)cv2.imshow("MROI_MTCNN Face Detector",frame)ifcv2.waitKey(1)==ord('q'):breakfd.clean()cap.release()cv2.destroyAllWindows()
- 项目
标签: