首页 > 解决方案 > 为什么我在对象检测中遇到错误?

问题描述

confidence_score = scores[class1]

IndexError:索引 172 超出轴 0 的范围,大小为 5

import cv2
import numpy as np
net = cv2.dnn.readNet("yolov3.weights", "yolov3.cfg.txt")
classes = []
img1 = cv2.imread('img1.jpg')
img1 = cv2.resize(img1,None, fx =0.4, fy =0.4)
height,width,chanels = img1.shape
with open("coco.names.txt", "r") as f:
     classes = [line.strip() for line in f.readlines()]
layer_name = net.getLayerNames()
output_layers = [layer_name[i[0] - 1] for i in net.getUnconnectedOutLayers()]
floaty =0.004
blob = cv2.dnn.blobFromImage(img1,floaty,(416,416),(0,0,0),True)
# true to convert RBG
for b in blob:
    for n,img_blog in enumerate(b):
        cv2.imshow(str(n), img_blog)
net.setInput(blob)
out = net.forward(output_layers)

#trying to show or detect
for show in out:
    for detection in out:
        scores = detection[:5]
        class1 = np.argmax(scores)
        confidence_score = scores[class1]
        if confidence_score > 0.6:
            center_x = int[detection[0] * width]
            center_y = int(detection[1] * height)
            w = int(detection[2] * width)
            h = int(detection[3] * height)
            cv2.circle(img1,(center_x,center_y),12,(0,255,0),2)

cv2.imshow('image', img1)
cv2.waitKey(0)
cv2.destroyAllWindows()

标签: python

解决方案


问题是numpy.argmax不带axis参数使用会返回索引,就好像数组被展平了一样:

a = np.arange(6).reshape(2,3) + 10
# array([[10, 11, 12],
#       [13, 14, 15]])

flat_max = np.argmax(a)
# 5 <=== "flattened" indices of max element (15)

a[flat_max]
# IndexError: index 5 is out of bounds for axis 0 with size 2

您可以使用np.unravel_index()结果numpy.argmax()来获取最大元素的正确索引:

i, j = np.unravel_index(np.argmax(a), a.shape)
a[i][j]
# (1,2) <=== correct indices of max element (15)

在您的代码中,您得到 172 作为 的结果np.argmax(scores),但由于轴零的scores大小仅为 5,因此您得到IndexError. np.unravel_index()如上所示使用并使用返回的索引来索引scores

i, j = np.unravel_index(np.argmax(scores), scores.shape)
confidence_score = scores[i][j]

推荐阅读