警告无法打开/读取文件:检查文件路径/完整性

awa*_*tif 8 python opencv path

images_per_class = 80
fixed_size       = tuple((500, 500))
train_path       = "dataset/train"

train_labels = os.listdir(train_path)
for training_name in train_labels:
dir = os.path.join(train_path, training_name)
current_label = training_name
   for x in range(1,images_per_class+1):
    # get the image file name
    file = dir + "/" + str(x) + ".jpg"

    # read the image and resize it to a fixed-size
    image = cv2.imread(file)
    image = cv2.resize(image, fixed_size)
Run Code Online (Sandbox Code Playgroud)

当我运行此代码时出现此错误

error: OpenCV(4.5.5) D:\a\opencv-python\opencv-python\opencv\modules\imgproc\src\resize.cpp:4052: error: (-215:Assertion failed) !ssize.empty() in function 'cv::resize'
Run Code Online (Sandbox Code Playgroud)

和这个警告[ WARN:0@19.045] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\loadsave.cpp (239) cv::findDecoder imread_('dataset/train\Apple/1.jpg'): can't open/read file: check file path/integrity

我安装 opencv 没有问题,因为我之前使用过它并且使用了另一个代码,它有任何帮助吗?

小智 5

亲爱的,对我来说,问题与文件扩展名有关。我添加了扩展名 .JPEG,而实际扩展名是 .jpg。更正扩展即可清除警告。谢谢


小智 0

文件=目录+“/”+str(x)+“.jpg”

尝试将此行替换为:

file = dir + "\" + str(x) + ".jpg"
Run Code Online (Sandbox Code Playgroud)

/ 不正确

正确的是\

  • 常规字符串文字中的反斜杠将对后面的字符进行转义。该答案包含**语法错误**。-- opencv/windows 通常允许路径中混合使用正斜杠和反斜杠。我不会打赌斜杠是问题所在。-- 我敢打赌这里的问题是**文件实际上并不存在** (2认同)