“方法”对象不可下标错误

原文标题‘method’ object is not subscriptable erroor

我在 vscode 中写了这段代码:

from fileinput import filename
import imp
import cv2,time,os,tensorflow as tf
import numpy as np

from tensorflow.python.keras.utils.data_utils import get_file

np.random.seed(123)

class Detector:
    def __init__(self) -> None:
        pass


    def readClaassees(self,classesFilePath):
        with open(classesFilePath,'r') as f:
            self.classesList = f.read().splitlines()

            #colors list 
            self.colorList =np.random.uniform(low=0,high=255,size=(len(self.classesList),3))

            print(len(self.classesList),len(self.colorList))    
        

    def downloadModel(self,modelURL):

       fileName= os.path.basename(modelURL)
       self.modelName =fileName[:fileName.index('.')]

       self.cacheDir ="./pretrained_model"
       os.makedirs(self.cacheDir,exist_ok=True)

       get_file(fname=fileName,origin=modelURL,cache_dir=self.cacheDir,cache_subdir="checkpoints",extract=True)  
        
       
    def loadModel(self):
        print("Loading Model "+self.modelName)
        #tf.keras.backend.clear_session()
        self.model = tf.saved_model.load(os.path.join(self.cacheDir,"checkpoints",self.modelName,"saved_model"))

        print("Model"+self.modelName+"loaded successfully...")

    
    def createBoundinBox(self,image):
        inputTensor = cv2.cvtColor(image.copy(),cv2.COLOR_BGR2RGB)
        inputTensor = tf.convert_to_tensor(inputTensor,dtype=tf.uint8)
        inputTensor = inputTensor[tf.newaxis,...]

        detections = self.model(inputTensor)

        bboxs = detections['detection_boxes'][0].numpy()
        classIndexes = detections['detection_classes'][0].numpy().astype(np.int32)
        classScores = detections['detection_scores'][0].numpy

        imH,imW,imC =image.shape

        if len(bboxs) != 0 :
            for i in range(0,len(bboxs)):
                bbox = tuple(bboxs[i].tolist())
                classConfidence = classScores[i]
                classIndex = classIndexes[i]

                classLblelText = self.classesList[classIndex]
                classColor = self.colorList[classIndex]


                displayText ='{}: {}%'.format(classLblelText,classConfidence)

                ymin, xmin, ymax, xmax = bbox

                print(ymin,xmin,ymax,xmax)
                break

    def pedictImage(self,imagePath):
        image = cv2.imread(imagePath)

        self.createBoundinBox(image)


        cv2.imshow("result",image)
        cv2.waitKey(0)
        cv2.destroyAllWindows()

在主运行 self.createBoundinBox(image) 方法后出现此错误:

File "d:\TensorflowObjectDetection\Detector.py", line 60, in createBoundinBox
    classConfidence = classScores[i]
TypeError: 'method' object is not subscriptable.

有谁知道如何解决它,请帮助。

原文链接:https://stackoverflow.com//questions/71992834/method-object-is-not-subscriptable-erroor

回复

我来回复
  • D.Manasreh的头像
    D.Manasreh 评论

    我认为是因为您忘记了这一行中的括号

    classScores = detections['detection_scores'][0].numpy
    

    我认为应该是:

    classScores = detections['detection_scores'][0].numpy()
    

    当你在没有括号的情况下调用它时,你正在调用一个你不能像这样下标的方法或函数method[]。这就是错误告诉你的。

    2年前 0条评论
  • David Culbreth的头像
    David Culbreth 评论

    我在这里大胆猜测一下,说声明classScores的那行,目前是这样的:

    classScores = detections['detection_scores'][0].numpy
    

    应该是这样的:

    classScores = detections['detection_scores'][0].numpy().astype(np.int32)
    

    在这种情况下,.numpy是您调用的方法,它本身不可下标,这意味着您不能在其上使用索引表示法。这是有道理的,因为它是一种方法,而不是数组或列表或其他任何东西。

    2年前 0条评论