【问题标题】:FileNotFoundError: [WinError 3] The system cannot find the path specifiedFileNotFoundError: [WinError 3] 系统找不到指定的路径
【发布时间】:2020-01-29 01:26:21
【问题描述】:

代码:

import os
import cv2
folder = ['test images', 'ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 
'SHARK', 'YFT' ]
Path = r'D:\ncfm\train'

for i in range(9):
    listing = os.listdir(path+'/'+folder[i])
    folder[i+1] = np.array([np.array(cv2.imread(path+'/'+folder[i]+'/'+file)).flatten()
for file in listing])

错误:

FileNotFoundError                         Traceback (most recent call 
last)
<ipython-input-152-d8f8c2149488> in <module>
      5 
      6 for i in range(9):
----> 7     listing = os.listdir(path+'/'+folder[i])
      8     folder[i+1] = 
np.array([np.array(cv2.imread(path+'/'+folder[i]+'/'+file)).flatten()
      9     for file in listing])

FileNotFoundError: [WinError 3] The system cannot find the path specified: 
'Users\\USER\\Desktop\\ncfmtrain\\YFT\\*.jpg/test images'

我已多次尝试纠正此问题。但问题依然存在。然后我尝试了这个对我有用的代码。

import os
from os import listdir

for i in range(9):
    for fld in folders:
        index = folders.index(fld)
        print('Load folders {} (Index: {})'.format(fld, index))
        path = os.path.join('Users', 'USER' , 'Desktop','ncfm' 'train', fld, '*.jpg')
        L.append(len(path))


    break

这对我来说很好用。但随后出现以下错误:

ValueError: Found input variables with inconsistent numbers of samples: [8, 3777]

我猜这些是相关的。

【问题讨论】:

    标签: python-3.x image-processing jupyter-notebook


    【解决方案1】:

    使用 pathlib 进行文件系统访问。

    from pathlib import Path
    
    jpeg_images = list(Path('D:/ncfm/train').glob('**/*.jpg'))
    print(jpeg_images)
    np.array([np.array(cv2.imread(str(file))).flatten() for file in jpeg_images])
    

    更新:

    测试一个文件

    • 使用文件的完整路径,包括驱动器号。
    file = Path(r'C:\Users\USER\Desktop\ncfmtrain\YFT\image_name.jpg')
    print(cv2.imread(str(file))).flatten())
    

    【讨论】:

    • SystemError: 返回 NULL 而没有设置错误现在我收到这样的错误。
    • 更新了帖子。可能是生成器无法使用 opencv。我已将生成转换为列表,并添加了打印以打印所有文件。检查文件名是否正确。
    • 文件正在打印。但我得到了同样的错误。
    • 我认为问题在于这个。 ----> 8 np.array([np.array(cv2.imread(file)).flatten() 用于 jpeg_images 中的文件])
    • 知道它必须是一个字符串..检查更新的答案
    猜你喜欢
    • 2018-12-03
    • 2022-01-23
    • 2021-10-13
    • 2021-08-20
    • 2022-12-11
    • 2020-06-03
    • 2023-01-25
    • 1970-01-01
    • 2022-11-17
    相关资源
    最近更新 更多