简体   繁体   English

从路径列表中读取文件

[英]Read files from a list of paths

I have downloaded a dataset for a deep learning project that contains images (.png) and the corresponding label for each image (.txt).我已经下载了一个深度学习项目的数据集,其中包含图像 (.png) 和每个图像 (.txt) 的相应 label。 I have all the images' paths in a list x .我在列表x中有所有图像的路径。 I want to iterate through these paths, preprocess the images using cv2 , and append the new images to a new list images_data .我想遍历这些路径,使用cv2预处理图像,并将新图像 append 处理到新列表images_data However, every time I try to loop through it, I keep getting this same error: cv2.error: OpenCV(4.1.1) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed)._src.empty() in function 'cvtColor'但是,每次我尝试遍历它时,我都会收到同样的错误: cv2.error: OpenCV(4.1.1) /io/opencv/modules/imgproc/src/color.cpp:182: error: (-215:Assertion failed)._src.empty() in function 'cvtColor'

Even when I comment out that line of code that throws the error, I get another error when trying to resize the image.即使我注释掉引发错误的那行代码,在尝试调整图像大小时也会出现另一个错误。

This is the for loop I'm using to iterate through the list:这是我用来遍历列表的 for 循环:

images_data = []
    for file in x:
        img = cv2.imread(file)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img = cv2.resize(img, (80, 80))
        images_data.append(img)

My x list looks pretty much like this: x = [car1.png, car2.png, car3.png, car4.png, car5.png]我的x列表看起来很像这样: x = [car1.png, car2.png, car3.png, car4.png, car5.png]

How can I solve this error?我该如何解决这个错误?

That error indicates your img variable is empty so cv2.imread(file) did not read an image.该错误表明您的img变量为空,因此cv2.imread(file)没有读取图像。 You can check this after reading the image and before converting the color or resizing, with a simple if case:您可以在读取图像之后并在转换颜色或调整大小之前检查这一点,使用简单的 if 情况:

if img is None: 
    print('Error reading image')
else:
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

or check if the file exists using the os module:或使用os模块检查文件是否存在:

img = cv2.imread(file)
if os.path.isfile(file): 
    img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

os.path.isfile(file) will return True if the file exists.如果文件存在,os.path.isfile(file) 将返回True

I am assuming your files, car1.png, car2.png etc are in a different folder and not in the same path with this script in which case, you need to append the directory of the images to the file name before you can read it.我假设您的文件car1.png, car2.png等位于不同的文件夹中,并且与此脚本不在同一路径中,在这种情况下,您需要将图像的目录 append 到文件名,然后才能读取它. So for example, if your images are in, let's say, '/home/Deep_Learning/Car_Images/' then when reading the images, the variable file must contain the string: /home/Deep_Learning/Car_Images/car1.png , for the first image and not just car1.png .因此,例如,如果您的图像位于'/home/Deep_Learning/Car_Images/'中,那么在读取图像时,变量file必须包含字符串: /home/Deep_Learning/Car_Images/car1.png ,对于第一个图像而不仅仅是car1.png You can do this using python's os module like this:您可以使用 python 的os模块来执行此操作,如下所示:

import cv2
import os

# Directory of the images. Change it according your path
data_dir = '/home/Deep_Learning/Car_Images/' 

images_data = []
for file in x:
    file_name = os.path.join(data_dir, file) # Append the image folder with the image name

    if os.path.isfile(file_name) is False: #Check if image file exists
        print("Image file ", file_name, "does not exist")
    else:
        img = cv2.imread(file_name)
        img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
        img = cv2.resize(img, (80, 80))
        images_data.append(img)

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

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