许可优化
许可优化
产品
产品
解决方案
解决方案
服务支持
服务支持
关于
关于
软件库
当前位置:服务支持 >  软件文章 >  图像处理:使用Python+OpenFace实现人脸识别与关键点(Landmarks)检测

图像处理:使用Python+OpenFace实现人脸识别与关键点(Landmarks)检测

阅读数 9
点赞 0
article_banner

1、如何在Python上安装OpenFace

下载安装

1、首先需要下载OpenFace源码:

(Anaconda、 Git  等自行提前安装)

git clone https://github.com/cmusatyalab/openface.git

2、接着进入openface路径

cd openface

3、执行:

pip install -r requirements.txt

4、执行:

python setup.py install

需要说明的是,第3步,自动安装可能会很慢或出问题,可以(参考该文件内容)手动安装依赖项。

完成验证

安装完之后,可以在控制台中,进入python环境,然后导入openface的包看是否报错

可能会有确实其他包的报错,比如报缺少“opencv_python”,缺什么就装什么即可,知道不再报错。

模型  下载

进入

cd models

运行

./get-models.sh

或者手动下载(参考该文件内容):

1、http://dlib.net/files/shape_predictor_68_face_landmarks.dat.bz2

解压到:【*openface\models\dlib】目录下

2、https://storage.cmusatyalab.org/openface-models/nn4.small2.v1.t7

https://storage.cmusatyalab.org/openface-models/celeb-classifier.nn4.small2.v1.pkl

这两个放到【*openface\models\openface】目录下

2、用opencv实现一个实时摄像头获取画面的程序

import threadingimport cv2  class OpcvCapture(threading.Thread):    def __init__(self, win_name, cam_name):        super().__init__()        self.cam_name = cam_name        self.win_name = win_name     def run(self):        capture = cv2.VideoCapture(self.cam_name)        while (True):            # 获取一帧            ret, frame = capture.read()            cv2.imshow(self.win_name, frame)            cv2.waitKey(1)  if __name__ == "__main__":    camera1 = OpcvCapture("camera1", 1)    camera1.start()    run_code = 0

3、添加 人脸检测 和关键点识别代码

加载必要的数据和模型:

路径根据实际进行配置,或将【models】文件夹复制到项目所在位置

import osimport openface  fileDir = os.path.dirname(os.path.realpath(__file__))modelDir = os.path.join(fileDir, 'models')dlibModelDir = os.path.join(modelDir, 'dlib') align = openface.AlignDlib(os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat"))

添加检测算法并绘制点

    def _detector(self, frame, mirror=False):        show_img = cv2.flip(frame, flipCode=1) if mirror else frame        rects = align.getAllFaceBoundingBoxes(show_img)        if len(rects) > 0:            bb = align.findLandmarks(show_img, rects[0])            for pt in bb:                cv2.circle(show_img, pt, 3, [0, 0, 255], thickness=-1)        return show_img

在获取到的帧上进行调用

在第二节的摄像头画面获取的程序中,每获取到一帧图像后调用检测算法,将返回的结果进行显示:

    #摄像头实时画面线程类的部分代码    def run(self):        capture = cv2.VideoCapture(self.cam_name)        while (True):            # 获取一帧            ret, frame = capture.read()            # 获取的帧送入检测,绘制检测结果后返回,自拍模式做镜像            show_img = self._detector(frame, mirror=True)            cv2.imshow(self.win_name, show_img)            cv2.waitKey(1)

最终效果:

完整代码:

#!/usr/bin/env python# -*- coding:utf-8 -*-# @FileName :opfacevideo.py# @Time :2020/6/20 16:23# @Author :Raink  import threadingimport cv2import osimport openface  fileDir = os.path.dirname(os.path.realpath(__file__))modelDir = os.path.join(fileDir, 'models')dlibModelDir = os.path.join(modelDir, 'dlib') align = openface.AlignDlib(os.path.join(dlibModelDir, "shape_predictor_68_face_landmarks.dat"))  class OpcvCapture(threading.Thread):    def __init__(self, win_name, cam_name):        super().__init__()        self.cam_name = cam_name        self.win_name = win_name        # self.frame = np.zeros([400, 400, 3], np.uint8)     def run(self):        capture = cv2.VideoCapture(self.cam_name)        while (True):            # 获取一帧            ret, frame = capture.read()            # 获取的帧送入检测,绘制检测结果后返回,自拍模式做镜像            show_img = self._detector(frame, mirror=True)            cv2.imshow(self.win_name, show_img)            cv2.waitKey(1)     def _detector(self, frame, mirror=False):        show_img = cv2.flip(frame, flipCode=1) if mirror else frame        rects = align.getAllFaceBoundingBoxes(show_img)        if len(rects) > 0:            bb = align.findLandmarks(show_img, rects[0])            for pt in bb:                cv2.circle(show_img, pt, 3, [0, 0, 255], thickness=-1)        return show_img  if __name__ == "__main__":    camera1 = OpcvCapture("camera1", 1)    camera1.start()


免责声明:本文系网络转载或改编,未找到原创作者,版权归原作者所有。如涉及版权,请联系删

相关文章
技术文档
QR Code
微信扫一扫,欢迎咨询~
customer

online

联系我们
武汉格发信息技术有限公司
湖北省武汉市经开区科技园西路6号103孵化器
电话:155-2731-8020 座机:027-59821821
邮件:tanzw@gofarlic.com
Copyright © 2023 Gofarsoft Co.,Ltd. 保留所有权利
遇到许可问题?该如何解决!?
评估许可证实际采购量? 
不清楚软件许可证使用数据? 
收到软件厂商律师函!?  
想要少购买点许可证,节省费用? 
收到软件厂商侵权通告!?  
有正版license,但许可证不够用,需要新购? 
联系方式 board-phone 155-2731-8020
close1
预留信息,一起解决您的问题
* 姓名:
* 手机:

* 公司名称:

姓名不为空

姓名不为空

姓名不为空
手机不正确

手机不正确

手机不正确
公司不为空

公司不为空

公司不为空