2016-03-03 29 views
1

這是一個使用pca邏輯進行人臉識別的程序。除了程序結束時出現的索引錯誤,一切都很順利。 當我運行代碼時,我的程序的最後第四行出現索引錯誤。我的python程序中的索引錯誤

distances.append((dist, y[i]))

IndexError: list index out of range

任何人都可以在此幫忙。我是Python新手,所以我不擅長解決。

這裏是我的代碼:

from sklearn.decomposition import RandomizedPCA 
import numpy as np 
import glob 
import cv2 
import math 
import os.path 
import string 

#function to get ID from filename 
def ID_from_filename(filename): 
    part = string.split(filename, '/') 
    return part[1].replace("s", "") 

#function to convert image to right format 
def prepare_image(filename): 
    img_color = cv2.imread(filename) 
    img_gray = cv2.cvtColor(img_color, cv2.cv.CV_RGB2GRAY) 
    img_gray = cv2.equalizeHist(img_gray) 
    return img_gray.flat 

IMG_RES = 92 * 112 # img resolution 
NUM_EIGENFACES = 10 # images per train person 
NUM_TRAINIMAGES = 110 # total images in training set 

#loading training set from folder train_faces 
folders = glob.glob('train_faces/*') 

# Create an array with flattened images X 
# and an array with ID of the people on each image y 
X = np.zeros([NUM_TRAINIMAGES, IMG_RES], dtype='int8') 
y = [] 

# Populate training array with flattened imags from subfolders of                
train_faces and names 
c = 0 
for x, folder in enumerate(folders): 
    train_faces = glob.glob(folder + '/*') 
    for i, face in enumerate(train_faces): 
     X[c,:] = prepare_image(face) 
     y.append(ID_from_filename(face)) 
     c = c + 1 

# perform principal component analysis on the images 
pca = RandomizedPCA(n_components=NUM_EIGENFACES, whiten=True).fit(X) 
X_pca = pca.transform(X) 

# load test faces (usually one), located in folder test_faces 
test_faces = glob.glob('test_faces/*') 

# Create an array with flattened images X 
X = np.zeros([len(test_faces), IMG_RES], dtype='int8') 

# Populate test array with flattened imags from subfolders of train_faces 
for i, face in enumerate(test_faces): 
    X[i,:] = prepare_image(face) 

# run through test images (usually one) 
for j, ref_pca in enumerate(pca.transform(X)): 
    distances = [] 
    # Calculate euclidian distance from test image to each of the known  
     images and save distances 
    for i, test_pca in enumerate(X_pca): 
     dist = math.sqrt(sum([diff**2 for diff in (ref_pca - test_pca)])) 
     distances.append((dist, y[i])) 

    found_ID = min(distances)[1] 

    print "Identified (result: "+ str(found_ID) +" - dist - " + 
    str(min(distances)[0]) + ")" 
+1

它可能來自'y [i]'位。 'y'不夠大,無法訪問你所要求的索引('i')。 – Kupiakos

+0

如果'X_pca'具有比'y'更多的元素,就會發生這種情況。 – Barmar

回答

0

i在下面的循環上升到X_pca的長度 - 1

for i, test_pca in enumerate(X_pca): 
    dist = math.sqrt(sum([diff**2 for diff in (ref_pca - test_pca)])) 
    distances.append((dist, y[i])) 

但是,你Y不是建有長度不一定:

for x, folder in enumerate(folders): 
    train_faces = glob.glob(folder + '/*') 
    for i, face in enumerate(train_faces): 
     X[c,:] = prepare_image(face) 
     y.append(ID_from_filename(face)) 

因此,您使用的索引i大於列表y的範圍。

+0

是的,沒錯。這本身一定是理性的。所以我想增加清單的大小將解決我的問題。你能告訴我一些如何消除這個錯誤的方法嗎? – paps

+0

你的邏輯不正確,你需要檢查你的數學表達式。 – DevShark