从文件夹访问文件时检索错误

我有以下代码,我需要 opencv 读取文件夹中的文件。


 filename = 'C:/Users/test_image'

 files = filename

 for file in files:

    orig = cv2.imread(file)

    image = image_utils.load_img(file, target_size=(224,224))

    image = image_utils.img_to_array(image)

     

我正在检索以下错误。


test_imagenet.py:26: in <module>

 image = image_utils.load_img(file, target_size=(224, 224))

 ..\..\miniconda3\envs\tensorflow\lib\site-packages\keras_preprocessing\image\utils.py:110: in 

 load_img

 img = pil_image.open(path)

 ..\..\miniconda3\envs\tensorflow\lib\site-packages\PIL\Image.py:2809: in open

 fp = builtins.open(filename, "rb")

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

 collected 0 items / 1 error

非常感谢您的帮助。谢谢


九州编程
浏览 34回答 1
1回答

慕尼黑5688855

您正在迭代一个字符串,因此它只会获得第一个字符。您需要列出该目录的内容。像这样使用 os.listdir 。import osfilename = 'C:/Users/test_image'files = os.listdir(filename)for file in files:&nbsp; &nbsp; orig = cv2.imread(file)&nbsp; &nbsp; image = image_utils.load_img(file, target_size=(224,224))&nbsp; &nbsp; image = image_utils.img_to_array(image)
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python