错误“ NoneType”对象的含义是不能下标的

时间:2019-12-05 17:12:05

标签: python artificial-intelligence

我正在研究人脸识别项目,其中训练了模型。预测我加载图像,并想计算2张图像之间的距离。在进行预防时,我遇到以下错误:

             TypeError                                 Traceback (most recent call last)
       <ipython-input-21-0b1f36824e17> in <module>()
  8   if(cropped_img == type(None)):
  9     cropped_img = np.zeros(shape = (224, 224))
  ---> 10   img = (load_image(cropped_img) / 255.).astype(np.float32)
 11   img = cv2.resize(img, dsize = (224,224))
 12   embedding_vector = vgg_face_descriptor.predict(np.expand_dims(img, axis=0))[0]

 <ipython-input-9-6d96fb74d85b> in load_image(path)
  4     # OpenCV loads images with color channels
  5     # in BGR order. So we need to reverse them
  ----> 6     return img[...,::-1]

 TypeError: 'NoneType' object is not subscriptable

代码在下面

      NoneType = type(None)


embedding =[]
for i, m in enumerate(metadata):
   cropped_img = m.image_path()
   print(i)
  if(cropped_img == type(None)):
     cropped_img = np.zeros(shape = (224, 224))
  img = (load_image(cropped_img) / 255.).astype(np.float32)
  img = cv2.resize(img, dsize = (224,224))
  embedding_vector = vgg_face_descriptor.predict(np.expand_dims(img, axis=0))[0]

  embedding.append(embedding_vector)

加载图像代码如下:

       def load_image(path):
         img = cv2.imread(path, 1)
         # OpenCV loads images with color channels
         # in BGR order. So we need to reverse them
         return img[...,::-1]

由于我是python新手,所以我无法理解此错误的含义

2 个答案:

答案 0 :(得分:1)

public static void PaypalPayment() throws InterruptedException {

        driver.DOSTUFF...blah blah blah

        driver.findElement(By.id("email")).sendKeys("blah blah");
        driver.findElement(By.id("btnNext")).click();

        driver.findElement(By.id("password")).sendKeys("blah blah");
        driver.findElement(By.id("btnLogin")).click();

        wait.until(ExpectedConditions.visibilityOfElementLocated(By.id("button")));

因此,如果>>> None == type(None) False 为None,则您的比较cropped_img将为if(cropped_img == type(None)):。因此,False将永远不会执行,因此cropped_img = np.zeros(shape = (224, 224)) 将保留为cropped_img 并将传递给None,这是错误消息显示,不适用于load_image

您应该这样检查:

None

答案 1 :(得分:-1)

下面的代码将解决问题,因为imread给出None

     def load_image(path):
       img = cv2.imread(path, 1)
        if type(img) == type(None):
           img = np.zeros(shape = (224, 224))
     # OpenCV loads images with color channels
     # in BGR order. So we need to reverse them
       return img[...,::-1]