Aditya S
Aditya S

Reputation: 53

What does (AttributeError: 'NoneType' object has no attribute '__array_interface__') mean?

I am trying to make a machine learning program which classifies an image of a cell as an infected or uninfected. I created the model on a separate python file and am creating a script which will call it on an image to classify it. Here is the full code:

def convert(img):
    img1 = cv2.imread(img)
    img = Image.fromarray(img1, 'RGB')
    image = img.resize((50, 50))
    return (np.array(image))
def cell_name(label):
    if label == 0:
        return ("Paracitized")
    if label == 1:
        return ("Uninfected")
def predict(file):
    print ("Predicting...please wait")
    ar = convert(file)
    ar = ar/255
    label = 1
    a = []
    ar.append(ar)
    a = np.array(a)
    score = loaded_model.predict(a, verbose=1)
    print (score)
    label_index=np.argmax(score)
    print(label_index)
    acc=np.max(score)
    Cell=cell_name(label_index)
    return (Cell,"The predicted Cell is a "+Cell+" with accuracy =    "+str(acc))

print(predict("test/paracitized.png"))

Here is the full error message:

File "malaria_img.py", line 15, in convert
    img = Image.fromarray(img1, 'RGB')
  File "/Library/Python/2.7/site-packages/PIL/Image.py", line 2526, in fromarray
    arr = obj.__array_interface__
AttributeError: 'NoneType' object has no attribute '__array_interface__'

Does anyone know why this may be happening?

Upvotes: 2

Views: 18599

Answers (2)

Hosseinzlf
Hosseinzlf

Reputation: 21

The problem is in your convert function. You try to turn the image to array and then inside your code you are trying to turn it again to array. Remove one of them and test your code again. I revised your function:

def convert(img):
   img1 = cv2.imread(img)
   img = Image.fromarray(img1, 'RGB')
   image = img.resize((50, 50))
   return image

Upvotes: 1

Karl Knechtel
Karl Knechtel

Reputation: 61515

arr = obj.__array_interface__
AttributeError: 'NoneType' object has no attribute '__array_interface__'

AttributeError means that there was an Error that had to do with an Attribute request. In general, when you write x.y, y is the purported attribute of x.

'NoneType' object means an object which has NoneType as its type. There is exactly one such object, called None (you should be familiar with it). It, indeed, has no attribute that is named __array_interface__.

The problem is that we are asking for this attribute from obj, but obj has the value None. It was supposed to be some kind of array-like object instead. This is not in our own code; rather, obj is PIL's internal name for the img1 that we passed in to the fromarray call.

The img1 in our own code comes from the previous line, img1 = cv2.imread(img). The documentation tells us (sort of - because it's trying to document the C++ and Python versions simultaneously; you kinda have to fill in some blanks here) that CV2 returns None when the image cannot be read (because of missing file, improper permissions, unsupported or invalid format).

Double-check your filename (if the filename came from processing a string, check for unexpected whitespace, and double-check the extension on the filename); double-check the file path (if you gave a relative path, double-check what the working directory is - you maybe surprised!); and double-check the file itself (does the Python process have permissions to open it? Is the image corrupted?).

Upvotes: 4

Related Questions