我的python程序中的索引错误

时间:2016-03-03 18:31:35

标签: python

这是一个使用pca逻辑进行人脸识别的程序。一切都很顺利,除了程序结束时出现的索引错误。 当我运行代码时,我在程序的第四行得到一个索引错误。

  

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

     

IndexError:列表索引超出范围

任何人都可以帮助解决这个问题。我是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 个答案:

答案 0 :(得分: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的边界。