1.OpenFace简介

http://cmusatyalab.github.io/openface/
安装就按照官方教程来就好了

装完各种依赖之后运行一下命令

https://github.com/cmusatyalab/openface.git --recursive
cd openface
sudo python setup.py install
sh modles/get-models.sh

2.素材准备

准备两个人的不同角度的照片我这准备了李、王两个人各三十张照片作为训练集,各六张张做为测试集;训练数据放在openface/ws_train_data路径下


下图是训练集图片:

测试集数据:


这里需要注意的是bmp文件是不被支持用于目标检测的

3.预处理(目标检测)

进入openface的根目录,输入

$ ./util/align-dlib.py ws_train_data/ align outerEyesAndNose ws_pre_process

ws_train_data/这个路径下的使我们训练集的数据,ws_pre_process是预处理结果的路径

一切顺利的话,会看到类似下面的输出:

在相应的路径你会看到这两个人的预处理结果,人脸被单独地截取出来了
.

4.生成相应的脸部特征表示数据

./batch-represent/main.lua -outDir ws_represent_data -data ws_pre_process/

ws_represent_data是输出路径,ws_pre_process/使我们的预处理结果路径

会在相应的ws_represent_data看到相应的两个CSV文件

5.训练模型

./demos/classifier.py train ws_represent_data/

会得到下列的输出

我在这里发现了一个Bug,就是在OpenFace根路径下的demos下的classifier.py文件中的LDA()函数已经不被sklearn库替换为了LinearDiscriminantAnalysis()

原先是这样的:

 clf = Pipeline([('lda', LDA(n_components=args.ldaDim)),('clf', clf_final)])

应该导入相应的模块,然后修改如下即可:

clf = Pipeline([('lda', LinearDiscriminantAnalysis(n_components=args.ldaDim)),('clf', clf_final)])

这里贴出完整的classifier.py

