LBP人脸识别的python实现
这几天看了看LBP及其人脸识别的流程,并在网络上搜相应的python代码,有,但代码质量不好,于是自己就重新写了下,对于att_faces数据集的识别率能达到95.0%~99.0%(40种类型,每种随机选5张训练,5张识别),全部代码如下,不到80行哦。
#coding:utf-8
import numpy as np
import cv2, os, math, os.path, glob, random g_mapping=[
0, 1, 2, 3, 4, 58, 5, 6, 7, 58, 58, 58, 8, 58, 9, 10,
11, 58, 58, 58, 58, 58, 58, 58, 12, 58, 58, 58, 13, 58, 14, 15,
16, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
17, 58, 58, 58, 58, 58, 58, 58, 18, 58, 58, 58, 19, 58, 20, 21,
22, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
23, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
24, 58, 58, 58, 58, 58, 58, 58, 25, 58, 58, 58, 26, 58, 27, 28,
29, 30, 58, 31, 58, 58, 58, 32, 58, 58, 58, 58, 58, 58, 58, 33,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 34,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 35,
36, 37, 58, 38, 58, 58, 58, 39, 58, 58, 58, 58, 58, 58, 58, 40,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 41,
42, 43, 58, 44, 58, 58, 58, 45, 58, 58, 58, 58, 58, 58, 58, 46,
47, 48, 58, 49, 58, 58, 58, 50, 51, 52, 58, 53, 54, 55, 56, 57] def loadImageSet(folder, sampleCount=5):
trainData = []; testData = []; yTrain=[]; yTest = [];
for k in range(1,41):
folder2 = os.path.join(folder, 's%d' %k)
data = [cv2.imread(d.encode('gbk'),0) for d in glob.glob(os.path.join(folder2, '*.pgm'))]
sample = random.sample(range(10), sampleCount)
trainData.extend([data[i] for i in range(10) if i in sample])
testData.extend([data[i] for i in range(10) if i not in sample])
yTest.extend([k]* (10-sampleCount))
yTrain.extend([k]* sampleCount)
return trainData, testData, np.array(yTrain), np.array(yTest) def LBP(I, radius=2, count=8): #得到图像的LBP特征
dh = np.round([radius*math.sin(i*2*math.pi/count) for i in range(count)])
dw = np.round([radius*math.cos(i*2*math.pi/count) for i in range(count)]) height ,width = I.shape
lbp = np.zeros(I.shape, dtype = np.int)
I1 = np.pad(I, radius, 'edge')
for k in range(count):
h,w = radius+dh[k], radius+dw[k]
lbp += ((I>I1[h:h+height, w:w+width])<<k)
return lbp def calLbpHistogram(lbp, hCount=7, wCount=5, maxLbpValue=255): #分块计算lbp直方图
height,width = lbp.shape
res = np.zeros((hCount*wCount, max(g_mapping)+1), dtype=np.float)
assert(maxLbpValue+1 == len(g_mapping)) for h in range(hCount):
for w in range(wCount):
blk = lbp[height*h/hCount:height*(h+1)/hCount, width*w/wCount:width*(w+1)/wCount]
hist1 = np.bincount(blk.ravel(), minlength=maxLbpValue) hist = res[h*wCount+w,:]
for v,k in zip(hist1, g_mapping):
hist[k] += v
hist /= hist.sum()
return res def main(folder=u'E:/迅雷下载/faceProcess/att_faces'):
trainImg, testImg, yTrain, yTest = loadImageSet(folder) xTrain = np.array([calLbpHistogram(LBP(d)).ravel() for d in trainImg])
xTest = np.array([calLbpHistogram(LBP(d)).ravel() for d in testImg]) lsvc = cv2.SVM() #支持向量机方法
svm_params = dict( kernel_type = cv2.SVM_LINEAR, svm_type = cv2.SVM_C_SVC, C=2.67, gamma=5.383 )
lsvc.train(np.float32(xTrain), np.float32(yTrain), params = svm_params)
lsvc_y_predict = np.array( [lsvc.predict(d) for d in np.float32(xTest)])
print u'支持向量机识别率', (lsvc_y_predict == np.array(yTest)).mean() if __name__ == '__main__':
main()
下面是对mnist手写数字数据集的识别,修改了数据集的载入,并加了图像的倾斜校正,识别率达到96%(如果使用sklearn的svm,效率会更高一些。)
import cPickle
import gzip,math
import numpy as np
import os, glob, random, cv2 SZ = 28
def deskew(img):
m = cv2.moments(img)
if abs(m['mu02']) < 1e-2:
return img.copy()
skew = m['mu11']/m['mu02']
M = np.float32([[1, skew, -0.5*SZ*skew], [0, 1, 0]])
img = cv2.warpAffine(img,M,(SZ, SZ),flags=cv2.WARP_INVERSE_MAP|cv2.INTER_LINEAR)
return img g_mapping=[
0, 1, 2, 3, 4, 58, 5, 6, 7, 58, 58, 58, 8, 58, 9, 10,
11, 58, 58, 58, 58, 58, 58, 58, 12, 58, 58, 58, 13, 58, 14, 15,
16, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
17, 58, 58, 58, 58, 58, 58, 58, 18, 58, 58, 58, 19, 58, 20, 21,
22, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
23, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
24, 58, 58, 58, 58, 58, 58, 58, 25, 58, 58, 58, 26, 58, 27, 28,
29, 30, 58, 31, 58, 58, 58, 32, 58, 58, 58, 58, 58, 58, 58, 33,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 34,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 35,
36, 37, 58, 38, 58, 58, 58, 39, 58, 58, 58, 58, 58, 58, 58, 40,
58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 58, 41,
42, 43, 58, 44, 58, 58, 58, 45, 58, 58, 58, 58, 58, 58, 58, 46,
47, 48, 58, 49, 58, 58, 58, 50, 51, 52, 58, 53, 54, 55, 56, 57] def loadImageSet():
with gzip.open('./mnist.pkl.gz') as fp:
train_set, valid_set, test_set = cPickle.load(fp) xTrain = train_set[0]; s1 = xTrain.shape; xTrain = xTrain.reshape((s1[0],28,28))
xTest = test_set[0]; s2 = xTest.shape; xTest = xTest.reshape((s2[0],28,28))
xTrain = np.array([deskew(d) for d in xTrain])
xTest = np.array([deskew(d) for d in xTest])
return xTrain, xTest, train_set[1], test_set[1] def LBP(I, radius=2, count=8): #得到图像的LBP特征
dh = np.round([radius*math.sin(i*2*math.pi/count) for i in range(count)])
dw = np.round([radius*math.cos(i*2*math.pi/count) for i in range(count)]) height ,width = I.shape
lbp = np.zeros(I.shape, dtype = np.int)
I1 = np.pad(I, radius, 'edge')
for k in range(count):
h,w = radius+dh[k], radius+dw[k]
lbp += ((I>I1[h:h+height, w:w+width])<<k)
return lbp def calLbpHistogram(lbp, hCount=2, wCount=2, maxLbpValue=255): #分块计算lbp直方图
height,width = lbp.shape
res = np.zeros((hCount*wCount, max(g_mapping)+1), dtype=np.float)
assert(maxLbpValue+1 == len(g_mapping)) for h in range(hCount):
for w in range(wCount):
blk = lbp[height*h/hCount:height*(h+1)/hCount, width*w/wCount:width*(w+1)/wCount]
hist1 = np.bincount(blk.ravel(), minlength=maxLbpValue) hist = res[h*wCount+w,:]
for v,k in zip(hist1, g_mapping):
hist[k] += v
hist /= hist.sum()
return res def main():
trainImg, testImg, yTrain, yTest = loadImageSet() xTrain = np.array([calLbpHistogram(LBP(d)).ravel() for d in trainImg])
xTest = np.array([calLbpHistogram(LBP(d)).ravel() for d in testImg]) lsvc = cv2.SVM() #支持向量机方法
svm_params = dict( kernel_type = cv2.SVM_LINEAR, svm_type = cv2.SVM_C_SVC, C=2.67, gamma=5.383 )
lsvc.train(np.float32(xTrain), np.float32(yTrain), params = svm_params)
lsvc_y_predict = np.array( [lsvc.predict(d) for d in np.float32(xTest)])
print u'支持向量机', (lsvc_y_predict == np.array(yTest)).mean() if __name__ == '__main__':
main()
LBP人脸识别的python实现的更多相关文章
- gabor变换人脸识别的python实现,att_faces数据集平均识别率99%
大家都说gabor做人脸识别是传统方法中效果最好的,这几天就折腾实现了下,网上的python实现实在太少,github上的某个版本还误导了我好几天,后来采用将C++代码封装成dll供python调用的 ...
- PCA人脸识别的python实现
这几天看了看PCA及其人脸识别的流程,并在网络上搜相应的python代码,有,但代码质量不好,于是自己就重新写了下,对于att_faces数据集的识别率能达到92.5%~98.0%(40种类型,每种随 ...
- iOS活体人脸识别的Demo和一些思路
代码地址如下:http://www.demodashi.com/demo/12011.html 之前公司项目需要,研究了一下人脸识别和活体识别,并运用免费的讯飞人脸识别,在其基础上做了二次开发,添加了 ...
- 人脸检测? 对Python来说太简单, 调用dlib包就可以完成
"Dlib 是一个现代化的 C ++ 工具包,包含用于创建复杂软件的机器学习算法和工具 " .它使您能够直接在 Python 中运行许多任务,其中一个例子就是人脸检测. 安装 dl ...
- 百度Aip人脸识别之python代码
用python来做人脸识别代码量少 思路清晰, 在使用之前我们需要在我们的配置的编译器中通过pip install baidu-aip 即可 from aip import AipFace 就可以开 ...
- 人脸识别之Python DLib库进行人脸关键点识别
一.首先安装DLib模块 这里只介绍linux安装的过程,windows安装过程请自行百度 1.首先,安装dlib.skimage前:先安装libboost sudo apt-get install ...
- 转《在浏览器中使用tensorflow.js进行人脸识别的JavaScript API》
作者 | Vincent Mühle 编译 | 姗姗 出品 | 人工智能头条(公众号ID:AI_Thinker) [导读]随着深度学习方法的应用,浏览器调用人脸识别技术已经得到了更广泛的应用与提升.在 ...
- face_recognition 人脸识别报错
[root@localhost examples]# python facerec_from_video_file.py RuntimeError: module compiled against A ...
- face-api.js:一个在浏览器中进行人脸识别的 JavaScript 接口
Mark! 本文将为大家介绍一个建立在「tensorflow.js」内核上的 javascript API——「face-api.js」,它实现了三种卷积神经网络架构,用于完成人脸检测.识别和特征点检 ...
随机推荐
- wxpython 窗口排版- proportion/flag/border参数说明
新学习wxpython,一直纠结于窗口控件的排版,经过几天的查资料.试验,总结如下. 1.需求实例 来个实例,窗口有3行控件 第一行是文本提示(大小不变,文字左对齐,控件居左). 第二行依次为文本提示 ...
- Linux 文件的详解[分类/扩展名/inode/block]
关于Linux文件的介绍 Linux里文件扩展名和文件类型没有关系,Linux系统中一切皆文件 关于Linux文件分类 纯文本文件(可以cat的) 二进制文件(Linux的可执行文件等,如/b ...
- Linux 系统必须掌握的文件_【all】
0.Linux 系统文件的详解 1.Linux 系统的网络配置文件 2.Linux 系统的DNS配置文件 3.Linux 系统的IP与域名解析文件[局域网的DNS] 4.Linux 系统的主机别名文件 ...
- zabbix 监控iptables
参看的文章链接忘了...... yum -y install iptstate 1.脚本位置和内容 [root@web1 scripts]# pwd /etc/zabbix/scripts [root ...
- Ceph PG介绍及故障状态和修复
1 PG介绍pg的全称是placement group,中文译为放置组,是用于放置object的一个载体,pg的创建是在创建ceph存储池的时候指定的,同时跟指定的副本数也有关系,比如是3副本的则会有 ...
- linux下添加用户到sudo组 并禁止sudo用户修改密码
linux下添加用户到sudo组 创建用户 useradd hanli 为新用户设置密码 passwd hanli 创建用户组 groupadd op 将用户添加到用户组 usermod - ...
- Git rebase日志
Git日志重写 为了方便管理,最近公司git接了jira,然后开发任务需要在jira上面先建立task,然后再task上面建立分支,后面该分支就和这个task进行了绑定. 因为之前一直使用传统的svn ...
- 浏览器地址栏运行JavaScript代码
这个很多人应该还是知道的,在浏览器地址栏可以直接运行JavaScript代码,做法是以javascript:开头后跟要执行的语句.比如: javascript:alert('hello from ad ...
- 多启动引导工具——AIO Boot
该软件功能十分强大 官网介绍的也十分详尽 这里仅仅简单标记一下用来以后查找 https://www.aioboot.com/en/ 效果图: 支持多语言: 简单运用:
- linq to sql 中增删改查
首先我先说一下,如果真的要用linq做项目的话,也会是比较方便的.已经尝试了在三层架构中应用linq to sql 比较方便. //有三个不同的数据库表,所以写法不一样 public class Li ...