@Metralix
2019-08-22T23:06:59.000000Z
字数 10542
阅读 718
人脸识别
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 13 21:08:18 2019
@author: 54164
"""
import numpy as np
import matplotlib.pyplot as plt
import time
import LRA_GL_utils as utils
import data_pre_process as dprocess
time_start=time.time()
#train_vis,train_nir,test_vis,test_nir =utils.readPicture(folder_path="./dataset_CASIA")
#train_vis = np.load('train_vis.npy')
#train_nir = np.load('train_nir.npy')
#test_vis = np.load('test_vis.npy')
#test_nir = np.load('test_nir.npy')
#pre process the pictures
#dprocess.cut_face_into_folder(train_vis,"train_vis_face")
#dprocess.cut_face_into_folder(train_nir,"train_nir_face")
#dprocess.cut_face_into_folder(test_vis,"test_vis_face")
#dprocess.cut_face_into_folder(test_nir,"test_nir_face")
train_vis_face,train_nir_face,test_vis_face,test_nir_face = dprocess.reload_new_faces()
dprocess.rename_pictures()
k = test_nir_face.shape[0]
m = train_nir_face.shape[0]
Y_gl = np.hstack((np.eye(k),np.zeros([k,m])))
#Y_gl = np.hstack((np.eye(k),np.zeros([k,1000])))
probe = utils.lbp_encode(test_nir_face)
print("encoded test_nir !!!")
X = utils.lbp_encode(test_vis_face)
print("encoded test_vis !!!")
variants = utils.intro_class_variant(train_vis_face,train_nir_face)
print("extracted the varients !!!")
variants_lbp = utils.lbp_encode(variants)
print("encoded varients !!!")
X_gl = np.hstack((X,variants_lbp))
#X_gl = np.hstack((X,variants_lbp[:,0:1000]))
X_gl_mat = np.mat(X_gl)
X_gl_inv = X_gl_mat.I
X_gl_inv = np.array(X_gl_inv)
W_gl = np.dot(Y_gl,X_gl_inv)
Y_gl_hat = np.dot(W_gl,probe)
accuracy = utils.calculate_accuracy(Y_gl_hat)
accuracy_top5 = utils.calculate_accuracy_top5(Y_gl_hat)
time_end=time.time()
print('totally cost : ',time_end-time_start)
功能:
读入数据集,lbp编码,计算类内变化基,计算基准度
# -*- coding: utf-8 -*-
"""
Created on Fri Aug 9 21:00:10 2019
@author: haoyang li
"""
import numpy as np
import math
import matplotlib.pyplot as plt
import matplotlib.image as mpimg
import os
import cv2
"""
function:read the picture from the os folder
input:folder path
output:(train_vis,train_nir,gallary,probe) in the format of matrix
"""
def readPicture(folder_path=r"C:\Users\54164\Desktop\LRA_GL\dataset_CASIA"):
#读取 train_vis 训练的彩色照片,并转化为灰度图
train_vis = []
for i in range(30840):
train_vis_path = os.path.join(folder_path,'train_vis','{0:05d}.jpg'.format(i+1))
if os.path.exists(train_vis_path):
#I = mpimg.imread(train_vis_path)
I = cv2.imread(train_vis_path,cv2.IMREAD_GRAYSCALE)
train_vis.append(I)
print("readed train_vis")
#读取 train_nir 训练的黑白照片
train_nir = []
for i in range(30840):
suffix = ['a','b','c','d','e']
path_a = os.path.join(folder_path,'train_nir','{0:05d}_a.jpg'.format(i+1))
if os.path.exists(path_a):
for c in suffix:
path_all = os.path.join(folder_path,'train_nir','{0:05d}_{1}.jpg'.format(i+1,c))
#I = mpimg.imread(path_all)
I = cv2.imread(path_all,cv2.IMREAD_GRAYSCALE)
train_nir.append(I)
print("readed train_nir")
#读取 test_vis gallary 测试的彩照
test_vis = []
for i in range(30840):
test_vis_path = os.path.join(folder_path,'test_vis','{0:05d}.jpg'.format(i+1))
if os.path.exists(test_vis_path):
#I = mpimg.imread(test_vis_path)
I = cv2.imread(test_vis_path,cv2.IMREAD_GRAYSCALE)
test_vis.append(I)
print("readed test_vis")
#读取 test_nir probe 测试的黑白照
test_nir = []
for i in range(30840):
test_nir_path = os.path.join(folder_path,'test_nir','{0:05d}.jpg'.format(i+1))
if os.path.exists(test_nir_path):
#I = mpimg.imread(test_nir_path)
I = cv2.imread(test_nir_path,cv2.IMREAD_GRAYSCALE)
test_nir.append(I)
print("readed test_nir")
return np.array(train_vis),np.array(train_nir),np.array(test_vis),np.array(test_nir)
"""
lbp encode the picture set
input:picture sets of 2d or 3d reality photos
output:lbp code set of these pictures
"""
def lbp_encode(picture_set,cell_size=20):
for i in range(picture_set.shape[0]):
if i % 50 == 0:
print("read set's no. {0} pictures".format(i))
hist = np.zeros(256*cell_size*cell_size)
hist_idx = 0
pic = picture_set[i,:,:].copy()
height = math.floor(pic.shape[0]/cell_size) #20
width = math.floor(pic.shape[1]/cell_size) #20
for cell_x in range(cell_size): #0-10
for cell_y in range(cell_size):
for x in range(width):
x = x + cell_x * width
for y in range(height):
y = y + cell_y * height
if x-2>=0 and y-2>=0 and x+2<pic.shape[1] and y+2<pic.shape[0]: #边界值忽略
code = []
code.append(pic[y-1,x-1]>pic[y,x])
code.append(pic[y,x-1]>pic[y,x])
code.append(pic[y+1,x-1]>pic[y,x])
code.append(pic[y+1,x]>pic[y,x])
code.append(pic[y+1,x+1]>pic[y,x])
code.append(pic[y,x+1]>pic[y,x])
code.append(pic[y-1,x+1]>pic[y,x])
code.append(pic[y-1,x]>pic[y,x])
code = np.array(code)
code = code + 0
code = bin2oct(code)
hist[code + hist_idx*256] += 1
hist_idx += 1
hist = hist/(height*width)
hist = hist.reshape([256*cell_size*cell_size,1])
# normalized to zeros mean and unit length.
hist = hist - np.mean(hist)
sum_square = math.sqrt(np.sum(hist**2))
hist = hist/sum_square
if i == 0:
coverted_matrix = hist.copy()
else:
coverted_matrix = np.hstack((coverted_matrix,hist.copy()))
return coverted_matrix
"""
calculate the intro_class variants
input:the train_vis and trian_nir datasets
output:get the intro_class variant of the trian datasets
the out size is the same as a 840*480 gary picture
"""
def intro_class_variant(vis_pics,nir_pics):
variant_set = []
for i in range(nir_pics.shape[0]):
pic = nir_pics[i,:,:]-vis_pics[i//5,:,:]
#pic = vis_pics[i//5,:,:] - nir_pics[i,:,:]
variant_set.append(pic.copy())
return np.array(variant_set)
"""
input:result matrix of the algorithm
output:calculate the accuracy of the model
"""
def calculate_accuracy(res_matrix):
res_matrix = res_matrix + 10
max_list = np.max(res_matrix,axis=0)
for i in range(res_matrix.shape[1]):
res_matrix[:,i] = res_matrix[:,i]-max_list[i]+1e-5
res_matrix = res_matrix >= 0
correct_cnt = 0
for i in range(res_matrix.shape[1]):
if res_matrix[i,i] == 1:
correct_cnt += 1
return correct_cnt / res_matrix.shape[1]
"""
input:result matrix of the algorithm
output:calculate the accuracy_top5 of the model
"""
def calculate_accuracy_top5(res_matrix):
res_matrix = res_matrix + 10
#max_list = np.max(res_matrix,axis=0)
max5_list = []
for i in range(res_matrix.shape[1]):
cow_list = res_matrix[:,i].copy()
cow_list.sort()
max5_list.append(cow_list[res_matrix.shape[0]-5])
for i in range(res_matrix.shape[1]):
res_matrix[:,i] = res_matrix[:,i]-max5_list[i]+1e-5
res_matrix = res_matrix >= 0
correct_cnt = 0
for i in range(res_matrix.shape[1]):
if res_matrix[i,i] == 1:
correct_cnt += 1
else:
print("no.{}th pic is wrong!!!".format(i))
return correct_cnt / res_matrix.shape[1]
def calculate_accuracy_top10(res_matrix):
res_matrix = res_matrix + 10
#max_list = np.max(res_matrix,axis=0)
max5_list = []
for i in range(res_matrix.shape[1]):
cow_list = res_matrix[:,i].copy()
cow_list.sort()
max5_list.append(cow_list[res_matrix.shape[0]-10])
for i in range(res_matrix.shape[1]):
res_matrix[:,i] = res_matrix[:,i]-max5_list[i]+1e-5
res_matrix = res_matrix >= 0
correct_cnt = 0
for i in range(res_matrix.shape[1]):
if res_matrix[i,i] == 1:
correct_cnt += 1
return correct_cnt / res_matrix.shape[1]
"""
transform the RGB pattern into gray
"""
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
"""
transform the bin array into an oct number
"""
def bin2oct(bin_list):
summ = 0
for i in range(len(bin_list)):
summ += int(bin_list[len(bin_list)-1-i])*pow(2,i)
return summ
功能
从数据集中提取人脸写入另一个文件夹,从新读取人脸数据
# -*- coding: utf-8 -*-
"""
Created on Tue Aug 13 20:56:28 2019
@author: 54164
"""
import cv2
import os
import numpy as np
import matplotlib.pyplot as plt
from shutil import copyfile
def cut_face_into_folder(data_set,folder_name):
face_cascade = cv2.CascadeClassifier('haarcascade_frontalface_default.xml')
face_set = []
for i in range(data_set.shape[0]):
img = np.array(data_set[i,:,:],dtype='uint8')
face = face_cascade.detectMultiScale(img, scaleFactor=1.1, minNeighbors=15, minSize=(150, 150), flags=cv2.CASCADE_SCALE_IMAGE)
if face != ():
print("readed {0} faces".format(i))
x = face[0,0]
y = face[0,1]
w = face[0,2]
h = face[0,3]
#face_set.append(img[y:y+h,x:x+w].copy())
img_new = cv2.resize(img[y:y+h,x:x+w].copy(), (200,200), interpolation = cv2.INTER_AREA )
img_new = cv2.resize(img_new[10:190,25:175], (150,150), interpolation = cv2.INTER_AREA )
cv2.imwrite("./{0}/{1}.jpg".format(folder_name,i),img_new)
print("saved {0} faces in {1}".format(i,folder_name))
else:
print("Ooops!!!!!!!!!")
def reload_new_faces(train_vis_path="./face\train_vis_face",
train_nir_path="./face\train_nir_face",
test_vis_path="./face\test_vis_face",
test_nir_path="./face\test_nir_face",):
# test_nir and test_vis
test_nir = []
test_vis = []
for i in range(360):
test_nir_path_all = os.path.join(test_nir_path,"{0}.jpg".format(i))
test_vis_path_all = os.path.join(test_vis_path,"{0}.jpg".format(i))
if os.path.exists(test_nir_path_all) and os.path.exists(test_vis_path_all):
test_nir.append(cv2.imread(test_nir_path_all,cv2.IMREAD_GRAYSCALE))
test_vis.append(cv2.imread(test_vis_path_all,cv2.IMREAD_GRAYSCALE))
elif os.path.exists(test_nir_path_all):
os.remove(test_nir_path_all)
elif os.path.exists(test_vis_path_all):
os.remove(test_vis_path_all)
# train_vis and train_nir
train_vis = []
train_nir = []
for i in range(360):
train_vis_path_all = os.path.join(train_vis_path,"{0}.jpg".format(i))
if os.path.exists(train_vis_path_all):
nir_all_exist = True
for j in range(5):
if os.path.exists(os.path.join(train_nir_path,"{0}.jpg".format(i*5+j))) == False:
nir_all_exist = False
if nir_all_exist:
for j in range(5):
train_nir_path_all = os.path.join(train_nir_path,"{0}.jpg".format(i*5+j))
train_nir.append(cv2.imread(train_nir_path_all,cv2.IMREAD_GRAYSCALE))
train_vis.append(cv2.imread(train_vis_path_all,cv2.IMREAD_GRAYSCALE))
else:
for j in range(5):
train_nir_path_all = os.path.join(train_nir_path,"{0}.jpg".format(i*5+j))
if os.path.exists(train_nir_path_all):
os.remove(train_nir_path_all)
os.remove(train_vis_path_all)
return np.array(train_vis),np.array(train_nir),np.array(test_vis),np.array(test_nir)
def rename_pictures(train_vis_path="./train_vis_face",
train_nir_path="./train_nir_face",
test_vis_path="./test_vis_face",
test_nir_path="./test_nir_face",):
# rename
if os.path.exists(os.path.join(train_vis_path,"new")) == False:
os.mkdir(os.path.join(train_vis_path,"new"))
idx = 0
for i in range(360):
train_vis_path_all = os.path.join(train_vis_path,"{0}.jpg".format(i))
if os.path.exists(train_vis_path_all):
copyfile(train_vis_path_all,os.path.join(train_vis_path,"new","{0}.jpg".format(idx)))
idx += 1
if os.path.exists(os.path.join(train_nir_path,"new")) == False:
os.mkdir(os.path.join(train_nir_path,"new"))
idx = 0
for i in range(360):
train_nir_path_all = os.path.join(train_nir_path,"{0}.jpg".format(i))
if os.path.exists(train_nir_path_all):
copyfile(train_nir_path_all,os.path.join(train_nir_path,"new","{0}.jpg".format(idx)))
idx += 1
if os.path.exists(os.path.join(test_vis_path,"new")) == False:
os.mkdir(os.path.join(test_vis_path,"new"))
idx = 0
for i in range(360):
test_vis_path_all = os.path.join(test_vis_path,"{0}.jpg".format(i))
if os.path.exists(test_vis_path_all):
copyfile(test_vis_path_all,os.path.join(test_vis_path,"new","{0}.jpg".format(idx)))
idx += 1
if os.path.exists(os.path.join(test_nir_path,"new")) == False:
os.mkdir(os.path.join(test_nir_path,"new"))
idx = 0
for i in range(360):
test_nir_path_all = os.path.join(test_nir_path,"{0}.jpg".format(i))
if os.path.exists(test_nir_path_all):
copyfile(test_nir_path_all,os.path.join(test_nir_path,"new","{0}.jpg".format(idx)))
idx += 1
print("rename finished!!")