【问题标题】:Google Colab reading files from colabdisk instead of uploadGoogle Colab 从 colabdisk 读取文件而不是上传
【发布时间】:2019-11-26 16:26:41
【问题描述】:

我有一个函数只能与 colab.upload 一起使用,而不是从 colab 磁盘上的文件中传递一个文件名,我是从 Drive 上传的。 我想为 TensorFlow 对象检测 API 使用 jpg 图片。

这是有效的:

uploaded = files.upload()
img_infer = list(uploaded)[0]

print('running test on: ' + img_infer)
img_inference(img_infer)

但不是这个:

for file in os.listdir("/content/test"):
    print('running test on: ' + file)
    img_infer = list(file)[0]
    img_inference(img_infer)

错误:

---------------------------------------------------------------------------

FileNotFoundError                         Traceback (most recent call last)

<ipython-input-31-4fbcedf7108a> in <module>()
     12   print('Teste mit Bild: ' + file)
     13   img_infer = list(file)[0]
---> 14   img_inference(img_infer)

2 frames

<ipython-input-8-6a0800ccaf6f> in img_inference(img_path)
     50 
     51 def img_inference(img_path):
---> 52   image = read_image_bgr(img_infer)
     53 
     54   # copy to draw on

/content/keras-retinanet/keras_retinanet/utils/image.py in read_image_bgr(path)
     30     """
     31     # We deliberately don't use cv2.imread here, since it gives no feedback on errors while reading the image.
---> 32     image = np.asarray(Image.open(path).convert('RGB'))
     33     return image[:, :, ::-1].copy()
     34 

/usr/local/lib/python3.6/dist-packages/PIL/Image.py in open(fp, mode)
   2528 
   2529     if filename:
-> 2530         fp = builtins.open(filename, "rb")
   2531         exclusive_fp = True
   2532 

FileNotFoundError: [Errno 2] No such file or directory: 'I'

【问题讨论】:

    标签: python image object-detection google-colaboratory object-detection-api


    【解决方案1】:

    你的代码有逻辑问题:

    for file in os.listdir("/content/test"):
      print('running test on: ' + file)
      img_infer = list(file)[0] # <== This line is not correct
      img_inference(img_infer)
    

    您必须将img_infer = list(file)[0] 更改为img_infer = file,或者只需删除img_infer = list(file)[0] 并将img_inference(img_infer) 更改为img_inference(file)

    换句话说:

    for file in os.listdir("/content/test"):
      print('running test on: ' + file)
      img_inference(file)
    

    为了进一步解释原始代码中发生的情况,假设 /content/test 包含 aaa.txtbbb.pyccc.jpg。那么:

    for file in os.listdir("/content/test"):
      # In the first iteration file == "aaa.txt"
      print('running test on: ' + file)
      # But then list(file) == ["a", "a", "a", ".", "t", "x", "t"] which is not what you want.
      # And then list(file)[0] == "a", therefore img_infer == "a"
      img_infer = list(file)[0]
      img_inference(img_infer)
    

    希望对你有帮助!

    【讨论】:

      猜你喜欢
      • 2020-09-17
      • 1970-01-01
      • 2020-06-06
      • 2021-09-27
      • 1970-01-01
      • 2019-07-14
      • 2018-11-11
      • 2021-12-13
      • 2020-12-17
      相关资源
      最近更新 更多