Numpy 属性错误:';元组';对象没有属性';展平';

Numpy 属性错误:';元组';对象没有属性';展平';,numpy,cv2,Numpy,Cv2,我能知道如何解决这个错误吗 AttributeError:“tuple”对象没有属性“Flatte” for i in indexes.flatten(): x, y, w, h = boxes[i] label = str(classes[class_ids[i]]) confidence = str(round(confidences[i],2)) color = colors[i] cv2.rectangle(img, (x, y), (x+w, y

我能知道如何解决这个错误吗

AttributeError:“tuple”对象没有属性“Flatte”

for i in indexes.flatten():
    x, y, w, h = boxes[i]
    label = str(classes[class_ids[i]])
    confidence = str(round(confidences[i],2))
    color = colors[i]
    cv2.rectangle(img, (x, y), (x+w, y+h), color, 2)
    cv2.putText(img, label + " " + confidence, (x, y+20), font, 2, (255, 255, 255),2)

我不知道你是否已经解决了这个问题。 在像这样的for循环之前,必须检查0个长度索引:

if len(indexes) > 0:
    for i in indexes.flatten():
        ....
这样,当索引长度为0时,它就不会进入for循环,这导致了错误


问候

对于layerOutputs中的输出:对于输出中的检测:分数=检测[5:]#提取从第六个元素开始到结束的类预测_id=np。argmax(分数)置信度=分数[class_id]如果置信度>0.5:中心_x=int(检测[0]*宽度)中心y=int(检测[1]*高度)w=int(检测[2]*宽度)h=int(检测[3]*高度)x=int(中心x-w/2)y=int(中心y-h/2)框。追加([x,y,w,h])置信度。追加((浮动(置信度)))类标识。追加(类标识)print(len(box))index=cv2.dnn.NMSBoxes(box,confidences,0.5,0.4)font=cv2.font\u HERSHEY\u PLAIN colors=np.random.uniform(0,255,size=(len(box),3))是的,它已经工作了,这与您的一样。非常感谢。