【问题标题】:Loading an image file加载图像文件
【发布时间】:2020-08-01 19:33:21
【问题描述】:

任务包括以下内容: “输入图像是3个板块的集合,分别对应B、G、R通道(自上而下)。你应该实现函数????????????????_?? ??????????????? 读取数据并返回印版图像列表。???????????? _ ???????????? ?????? 是带有车牌图像的目录的路径。如果此目录与此笔记本位于同一目录中,则可以使用默认参数。"

包含要加载的图像的文件是印版。板和笔记本都在同一个文件中,称为计算机视觉中的深度学习。

这是我写的代码:

def load_data(dir_name ='C:/Users/ASUS/Desktop/Self_Learning/Coursera/Deep Learning in Computer Vision/plates'):

    im_list=[]
    for i in dir_name:
        im=np.load(i)
        im_list.append(im)
    return im_list
    pass

plates = load_data()

这是我得到的错误。

FileNotFoundError Traceback(最近一次调用最后一次) 在 8 通 9 ---> 10 个盘子 = load_data()

在 load_data(dir_name) 3 im_list=[] 4 for i in dir_name: ----> 5 im=np.load(i) 6 im_list.append(im) 7 返回im_list

~\Anaconda3\lib\site-packages\numpy\lib\npyio.py in load(file, mmap_mode, allow_pickle, fix_imports, encoding) 420 own_fid = 假 421 其他: --> 422 fid = 打开(os_fspath(文件),“rb”) 第423章 第424章

FileNotFoundError: [Errno 2] 没有这样的文件或目录:'C'

我试过这个:

import os

def load_data(dir_name ='C:/Users/ASUS/Desktop/Self_Learning/Coursera/Deep Learning in Computer Vision/plates'):
    im_list=[]
    for f in os.listdir(dir_name):
        fpath = os.path.join(dir_name, f) # this will give you the path of each file in your directory
        for im in fpath:
            im_list.append(im)

    print(im_list)

plates = load_data()

但结果不是我想要的:

我想要的是 ['1', '2', '3', '4', '5', '6'] 我基本上最终打印了每个图像的路径和图像......

【问题讨论】:

  • 看起来你正在迭代 dir_name 这是一个字符串
  • 它应该是图像的目录(包含我需要加载的图像的名为 Plates 的文件)

标签: python image numpy computer-vision anaconda


【解决方案1】:

这应该会给你你想要的

import os

def load_data(dir_name ='C:/Users/ASUS/Desktop/Self_Learning/Coursera/Deep Learning in Computer Vision/plates'):
    im_list=[]
    for f in os.listdir(dir_name):
        fpath = os.path.join(dir_name, f) # this will give you the path of each file in your directory
        do_something_with_your_file(fpath)

关于os.listdir()https://docs.python.org/3/library/os.html#os.listdir的更多信息

return 之后的 pass 语句也无效。

【讨论】:

  • 是否可以坚持使用 numpy?
  • 您使用numpy 加载文件并使用listdir 获取目录中的文件名,然后os.path.join 将为您提供文件的绝对路径(目录名+文件名)。
【解决方案2】:

您正在迭代变量dir_name,它是一个字符串。变量i 获取字符串中一个字符的值。您需要使用目录名称来获取其中的文件,如下所示。

import os

def load_data(dir_name ='C:/Users/ASUS/Desktop/Self_Learning/Coursera/Deep Learning in Computer Vision/plates'):
  im_list=[]
  for subdir, dirs, files in os.walk(dir_name):
      for file in files:
         im=np.load(file)
         im_list.append(im)
  return im_list

【讨论】:

    【解决方案3】:

    所以我尝试了这个,它成功了!

    import os
    import matplotlib.image as img
    
    def load_data(dir_name ='C:/Users/ASUS/Desktop/Self_Learning/Coursera/Deep Learning in Computer Vision/plates'):
        im_list=[]
        for f in os.listdir(dir_name):
            fpath = os.path.join(dir_name, f) # this will give you the path of each file in your directory
            im = img.imread(fpath) 
            im_list.append(im)
        return im_list
    
    
    plates = load_data()
    
    

    感谢大家的帮助。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-07-07
      • 2017-06-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-06-08
      相关资源
      最近更新 更多