简体   繁体   中英

Why am i getting this IndexError?

I have split my signature folder into train, val and test. And now I want to see two orig_train signature selected randomly and one forg_val signature. But I am getting Error. Note: I didn't use Forged signature in training (It's the part of my research, it's not something I did by mistake)

#Train-Validation-Test Split
#Signatures of 4 people are used for training
#Signatures of 2 people are used for validation
#Signatures of 2 people are used for testing

orig_train, orig_val, orig_test = orig_groups[:4], orig_groups[4:6], orig_groups[6:]
forg_val, forg_test = forg_groups[:4], forg_groups[4:]```
def visualize_sample_signature():
    '''Function to randomly select a signature from train set and
    print two genuine copies and one forged copy'''
    fig, (ax1, ax2, ax3) = plt.subplots(1, 3, figsize = (10, 10))
    k = np.random.randint(len(orig_train))
    orig_img_names = random.sample(orig_train[k], 1)
    forg_img_name = random.sample(forg_val[k], 0)
    orig_img1 = cv2.imread(orig_img_names[0], 0)
    orig_img2 = cv2.imread(orig_img_names[1], 0)
    forg_img = plt.imread(forg_img_name[0], 0)
    orig_img1 = cv2.resize(orig_img1, (img_w, img_h))
    orig_img2 = cv2.resize(orig_img2, (img_w, img_h))
    forg_img = cv2.resize(forg_img, (img_w, img_h))

    ax1.imshow(orig_img1, cmap = 'gray')
    ax2.imshow(orig_img2, cmap = 'gray')
    ax3.imshow(forg_img, cmap = 'gray')

    ax1.set_title('Genuine Copy')
    ax1.axis('off')
    ax2.set_title('Genuine Copy')
    ax2.axis('off')
    ax3.set_title('Forged Copy')
    ax3.axis('off')

visualize_sample_signature()
IndexError                                Traceback (most recent call last)
<ipython-input-113-8ca6484a3a89> in <module>
----> 1 visualize_sample_signature()

<ipython-input-112-2588ee191bd7> in visualize_sample_signature()
      7     forg_img_name = random.sample(forg_val[k], 0)
      8     orig_img1 = cv2.imread(orig_img_names[0], 0)
----> 9     orig_img2 = cv2.imread(orig_img_names[1], 0)
     10     forg_img = plt.imread(forg_img_name[0], 0)
     11     orig_img1 = cv2.resize(orig_img1, (img_w, img_h))

IndexError: list index out of range

When calling random.sample()

orig_img_names = random.sample(orig_train[k], 1)

You are only drawing one sample.

Syntax: random.sample(sequence, k)

sequence: is what you are choosing from

k: is the number of samples you are choosing

In this case you are choosing 1 sample and later in your code you are asking for the next element in orig_img_names.

orig_img2 = cv2.imread(orig_img_names[1], 0)

There is not an element at position 1; only at position 0. Therefore you get a "list index out of range."

In other words, you only have one image to choose from in orig_img_names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM