Skip to content Skip to sidebar Skip to footer

Attributeerror: 'nonetype' Object Has No Attribute 'shape'

import numpy as np import cv2 from matplotlib import pyplot as plt img = cv2.imread('AB.jpg') mask = np.zeros(img.shape[:2] , np.uint8) bgdModel = np.zeros((1,65), np.float64)

Solution 1:

Answering, because the community brought it back. Adding my two objects (cents).

The only reason you see that error is because you are trying to get information or perform operations on an object that doesn't exist in the first place. To check, try printing the object. Like, adding -

print img      # such as this caseprint contours # if you are working with contours and cant draw oneprint frame    # if you are working with videos and it doesn't show

gives you a None. That means you haven't read it properly. Either the image name you gave does not exist or the path to it is wrong. If you find such an error here's the quick things to do-

  1. Check the path or bring your image to the working directory
  2. Check the name you gave is right (including the extension- .jpg, .png etc)
  3. Put the entire code in a if statement with the object and the code to proceed if true
  4. Will add more if suggested in the comments.

Solution 2:

First of all

python img.py AB.jpg

will not work as you expect (load AB.jpg from the current directory). The file to load is hardcoded in line 6 of the provided example: to work as intended, it should be something like this:

importsysimg= cv2.imread(sys.argv[1])

The error is returned because AB.jpg does not exist in the directory img.py is being run from (the current working directory), and there is no verification for a missing file before trying to read it.

Post a Comment for "Attributeerror: 'nonetype' Object Has No Attribute 'shape'"