TypeError:“numpy.float32”类型的对象没有 len()

Posted

技术标签:

【中文标题】TypeError:“numpy.float32”类型的对象没有 len()【英文标题】:TypeError: object of type 'numpy.float32' has no len() 【发布时间】:2021-12-19 12:12:17 【问题描述】:

我已经在 Raspberry Pi 上运行了一段时间的 Tensorflow Lite 来进行对象检测,我已经在几个测试模型上进行了尝试,完全没有问题。最近我尝试制作自己的模型,但遇到了这个错误。我该如何解决这个问题,有人知道它有什么问题吗?

这是我在 Pi 上运行 Tensorflow Lite 的代码

import os

import argparse

import cv2

import numpy as np

import sys

import time

from threading import Thread

import importlib.util



class VideoStream:

    """Camera object that controls video streaming from the Picamera"""

    def __init__(self,resolution=(640,480),framerate=30):

        self.stream = cv2.VideoCapture(0)

        ret = self.stream.set(cv2.CAP_PROP_FOURCC, cv2.VideoWriter_fourcc(*'MJPG'))

        ret = self.stream.set(3,resolution[0])

        ret = self.stream.set(4,resolution[1])

            

        (self.grabbed, self.frame) = self.stream.read()



        self.stopped = False



    def start(self):

        Thread(target=self.update,args=()).start()

        return self



    def update(self):

        while True:

            if self.stopped:

                self.stream.release()

                return



            (self.grabbed, self.frame) = self.stream.read()



    def read(self):

        return self.frame



    def stop(self):

        self.stopped = True



parser = argparse.ArgumentParser()

parser.add_argument('--modeldir', help='Folder the .tflite file is located in',

                    required=True)

parser.add_argument('--graph', help='Name of the .tflite file, if different than detect.tflite',

                    default='detect.tflite')

parser.add_argument('--labels', help='Name of the labelmap file, if different than labelmap.txt',

                    default='labelmap.txt')

parser.add_argument('--threshold', help='Minimum confidence threshold for displaying detected objects',

                    default=0.5)

parser.add_argument('--resolution', help='Desired webcam resolution in WxH. If the webcam does not support the resolution entered, errors may occur.',

                    default='1280x720')

parser.add_argument('--edgetpu', help='Use Coral Edge TPU Accelerator to speed up detection',

                    action='store_true')



args = parser.parse_args()



MODEL_NAME = args.modeldir

GRAPH_NAME = args.graph

LABELMAP_NAME = args.labels

min_conf_threshold = float(args.threshold)

resW, resH = args.resolution.split('x')

imW, imH = int(resW), int(resH)

use_TPU = args.edgetpu



pkg = importlib.util.find_spec('tflite_runtime')

if pkg:

    from tflite_runtime.interpreter import Interpreter

    if use_TPU:

        from tflite_runtime.interpreter import load_delegate

else:

    from tensorflow.lite.python.interpreter import Interpreter

    if use_TPU:

        from tensorflow.lite.python.interpreter import load_delegate



if use_TPU:

    if (GRAPH_NAME == 'detect.tflite'):

        GRAPH_NAME = 'edgetpu.tflite'       



CWD_PATH = os.getcwd()



PATH_TO_CKPT = os.path.join(CWD_PATH,MODEL_NAME,GRAPH_NAME)



PATH_TO_LABELS = os.path.join(CWD_PATH,MODEL_NAME,LABELMAP_NAME)



with open(PATH_TO_LABELS, 'r') as f:

    labels = [line.strip() for line in f.readlines()]



if labels[0] == '???':

    del(labels[0])





if use_TPU:

    interpreter = Interpreter(model_path=PATH_TO_CKPT,

                              experimental_delegates=[load_delegate('libedgetpu.so.1.0')])

    print(PATH_TO_CKPT)

else:

    interpreter = Interpreter(model_path=PATH_TO_CKPT)



interpreter.allocate_tensors()



input_details = interpreter.get_input_details()

output_details = interpreter.get_output_details()

height = input_details[0]['shape'][1]

width = input_details[0]['shape'][2]



floating_model = (input_details[0]['dtype'] == np.float32)



input_mean = 127.5

input_std = 127.5



frame_rate_calc = 1

freq = cv2.getTickFrequency()



videostream = VideoStream(resolution=(imW,imH),framerate=30).start()

time.sleep(1)



while True:



    t1 = cv2.getTickCount()



    frame1 = videostream.read()



    frame = frame1.copy()

    frame_rgb = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)

    frame_resized = cv2.resize(frame_rgb, (width, height))

    input_data = np.expand_dims(frame_resized, axis=0)



    if floating_model:

        input_data = (np.float32(input_data) - input_mean) / input_std



    interpreter.set_tensor(input_details[0]['index'],input_data)

    interpreter.invoke()



    boxes = interpreter.get_tensor(output_details[0]['index'])[0] 

    classes = interpreter.get_tensor(output_details[1]['index'])[0] 

    scores = interpreter.get_tensor(output_details[2]['index'])[0] 



    for i in range(len(scores)):

        if ((scores[i] > min_conf_threshold) and (scores[i] <= 1.0)):



            ymin = int(max(1,(boxes[i][0] * imH)))

            xmin = int(max(1,(boxes[i][1] * imW)))

            ymax = int(min(imH,(boxes[i][2] * imH)))

            xmax = int(min(imW,(boxes[i][3] * imW)))

            

            cv2.rectangle(frame, (xmin,ymin), (xmax,ymax), (10, 255, 0), 2)



            object_name = labels[int(classes[i])]

            label = '%s: %d%%' % (object_name, int(scores[i]*100)) 

            labelSize, baseLine = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.7, 2) 

            label_ymin = max(ymin, labelSize[1] + 10)

            cv2.rectangle(frame, (xmin, label_ymin-labelSize[1]-10), (xmin+labelSize[0], label_ymin+baseLine-10), (255, 255, 255), cv2.FILLED) 

            cv2.putText(frame, label, (xmin, label_ymin-7), cv2.FONT_HERSHEY_SIMPLEX, 0.7, (0, 0, 0), 2) 



    cv2.putText(frame,'FPS: 0:.2f'.format(frame_rate_calc),(30,50),cv2.FONT_HERSHEY_SIMPLEX,1,(255,255,0),2,cv2.LINE_AA)



    cv2.imshow('Matthew De La Rosa: AP Research 2021-2022', frame)



    t2 = cv2.getTickCount()

    time1 = (t2-t1)/freq

    frame_rate_calc= 1/time1



    if cv2.waitKey(1) == ord('q'):

        break



cv2.destroyAllWindows()

videostream.stop()

这是我的错误

Traceback (most recent call last):
  File "TFLite_detection_webcam.py", line 137, in <module>
    for i in range(len(scores)):
TypeError: object of type 'numpy.float32' has no len()

【问题讨论】:

请修剪您的代码,以便更容易找到您的问题。请按照以下指南创建minimal reproducible example。 【参考方案1】:

scorenumpy.float32 类型,因此不能传递给len(),但可以直接传递给range()(如果它可以转换为int)

for i in range(int(scores)):

如果这是不可能的,可能分数应该是list,而问题出在代码的前面

【讨论】:

我可以试试这个,我只是不明白为什么它会在我的自定义模型而不是每个模型上给我这个错误。仍然会尝试这个解决方案。谢谢。 如果你有参考代码要关闭,我会检查当时应该是什么数据类型分数 我的项目基于本教程github.com/EdjeElectronics/…len() 是最初使用的。 您的代码似乎已将分数设置为列表,但是 scores = interpreter.get_tensor(output_details[2]['index'])[0] 返回的是浮点数而不是列表 我明白了。那么我该如何解决这个问题呢?

以上是关于TypeError:“numpy.float32”类型的对象没有 len()的主要内容,如果未能解决你的问题,请参考以下文章

为啥 Pandas 将我的 numpy float32 强制转换为 float64?

第一周总结

总结元素时numpy float32的精度值变化[重复]

将 numpy dtypes 转换为原生 python 类型

tensorflow学习之路---简单的代码

TypeError: 'float' 类型的对象没有 len() & TypeError: 'float' 对象不可迭代