博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
LBP人脸识别的python实现
阅读量:5151 次
发布时间:2019-06-13

本文共 4759 字,大约阅读时间需要 15 分钟。

    这几天看了看LBP及其人脸识别的流程,并在网络上搜相应的python代码,有,但代码质量不好,于是自己就重新写了下,对于att_faces数据集的识别率能达到95.0%~99.0%(40种类型,每种随机选5张训练,5张识别),全部代码如下,不到80行哦。

#coding:utf-8import numpy as npimport cv2, os, math, os.path, glob, randomg_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])<

  下面是对mnist手写数字数据集的识别,修改了数据集的载入,并加了图像的倾斜校正,识别率达到96%(如果使用sklearn的svm,效率会更高一些。)

import cPickleimport gzip,mathimport numpy as npimport os, glob, random, cv2SZ = 28def 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 imgg_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])<

  

转载于:https://www.cnblogs.com/zmshy2128/p/6150707.html

你可能感兴趣的文章
qml----Model/View入门(三)ListView分组显示
查看>>
DXP Altium Ddesigner的各种栅格(grid)意义及设置 分类: ...
查看>>
Atitit。Cas机制 软件开发 编程语言 无锁机制 java c# php
查看>>
posix信号量(sem_t)
查看>>
django数据库中
查看>>
笔记-ASP.NET WebApi
查看>>
面向对象进阶之元类
查看>>
第十九章 为什么索超到底还是输了
查看>>
PHP代码执行流程
查看>>
对MySql查询缓存及SQL Server过程缓存的理解及总结
查看>>
关于构造函数的一点理解
查看>>
.NetMVC过滤器
查看>>
HDU-1754I Hate It 线段树区间最值
查看>>
【BZOJ-1692&1640】队列变换 后缀数组 + 贪心
查看>>
Stream/Bytes[]/Image对象相互转化
查看>>
还是畅通工程(最小生成树入门基础题)
查看>>
gearman的安装与使用
查看>>
hdu3400(三分套三分)
查看>>
关于Delphi中预编译指令的使用方法
查看>>
Cisco AP-AP重置操作
查看>>