Python CV2映像错误:错误:(-215:断言失败)!函数';cv::调整大小';

Python CV2映像错误:错误:(-215:断言失败)!函数';cv::调整大小';,python,image,opencv,tensorflow,image-processing,Python,Image,Opencv,Tensorflow,Image Processing,我正在用TensorFlow和Python制作一个图像分类器,但是用CV2读取图像时出错。我对CV2非常陌生,我还没有找到任何足以解决我问题的方法。有人能解释一下如何解决这个问题吗 def序列数据(带标签)(): 列车图像=[] 对于tqdm中的i(os.listdir(列车数据)): path=os.path.join(列数据,i) img=cv2.imread(路径3) img=cv2.调整大小(img,(64,64)) train_images.append([np.array(img),

我正在用TensorFlow和Python制作一个图像分类器,但是用CV2读取图像时出错。我对CV2非常陌生,我还没有找到任何足以解决我问题的方法。有人能解释一下如何解决这个问题吗

def序列数据(带标签)():
列车图像=[]
对于tqdm中的i(os.listdir(列车数据)):
path=os.path.join(列数据,i)
img=cv2.imread(路径3)
img=cv2.调整大小(img,(64,64))
train_images.append([np.array(img),一个热标签(i)])
洗牌(训练图像)
返回列车图像
带有标签()的def测试数据:
测试图像=[]
对于TQM中的i(os.listdir(测试数据)):
path=os.path.join(测试数据,i)
img=cv2.imread(路径3)
img=cv2.调整大小(img,(64,64))
test_images.append([np.array(img),一个热标签(i)])
洗牌(测试图像)
返回测试图像
这是我得到的错误:

使用TensorFlow后端。

0%| | 0/2[00:00您可能正在读取无效的零像素图像。请检查您的图像路径,因为源图像的大小很可能为空且未正确读取。您可以检查图像的形状以确保其具有有效的
(高度、宽度、通道)
这样的尺寸

image = cv2.imread('noexist.png')
print(image.shape)
如果出现类似于此的
AttributeError
错误,则表示图像路径无效/损坏

Traceback (most recent call last):
  File ".\test.py", line 4, in <module>
    print(image.shape)
AttributeError: 'NoneType' object has no attribute 'shape'
注意:如果删除
cv2.resize()
,将不会引发异常。要解决问题,可以使用
cv2.error
try/except
块捕获异常

import cv2

image = cv2.imread('noexist.jpg')
try:
    resize = cv2.resize(image, (64,64))
except cv2.error as e:
    print('Invalid frame!')
cv2.waitKey()
无效帧


根据错误,输入中似乎存在一些问题。应该是非空数组的列表。请验证路径和图像是否存在任何损坏的图像。此外,检查图像格式;如果存在cv2.imread无法读取的任何多波段tiff文件


尝试分批读取图像,以识别具有导致问题的图像的批。

检查您尝试在for循环中迭代的文件夹中是否有\u DS\u Store文件,因为它不是图像文件cv2.resize()无法调整大小。

在读取图像之前,请检查是否存在不存在的图像。在读取图像之前,请尝试以下操作:

if not os.path.isfile(train_data):
    continue

path = os.path.join(train_data, i)

img = cv2.imread(path, 3)

我想问题出在工作目录上。我也收到了同样的错误,当我更正我的工作目录时,它被解决了。下面是我的代码,它在从正确的目录运行时工作

def image_2_text(input_path,out_path):
 for ImgName in os.listdir(input_path):

    fullpath = os.path.join(input_path,ImgName)
    Img = cv2.imread(fullpath)
    text = pytesseract.image_to_string(Img,lang='eng')
    text = text.replace("\n"," ")
    text = " ".join(re.findall("[a-zA-Z]*",text))
    
    file_append = open(out_path,'a+')
    file_append.write(ImgName+'\t')
    file_append.write(text+'\n')
    file_append.close()
print("Process Complete")

我收到此错误是因为我正在将多个图像馈送到
cv2.resize()


resizedNumpyImage=cv2.resize(numpyArrayOfSingleImage,(neWidth,newHeight))

更改终端上的目录对我来说很有用

cd C:/Users…/图像位置


你有任何损坏的图片吗?你的一些图片之间有空格,如
Z(6).jpg
,可能是读错了吗?尝试删除图像名称中的所有空格。我在一组较小的图像上尝试了该程序,但名称中没有空格,但我得到了相同的错误。更正-事实上,如果我在没有图像的情况下运行该程序,它仍然会给出该错误。通常,当我遇到此错误时,99%的时间是因为无效d图像路径。在
cv2.imread()
之后立即尝试
print(img.shape)
,它应该立即诊断问题
def image_2_text(input_path,out_path):
 for ImgName in os.listdir(input_path):

    fullpath = os.path.join(input_path,ImgName)
    Img = cv2.imread(fullpath)
    text = pytesseract.image_to_string(Img,lang='eng')
    text = text.replace("\n"," ")
    text = " ".join(re.findall("[a-zA-Z]*",text))
    
    file_append = open(out_path,'a+')
    file_append.write(ImgName+'\t')
    file_append.write(text+'\n')
    file_append.close()
print("Process Complete")