标签:nim net [1] extend dom lib shape 准确率 for
大家都说gabor做人脸识别是传统方法中效果最好的,这几天就折腾实现了下,网上的python实现实在太少,github上的某个版本还误导了我好几天,后来采用将C++代码封装成dll供python调用的方式,成功解决。
图像经多尺度多方向的gabor变换后,gabor系数的数目成倍上升,所以对gabor系数必须进行降维才能送至后续的SVM分类器。测试图像使用att_faces数据集(40种类型,每种随机选5张训练,5张识别),降维方式我测试了DCT、PCA两种变换方式,说实话,dct不怎么靠谱,居然准确率不到70%,所以我有点怀疑网页 http://blog.csdn.net/bxyill/article/details/793785的实现效果,PCA方式也一般,平均识别率95%左右吧;同时测试了直接下采样、均值滤波后采样、最大值滤波后采样三种方式,它们的平均识别率分别为98.6%、98.5%、99%左右。可见,最大值滤波后再下采样的方式是最好的,其他的非线性降维方法没试过,我也不太懂
下面是python实现代码,不到50行哦
#coding:utf-8 import numpy as np import cv2, os, math, os.path, glob, random from ctypes import * from sklearn.svm import LinearSVC dll = np.ctypeslib.load_library(‘zmGabor‘, ‘.‘) #调用C++动态链接库 print dll.gabor dll.gabor.argtypes = [POINTER(c_uint8), POINTER(c_uint8), c_int32, c_int32, c_double, c_int32, c_double, c_double] 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 getGaborFeature(m): res = [] for i in range(6): for j in range(4): g = np.zeros(m.shape, dtype = np.uint8) dll.gabor(m.ctypes.data_as(POINTER(c_uint8)), g.ctypes.data_as(POINTER(c_uint8)), m.shape[0], m.shape[1], i*np.pi/6, j, 2*np.pi, np.sqrt(2)) #res.append(cv2.dct(g[:10,:10].astype(np.float))) #先DCT变换再子采样 #res.append(g[::10,::10]) #直接子采样 #res.append(cv2.blur(g, (10,10))[5::10, 5::10]) #先均值滤波再子采样 res.append(255-cv2.erode(255-g, np.ones((10,10)))[5::10, 5::10]) #先最大值滤波再子采样 return np.array(res) def main(folder=u‘D:/gabor/att_faces‘): trainImg, testImg, yTrain, yTest = loadImageSet(folder) xTrain = np.array([getGaborFeature(d).ravel() for d in trainImg]) xTest = np.array([getGaborFeature(d).ravel() for d in testImg]) lsvc = LinearSVC() #支持向量机方法 lsvc.fit(xTrain, yTrain) lsvc_y_predict = lsvc.predict(xTest) print u‘支持向量机识别率: %.2f%%‘ % (lsvc_y_predict == np.array(yTest)).mean() if __name__ == ‘__main__‘: main()
gabor变换人脸识别的python实现,att_faces数据集平均识别率99%
标签:nim net [1] extend dom lib shape 准确率 for
原文地址:http://www.cnblogs.com/zmshy2128/p/6180756.html