简体   繁体   English

Google Colab 从 colabdisk 读取文件而不是上传

[英]Google Colab reading files from colabdisk instead of upload

I've got a function that only works with colab.upload not with passing it a filename from a file on the colab disk, I uploaded from Drive.我有一个功能只能与 colab.upload 一起使用,而不是将文件名从 colab 磁盘上的文件传递给它,我是从云端硬盘上传的。 I want to work with a jpg picture for the TensorFlow object detection API.我想为 TensorFlow 对象检测 API 使用 jpg 图片。

This is working:这是工作:

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

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

but not this:但不是这个:

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

Error:错误:

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

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'

There is a logic problem in your code:您的代码中存在逻辑问题:

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)

You have to change img_infer = list(file)[0] to img_infer = file , or just delete img_infer = list(file)[0] and change img_inference(img_infer) to img_inference(file) .您必须将img_infer = list(file)[0]更改为img_infer = file ,或者只删除img_infer = list(file)[0]并将img_inference(img_infer)更改为img_inference(file)

In other words:换一种说法:

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

To further explain what is happening in your original code, suppose /content/test contains aaa.txt , bbb.py , ccc.jpg .为了进一步解释原始代码中发生的情况,假设/content/test包含aaa.txtbbb.pyccc.jpg Then:然后:

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)

Hope it helps!希望能帮助到你!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM