2017-06-02 66 views
-3

我剛開始學習python。當我執行下面的代碼時,我得到一個錯誤。它告訴 回溯(最近最後一次通話): 文件 「predict_1.py」,第87行,在 主(sys.argv中[1]) IndexError:列表索引超出範圍sys.argv [1])IndexError:列表索引超出範圍

任何幫助是很大的讚賞。感謝您的閱讀!

#import modules 
import sys 
import tensorflow as tf 
from PIL import Image,ImageFilter 

def predictint(imvalue): 
    """ 
    This function returns the predicted integer. 
    The imput is the pixel values from the imageprepare() function. 
    """ 

    # Define the model (same as when creating the model file) 
    x = tf.placeholder(tf.float32, [None, 784]) 
    W = tf.Variable(tf.zeros([784, 10])) 
    b = tf.Variable(tf.zeros([10])) 
    y = tf.nn.softmax(tf.matmul(x, W) + b) 

    init_op = tf.initialize_all_variables() 
    saver = tf.train.Saver() 

    """ 
    Load the model.ckpt file 
    file is stored in the same directory as this python script is started 
    Use the model to predict the integer. Integer is returend as list. 

    Based on the documentatoin at 
    https://www.tensorflow.org/versions/master/how_tos/variables/index.html 
    """ 
    with tf.Session() as sess: 
     sess.run(init_op) 
     new_saver = tf.train.import_meta_graph('model.ckpt.meta') 
    new_saver.restore(sess, "model.ckpt") 
     #print ("Model restored.") 

     prediction=tf.argmax(y,1) 
     return prediction.eval(feed_dict={x: [imvalue]}, session=sess) 


def imageprepare(argv): 
    """ 
    This function returns the pixel values. 
    The imput is a png file location. 
    """ 
    im = Image.open(argv).convert('L') 
    width = float(im.size[0]) 
    height = float(im.size[1]) 
    newImage = Image.new('L', (28, 28), (255)) #creates white canvas of 28x28 pixels 

    if width > height: #check which dimension is bigger 
     #Width is bigger. Width becomes 20 pixels. 
     nheight = int(round((20.0/width*height),0)) #resize height according to ratio width 
     if (nheigth == 0): #rare case but minimum is 1 pixel 
      nheigth = 1 
     # resize and sharpen 
     img = im.resize((20,nheight), Image.ANTIALIAS).filter(ImageFilter.SHARPEN) 
     wtop = int(round(((28 - nheight)/2),0)) #caculate horizontal pozition 
     newImage.paste(img, (4, wtop)) #paste resized image on white canvas 
    else: 
     #Height is bigger. Heigth becomes 20 pixels. 
     nwidth = int(round((20.0/height*width),0)) #resize width according to ratio height 
     if (nwidth == 0): #rare case but minimum is 1 pixel 
      nwidth = 1 
     # resize and sharpen 
     img = im.resize((nwidth,20), Image.ANTIALIAS).filter(ImageFilter.SHARPEN) 
     wleft = int(round(((28 - nwidth)/2),0)) #caculate vertical pozition 
     newImage.paste(img, (wleft, 4)) #paste resized image on white canvas 

    #newImage.save("sample.png") 

    tv = list(newImage.getdata()) #get pixel values 

    #normalize pixels to 0 and 1. 0 is pure white, 1 is pure black. 
    tva = [ (255-x)*1.0/255.0 for x in tv] 
    return tva 
    #print(tva) 

def main(argv): 
    """ 
    Main function. 
    """ 
    imvalue = imageprepare(argv) 
    predint = predictint(imvalue) 
    print (predint[0]) #first value in list 

    if __name__ == "__main__": 
    main(sys.argv[1]) 
+2

不,_this_ code只會給縮進錯誤。發佈Python代碼時,您需要準確地重現縮進。嚴重縮減的Python代碼是無稽之談。 – khelwood

+1

請閱讀[問]並嘗試提供[mcve] - 目前您的代碼存在嚴重的縮進問題(很可能是由於格式化)。 –

+0

爲什麼我們需要查看所有代碼?它大部分與這個錯誤無關。你是如何運行腳本的?你在命令行中提供了一個合適的'imageprepare'參數字符串嗎? –

回答

0

你應該提供一個參數傳送給腳本,即這樣稱呼它

predict_1.py path_to_your_file 
0

首先,在由索引列表中的訪問項目,你總是必須檢查清單的範圍,或者趕上例外情況並處理「超出範圍」的情況(案例中的IndexError)。

其次,更好的是開始使用​​模塊。此外,還有更簡單的getopt模塊,您可以在Web,Python標準文檔等中找到許多示例。它們允許您的腳本擁有更方便的命令行參數,如Unix工具(可選,位置,默認值,等等等等)。

相關問題