#!/usr/bin/env python2
#
# Example to classify faces.
# Brandon Amos
# 2015/10/11
#
# Copyright 2015-2016 Carnegie Mellon University
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.import time
start = time.time()import argparse
import cv2
import os
import pickle
import sysfrom operator import itemgetterimport numpy as np
np.set_printoptions(precision=2)
import pandas as pdimport openface
from sklearn.discriminant_analysis import LinearDiscriminantAnalysis
from sklearn.pipeline import Pipeline
# from sklearn.lda import LDA
from sklearn.preprocessing import LabelEncoder
from sklearn.svm import SVC
from sklearn.grid_search import GridSearchCV
from sklearn.mixture import GMM
from sklearn.tree import DecisionTreeClassifier
from sklearn.naive_bayes import GaussianNBfileDir = os.path.dirname(os.path.realpath(__file__))
modelDir = os.path.join(fileDir, '..', 'models')
dlibModelDir = os.path.join(modelDir, 'dlib')
openfaceModelDir = os.path.join(modelDir, 'openface')def getRep(imgPath, multiple=False):start = time.time()bgrImg = cv2.imread(imgPath)if bgrImg is None:raise Exception("Unable to load image: {}".format(imgPath))rgbImg = cv2.cvtColor(bgrImg, cv2.COLOR_BGR2RGB)if args.verbose:print("  + Original size: {}".format(rgbImg.shape))if args.verbose:print("Loading the image took {} seconds.".format(time.time() - start))start = time.time()if multiple:bbs = align.getAllFaceBoundingBoxes(rgbImg)else:bb1 = align.getLargestFaceBoundingBox(rgbImg)bbs = [bb1]if len(bbs) == 0 or (not multiple and bb1 is None):raise Exception("Unable to find a face: {}".format(imgPath))if args.verbose:print("Face detection took {} seconds.".format(time.time() - start))reps = []for bb in bbs:start = time.time()alignedFace = align.align(args.imgDim,rgbImg,bb,landmarkIndices=openface.AlignDlib.OUTER_EYES_AND_NOSE)if alignedFace is None:raise Exception("Unable to align image: {}".format(imgPath))if args.verbose:print("Alignment took {} seconds.".format(time.time() - start))print("This bbox is centered at {}, {}".format(bb.center().x, bb.center().y))start = time.time()rep = net.forward(alignedFace)if args.verbose:print("Neural network forward pass took {} seconds.".format(time.time() - start))reps.append((bb.center().x, rep))sreps = sorted(reps, key=lambda x: x[0])return srepsdef train(args):print("Loading embeddings.")fname = "{}/labels.csv".format(args.workDir)labels = pd.read_csv(fname, header=None).as_matrix()[:, 1]labels = map(itemgetter(1),map(os.path.split,map(os.path.dirname, labels)))  # Get the directory.fname = "{}/reps.csv".format(args.workDir)embeddings = pd.read_csv(fname, header=None).as_matrix()le = LabelEncoder().fit(labels)labelsNum = le.transform(labels)nClasses = len(le.classes_)print("Training for {} classes.".format(nClasses))if args.classifier == 'LinearSvm':clf = SVC(C=1, kernel='linear', probability=True)elif args.classifier == 'GridSearchSvm':print("""Warning: In our experiences, using a grid search over SVM hyper-parameters onlygives marginally better performance than a linear SVM with C=1 andis not worth the extra computations of performing a grid search.""")param_grid = [{'C': [1, 10, 100, 1000],'kernel': ['linear']},{'C': [1, 10, 100, 1000],'gamma': [0.001, 0.0001],'kernel': ['rbf']}]clf = GridSearchCV(SVC(C=1, probability=True), param_grid, cv=5)elif args.classifier == 'GMM':  # Doesn't work bestclf = GMM(n_components=nClasses)# ref:# http://scikit-learn.org/stable/auto_examples/classification/plot_classifier_comparison.html#example-classification-plot-classifier-comparison-pyelif args.classifier == 'RadialSvm':  # Radial Basis Function kernel# works better with C = 1 and gamma = 2clf = SVC(C=1, kernel='rbf', probability=True, gamma=2)elif args.classifier == 'DecisionTree':  # Doesn't work bestclf = DecisionTreeClassifier(max_depth=20)elif args.classifier == 'GaussianNB':clf = GaussianNB()# ref: https://jessesw.com/Deep-Learning/elif args.classifier == 'DBN':from nolearn.dbn import DBNclf = DBN([embeddings.shape[1], 500, labelsNum[-1:][0] + 1],  # i/p nodes, hidden nodes, o/p nodeslearn_rates=0.3,# Smaller steps mean a possibly more accurate result, but the# training will take longerlearn_rate_decays=0.9,# a factor the initial learning rate will be multiplied by# after each iteration of the trainingepochs=300,  # no of iternation# dropouts = 0.25, # Express the percentage of nodes that# will be randomly dropped as a decimal.verbose=1)if args.ldaDim > 0:clf_final = clfclf = Pipeline([('lda', LinearDiscriminantAnalysis(n_components=args.ldaDim)),('clf', clf_final)])# clf = Pipeline([('lda', LDA(n_components=args.ldaDim)),#                 ('clf', clf_final)])clf.fit(embeddings, labelsNum)fName = "{}/classifier.pkl".format(args.workDir)print("Saving classifier to '{}'".format(fName))with open(fName, 'w') as f:pickle.dump((le, clf), f)def infer(args, multiple=False):with open(args.classifierModel, 'rb') as f:if sys.version_info[0] < 3:(le, clf) = pickle.load(f)else:(le, clf) = pickle.load(f, encoding='latin1')for img in args.imgs:print("\n=== {} ===".format(img))reps = getRep(img, multiple)if len(reps) > 1:print("List of faces in image from left to right")for r in reps:rep = r[1].reshape(1, -1)bbx = r[0]start = time.time()predictions = clf.predict_proba(rep).ravel()maxI = np.argmax(predictions)person = le.inverse_transform(maxI)confidence = predictions[maxI]if args.verbose:print("Prediction took {} seconds.".format(time.time() - start))if multiple:print("Predict {} @ x={} with {:.2f} confidence.".format(person.decode('utf-8'), bbx,confidence))else:print("Predict {} with {:.2f} confidence.".format(person.decode('utf-8'), confidence))if isinstance(clf, GMM):dist = np.linalg.norm(rep - clf.means_[maxI])print("  + Distance from the mean: {}".format(dist))if __name__ == '__main__':parser = argparse.ArgumentParser()parser.add_argument('--dlibFacePredictor',type=str,help="Path to dlib's face predictor.",default=os.path.join(dlibModelDir,"shape_predictor_68_face_landmarks.dat"))parser.add_argument('--networkModel',type=str,help="Path to Torch network model.",default=os.path.join(openfaceModelDir,'nn4.small2.v1.t7'))parser.add_argument('--imgDim', type=int,help="Default image dimension.", default=96)parser.add_argument('--cuda', action='store_true')parser.add_argument('--verbose', action='store_true')subparsers = parser.add_subparsers(dest='mode', help="Mode")trainParser = subparsers.add_parser('train',help="Train a new classifier.")trainParser.add_argument('--ldaDim', type=int, default=-1)trainParser.add_argument('--classifier',type=str,choices=['LinearSvm','GridSearchSvm','GMM','RadialSvm','DecisionTree','GaussianNB','DBN'],help='The type of classifier to use.',default='LinearSvm')trainParser.add_argument('workDir',type=str,help="The input work directory containing 'reps.csv' and 'labels.csv'. Obtained from aligning a directory with 'align-dlib' and getting the representations with 'batch-represent'.")inferParser = subparsers.add_parser('infer', help='Predict who an image contains from a trained classifier.')inferParser.add_argument('classifierModel',type=str,help='The Python pickle representing the classifier. This is NOT the Torch network model, which can be set with --networkModel.')inferParser.add_argument('imgs', type=str, nargs='+',help="Input image.")inferParser.add_argument('--multi', help="Infer multiple faces in image",action="store_true")args = parser.parse_args()if args.verbose:print("Argument parsing and import libraries took {} seconds.".format(time.time() - start))if args.mode == 'infer' and args.classifierModel.endswith(".t7"):raise Exception("""
Torch network model passed as the classification model,
which should be a Python pickle (.pkl)See the documentation for the distinction between the Torch
network and classification models:http://cmusatyalab.github.io/openface/demo-3-classifier/http://cmusatyalab.github.io/openface/training-new-models/Use `--networkModel` to set a non-standard Torch network model.""")start = time.time()align = openface.AlignDlib(args.dlibFacePredictor)net = openface.TorchNeuralNet(args.networkModel, imgDim=args.imgDim,cuda=args.cuda)if args.verbose:print("Loading the dlib and OpenFace models took {} seconds.".format(time.time() - start))start = time.time()if args.mode == 'train':train(args)elif args.mode == 'infer':infer(args, args.multi)

