Reputation: 51
Wrote a function to display 6 chest xray images from 2 different folders. The images are split into 2 folders contained inside 1 folder. (Training data is the main folder and within are 2 folders names 'PNEUMONUA' and 'NORMAL'.
When I run the function on pictures from one folder it works perfectly, when I use the function on the other folder I get "Index Error: list index out of range."
Code:
TRAINING_DATA = "/home/jack/Desktop/chest_xray/train/"
TEST_DATA = "/home/jack/Desktop/chest_xray/test/"
VALIDATION_DATA = "/home/jack/Desktop/chest_xray/val/"
def plot_images(path, labeled=True, max_images=6):
amount = 0
fig = plt.figure(figsize=(10, 6))
for file in os.listdir(path):
if file.endswith('.jpeg'):
if amount == max_images:
break
img = mpimg.imread(os.path.join(path, file))
plt.subplot(231+amount)
if labeled:
plt.title(file.split('_')[1])
imgplot = plt.imshow(img)
amount += 1
plot_images(TRAINING_DATA + '/NORMAL')
#ERROR
plot_images(TRAINING_DATA + '/PNEUMONIA')
#WORKS FINE
Any ideas?
Upvotes: 1
Views: 2243
Reputation: 648
Filename of the images in the 'NORMAL' folder doesn't have '_' to split (eg. IM-0115-0001.jpeg,NORMAL2-IM-0666-0001.jpeg). So you cannot split it based on '_'.
I just commented the 2 line in your code and it is working fine.
TRAINING_DATA = "<path>/chest_xray/train/"
def plot_images(path, labeled=True, max_images=6):
amount = 0
fig = plt.figure(figsize=(10, 6))
for file in os.listdir(path):
if file.endswith('.jpeg'):
if amount == max_images:
break
img = cv2.imread(os.path.join(path, file))
plt.subplot(231+amount)
plt.title("Normal")
imgplot = plt.imshow(img)
amount += 1
plot_images(TRAINING_DATA + '/NORMAL')
Hope this helps.
Upvotes: 1