-8
    # TensorFlow and tf.keras
    import tensorflow as tf
    from tensorflow import keras
    
    # Helper libraries
    import numpy as np
    import matplotlib.pyplot as plt
    import os 
    import cv2
    
    print(tf.__version__)
    
    DATADIR ="D:\Datasetes\legen"
    CATEGORIES = ['light']
    
    for category in CATEGORIES:
        path = os.path.join(DATADIR,category) #to create a path to the image in our drive
        for img in os.listdir(path):
            img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
            plt.imshow(img_array , cmap="gray")
            plt.show
            plt.figure()
            plt.imshow(img_array)
            plt.colorbar()
            plt.grid(False)
            plt.show()
            break
        break
    
    class_names = ['light', 'power', 'capture']
    train_images.shape
    len(train_labels)
    train_labels
    train_images = train_images / 255.0
    train_labels = train_labels / 255.0
    
    test_images = test_images / 255.0
    test_labels = test_labels / 255.0
    
    def class_names():
        for category in CATEGORIES:  
    
            path = os.path.join(DATADIR,category) 
            class_names = CATEGORIES.index(category)  
    
            for img in (os.listdir(path)):  
                try:
                    img_array = cv2.imread(os.path.join(path,img) ,cv2.IMREAD_GRAYSCALE)  # convert to array
                    new_array = cv2.resize(img_array, (IMG_SIZE, IMG_SIZE))  # resize to normalize data size
                    training_data.append([new_array, class_num])
                except Exception as e:  
                    pass
         
 
    import random
    random.shuffle(training_data)
    img_array = cv2.imread(os.path.join(path,img),cv2.IMREAD_GRAYSCALE)
    plt.figure(figsize=(10,10))
    for i in range(10):
        plt.subplot(5,5,i+1)
        plt.xticks([])
        plt.yticks([])
        plt.grid(False)
        plt.imshow(img_array, cmap='gray')
        plt.xlabel(class_names(train_labels)) 
        plt.show()
        break
TypeError                                 Traceback (most recent call last)
<ipython-input-147-400073625620> in <module>
     29     plt.grid(False)
     30     plt.imshow(img_array, cmap='gray')
---> 31     plt.xlabel(class_names(train_labels))
     32     plt.show()
     33     break

TypeError: class_names() takes 0 positional arguments but 1 was given

this is the code that I am using and it keeps on giving an error as ''function' object is not subscriptable.I have added the whole code into this. please help as this is a school project and it is included in my goddamn GPA the error happens in the line with :the last part of the code

  • 2
    More information is required for people to help. On which line does the error occur? Please include the error message. – Nick Jul 01 '20 at 08:54
  • 2
    Hey man, welcome to StackOverFlow, I'm quite new to so it helps to post useful information on your title, and content, if everybody used your title it would make life so much worse wouldn't it? Try to make your title summarize the problem, appreciated – Salman B Jul 01 '20 at 08:54
  • 2
    Please read the following article on how to ask a good question: https://stackoverflow.com/help/how-to-ask – Nick Jul 01 '20 at 08:54
  • Why is this tagged tensorflow2.0? – gspr Jul 01 '20 at 08:56
  • it occurs on the plt.xlabel line – Karan Athavale Jul 01 '20 at 08:56
  • 1
    No wonder. That line uses `train_labels`, which isn't defined anywhere. There's other undefined stuff in your code, too. Please include the rest of your code, or produce a smaller example that demonstrates the problem. – gspr Jul 01 '20 at 08:58
  • Does this answer your question? ['TypeError: 'function' object is not subscriptable' in Python 3.4.3?](https://stackoverflow.com/questions/32446402/typeerror-function-object-is-not-subscriptable-in-python-3-4-3) – Melebius Jul 01 '20 at 09:06

1 Answers1

0

The name class_names is used both as a variable and also as a function name.Hence there is an ambiguity when you do

class_names[]

First Used here

class_names = ['light', 'power', 'capture']

Then here

def class_names():

After this class_names is no longer a list but a function.

Then, when you do

class_names[something] - it is an error