6.识别人脸

./demos/classifier.py infer ./ws_represent_data/classifier.pkl ws_test_data/{test6,test7}/*.jpg


从当前测试集合的测试结果来看,准确率100%

但是这里会出现一个问题,当系统在测试集中没有检测到人脸的时候会导致无法继续预测当前路径下的下一张图片

我想对classifier.py稍作修改应该可以解决这个问题。

OpenFace使用OpenFace进行人脸识别相关推荐

  1. 看OpenFace如何做到精准人脸识别

    原文:https://blog.algorithmia.com/understanding-facial-recognition-openface/ 作者:Stephanie Kim 翻译:不二 欢迎 ...

  2. 硬核科普:一文看懂人脸识别技术流程

    小编是个天生懒惰的人,同时又是个急性子,这样的人最享受被科技服务的乐趣. 举个例子,十多年前大家还在普遍用现金的时代,小编在商店买东西排队结账,每当看到收银员找零时手忙脚乱的样子就会心急如焚,只恨不能 ...

  3. 如何通过OpenFace实现人脸识别框架

    本文中,笔者将介绍OpenFace中实现人脸识别的pipeline,这个pipeline可以看做是使用深度卷积网络处理人脸问题的一个基本框架,很有学习价值,它的结构如下图所示: Input Image ...

  4. Openface人脸识别的原理与过程

    Openface人脸识别的原理与过程: https://zhuanlan.zhihu.com/p/24567586 原理可参考如下论文: <OpenFace: A general-purpose ...

  5. 【TensorFlow】人脸识别OpenFace、Face-recognition、Insightface和FaceNet源码运行

    比较人脸识别OpenFace.Face-recognition.Insightface: FaceNet源码运行 https://github.com/davidsandberg/facenet 1. ...

  6. 使用OpenFace进行人脸识别(1)

    http://www.chinacloud.cn/show.aspx?id=25992&cid=12 本文中,笔者将介绍OpenFace中实现人脸识别的pipeline,这个pipeline可 ...

  7. docker + openface进行人脸识别(初探)

    前记 因为最近需要用到人脸识别技术,所以对openface的使用做了一个简单的测试,使用的是docker+openface+ubuntu环境,docker下安装openface比较简单,不会因为环境安 ...

  8. 人脸识别项目openface

    原文出处: https://xugaoxiang.com/2019/12/10/openface/ 软硬件环境 openface ubuntu 16.04 64bit python2 openface ...

  9. 人脸识别框架 openface学习(一) openface的安装

    1)openface 地址: 官网地址: http://cmusatyalab.github.io/openface/ Git地址 : https://github.com/cmusatyalab/o ...

  10. 将openface部署到网页中识别用户表情

    该项目实现前端调用摄像头识别用户表情,并将表情结果呈现在网页上. 功能 1.实现openface监测au,识别基本表情 2.前端调用摄像头,将图像不断传到后端处理,然后将结果传到前端 3.将openf ...

最新文章

  1. Android开发--实现对话框(AlertDialog)
  2. sscanf函数中类型不匹配警告引发的BUG和思考
  3. 单线程实现并发——协程,gevent模块
  4. Jenkins集成java非maven/ant项目的打包思路
  5. JS 数字左补零函数
  6. Win-Get——Windows下的自由软件自动安装程序
  7. [Leedcode][JAVA][第914题][最大公约数]
  8. ltp︱基于ltp的无监督信息抽取模块
  9. Even Three is Odd
  10. 期待鸿蒙是什么意思,如何看待华为将于 6月2 日举办鸿蒙发布会?你对此有哪些期待?...
  11. IOS证书/私钥/代码签名/描述文件
  12. 年夜饭之 -- 红烧黄鳝
  13. 【Gson】2.2.4 StackOverflowError 异常
  14. 切比雪夫距离(bzoj 3210: 花神的浇花集会)
  15. 怎样用c语言预测股票价格,如何根据时间和价格的波动来预测股票走势
  16. Android 车机系统 One Binary 适配白天黑夜的一个方案
  17. vue-router仿天猫底部导航栏
  18. ed302安兔兔版main.31.com.antutu.ABenchMark.fix23下载安装使用教程
  19. C语言编程单片机相关资料,单片机的C语言编程资料.ppt
  20. 【shell】网络处理:判断IP是否在网段、两个ip是否同网段、IP地址范围、网段包含关系

热门文章

  1. 如何实现异步 connect
  2. 软测学习笔记(黑马程序员)
  3. 从0-1深入了解SpringBoot特点(版本2.X)-第四天
  4. 企业内网视频会议服务器搭建有哪些技巧?
  5. 基于javaweb的在线心理测评系统设计和实现(java+springboot+ssm+mysql+jsp)
  6. 容易被忽略的罗大佑歌曲
  7. oracle最强大函数之一decode函数的使用
  8. Storm-实时计算系统
  9. python 图灵机器人_Python-微信图灵机器人
  10. css 选择器 兼容性,css选择器与属性的兼容性问题