Home > Back-end >  IndexError: index 589 is out of bounds for axis 0 with size 589 in
IndexError: index 589 is out of bounds for axis 0 with size 589 in

Time:07-08

I have been given this code to experiment with. Keep getting index out of bound error, seems like an easy fix but been stuck.

Have a feeling it has to do with 3rd line of code All_Images = np.zeros((589,imgDWTsize,imgDWTsize)) what is exactly the 589 represent as a perameter?

Also the array index out of bound throws in relation to the number 589.

The code:

#trainDataFile = './own_student_id.csv'
#testDataFile = './emnist-balanced-test.csv'

imgResize = 100
imgDWTsize = 100

All_Images = np.zeros((589,imgDWTsize,imgDWTsize)) 

All_Images_Label = []
classesNames = os.listdir(PATH)

count = 0
for className in classesNames:
  classPath = os.path.join(PATH, className)
  img_names = os.listdir(classPath)
  for ImgN in img_names:
    ImgPath = os.path.join(classPath, ImgN)
    img = cv2.imread(ImgPath)
    img1 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img1 = cv2.resize(img1, (imgResize, imgResize))
    #print(img1[200,200:220])

    img1 =  np.float32(img1)   
    img1 /= 255
    
    #cA, (cH, cV, cD) = pywt.dwt2(img1, 'haar')
    #cA, (cH, cV, cD) =  pywt.dwt2(cA, 'haar')
    #cA, (cH, cV, cD) =  pywt.dwt2(cA, 'haar')
    #NewImg3 =  w2d(NewImg2, mode='haar')
    #cA *= 255
    NewImg3 = img1 #np.uint8(cA)
    #print(NewImg3[100,100:120])
    #exit(0)
    #exit(0)
    #All_Images.append(np.array(img1))

    All_Images[count,:,:] = NewImg3 ### ERROR IS BEING THROWN HERE ###

    Label = int(className) - 1
    All_Images_Label.append(Label)
    #img1Dlst= [0 if x<16 else x for x in img1Dlst]
    
    #Letter1D = [Label]    img1Dlst
    
    count  =1
    if count % 50 == 0:
        print(count)
All_Images[0].shape
print('X_data shape:', np.array(All_Images).shape)
print(All_Images[25,25:50])

CodePudding user response:

Your code is iterating through two nested. One is for directories and second one is images that is found in directory. Since your All_Images 3D array has size of 589 in first axis, it can only iterate until index of 588. If you have more than 588 images in whole directories you will get this error. I'd check the amount of images you have in that directory and adjust fixed size of All_Images 3D array.

CodePudding user response:

Your array 1st dimension (589) is smaller that the total number of files (in your directories) that you need to store in it, and when it gets full, it goes out of bounds. You have to ensure the size is large enough, so all the files fit.

Example:

img_dwt_size = 100

files_count = sum(1 for cls_dir in os.listdir(path) for _ in os.listdir(os.path.join(path, cls_dir)))  # !!! Also replaced PATH by path !!!

all_images = np.zeros((files_count, img_dwt_size, img_dwt_size))

That would create a vector which would match the number of files.
Drawbacks:

  • It's inefficient - as it traverses the directory structure twice

  • If PATH structure changes, you might get in trouble (if files are added while program is running, you'll end up in this situation again)

As a side note, watch the coding style. Try using names that are [Python.PEPs]: PEP 8 - Style Guide for Python Code compliant, otherwise it's harder to follow (for you and others), and it just looks ugly.

  • Related