OpenCV错误:断言由于图像而失败

时间:2019-09-18 09:29:48

标签: python machine-learning

我一直在打开错误,但是cv断言失败,并且我认为我的图像还可以,并且beem尝试了很多代码,没有得到任何结果,请帮忙。试图创建一个分类器来读取图像是否是车牌号。

def read_image(file_path):
  img = cv2.imread(file_path, cv2.IMREAD_COLOR)
  return cv2.resize(img,(16,16), interpolation=cv2.INTER_CUBIC)


def prep_data(images):
  m = len(images)
  n_x = 16*16*3

  X = np.ndarray((n_x,m), dtype=np.uint8)
  y = np.zeros((1,m))
  print("X.shape is {}".format(X.shape))

  for i,image_file in enumerate(images) :
    image = read_image(image_file)
    X[:,i] = np.squeeze(image.reshape((n_x,1)))
    if image_file[72:-3].isupper() :
      y[0,i] = 1
    else :
      y[0,i] = 0

  return X,y
X_train,y_train = prep_data(train_data)
X_test,y_test = prep_data(test_data)

error
X.shape is (768, 70)
---------------------------------------------------------------------------
error                                     Traceback (most recent call last)
<ipython-input-12-3efa5d093ab3> in <module>()
----> 1 X_train,y_train = prep_data(train_data)
      2 X_test,y_test = prep_data(test_data)

1 frames
<ipython-input-11-86de378ce20c> in read_image(file_path)
      1 def read_image(file_path):
      2   img = cv2.imread(file_path, cv2.IMREAD_COLOR)
----> 3   return cv2.resize(img,(16,16),interpolation=cv2.INTER_CUBIC)
      4 
      5 

error: OpenCV(3.4.3) /io/opencv/modules/imgproc/src/resize.cpp:4044: error: (-215:Assertion failed) !ssize.empty() in function 'resize'

2 个答案:

答案 0 :(得分:0)

检查图像的路径,这是错误的。

要进行验证,请执行以下操作

import sys

def read_image(file_path):
  img = cv2.imread(file_path, cv2.IMREAD_COLOR)
  if img is None: 
      print("Couldnt read image")
      sys.exit()
  return cv2.resize(img,(16,16),interpolation=cv2.INTER_CUBIC)

检查图像是否放置在正确的路径中

答案 1 :(得分:-1)

在重塑图像时将其转换为float32。

img =  X[:,i] = np.squeeze(image.reshape((n_x,1))).astype('float32')