Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/image/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 索引器:列表索引超出范围-打印图像时_Python_Image_Plot_Directory_Index Error - Fatal编程技术网

Python 索引器:列表索引超出范围-打印图像时

Python 索引器:列表索引超出范围-打印图像时,python,image,plot,directory,index-error,Python,Image,Plot,Directory,Index Error,编写了一个函数来显示来自2个不同文件夹的6个胸部X光图像。图像被拆分为包含在1个文件夹中的2个文件夹。(培训数据是主文件夹,其中有两个文件夹名为“肺炎”和“正常” 当我在一个文件夹中的图片上运行该功能时,效果非常好;当我在另一个文件夹中使用该功能时,我得到“索引错误:列表索引超出范围” 代码: 有什么想法吗?普通文件夹中图像的文件名没有要拆分的“u”(例如IM-0115-0001.jpeg,NORMAL2-IM-0666-0001.jpeg)。因此不能基于“u”拆分它 我刚刚对代码中的2行进行了

编写了一个函数来显示来自2个不同文件夹的6个胸部X光图像。图像被拆分为包含在1个文件夹中的2个文件夹。(培训数据是主文件夹,其中有两个文件夹名为“肺炎”和“正常”

当我在一个文件夹中的图片上运行该功能时,效果非常好;当我在另一个文件夹中使用该功能时,我得到“索引错误:列表索引超出范围”

代码:


有什么想法吗?

普通文件夹中图像的文件名没有要拆分的“u”(例如IM-0115-0001.jpeg,NORMAL2-IM-0666-0001.jpeg)。因此不能基于“u”拆分它

我刚刚对代码中的2行进行了注释,它运行良好

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')
TRAINING_DATA=“/胸部x光/train/”
def绘图图像(路径,标记为真,最大图像数为6):
金额=0
图=plt.图(图尺寸=(10,6))
对于os.listdir(路径)中的文件:
如果文件.endswith('.jpeg'):
如果金额==最大图像数:
打破
img=cv2.imread(os.path.join(路径,文件))
plt.子批次(231+金额)
产品名称(“正常”)
imgplot=plt.imshow(img)
金额+=1
打印图像(训练数据+'/正常')

希望这能有所帮助。

回答得很好!我可以用肺炎来代替“正常”和“正常”,对其他人也有效。谢谢!问题解决了!
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')