IndexError: इंडेक्स 14708 अक्ष 295 के आकार की सीमा से बाहर है

Nov 26 2020

मैं yolo के साथ ऑब्जेक्ट डिटेक्शन सॉफ्टवेयर बनाने की कोशिश कर रहा हूं और यह त्रुटि पॉपिंग है और मैं इतना खो गया हूं कि कृपया कोई मेरी मदद कर सकता है !! (इस पोस्ट cus I new Stackoverflow में कोई ग़लती होने पर कोड पूरा और सॉरी नहीं है।) ट्यूटोरियल यहाँ से है ।

Traceback (most recent call last):
  File "d:/opencv/objdetect_yolo.py", line 66, in <module>
    findobj(output,img)
  File "d:/opencv/objdetect_yolo.py", line 33, in findobj
    cofidence = scores[classId]
IndexError: index 14708 is out of bounds for axis 0 with size 295

IndexError: इंडेक्स 14708 अक्ष 295 के आकार की सीमा से बाहर है

import numpy as np 
import cv2

cap = cv2.VideoCapture(0)
whT = 320

classespath = 'coco.names.txt'
classes = []

with open(classespath,'rt')as f:
    classes = f.read().rstrip('\n').split('\n')
#print (classes)
#print(len(classes))

modelConfiguration = 'yolov3.cfg'
modelWeights = 'yolov3.weights'

net = cv2.dnn.readNetFromDarknet(modelConfiguration, modelWeights)
net.setPreferableBackend(cv2.dnn.DNN_BACKEND_OPENCV)
net.setPreferableTarget(cv2.dnn.DNN_TARGET_CPU)

def findobj(outputs,img):
    hT, wT , cT = img.shape
    bbox = []
    classIds = []
    confs = []


    for output in outputs:
        for det in outputs:
            scores = det[5:]
            classId = np.argmax(scores)
            cofidence = scores[classId]
            if float(0.5) < cofidence:

            
                w,h = int(det[2]*wT),int(det[3]*hT)
                x,y = int((det[0]*wT) - w/2), int((det[1]*hT) - h/2)
                bbox.append([x,y,w,h])
                classIds.append(classId)
                confs.append(float(cofidence))
              




     
while True:
    succes, img = cap.read()

    blob = cv2.dnn.blobFromImage(img,1/255,(whT,whT),[0,0,0],1,crop=False)
    net.setInput(blob)

    layerNames = net.getLayerNames()
    #print(layerNames)
    outputNames = [layerNames[i[0]-1]for i in net.getUnconnectedOutLayers() ]
    #print(outputNames)
    #print(net.getUnconnectedOutLayers())
    output = net.forward(outputNames)


    findobj(output,img)


    cv2.imshow("objdetect",img)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

जवाब

VascoCansadoCarvalho Nov 26 2020 at 19:13

ऐसा लगता है कि आप एक समस्या में चल रहे हैं क्योंकि np.argmaxआपको सूचकांक के बजाय अधिकतम तत्व की कच्ची संख्या देगा। तो अगर आपके पास एक 3x3 मैट्रिक्स है तो argmax फ़ंक्शन मैट्रिक्स को 3x3 वर्ग के बजाय 9x1 लाइन के रूप में व्यवहार करेगा।

# The matrix:
[[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]]

#will be treated as:
[1, 2, 3, 4, 5, 6, 7, 8, 9]

प्रलेखन पता चलता है bellow समाधान:

classId = np.unravel_index(np.argmax(scores, axis=None), scores.shape)
KuliduDavid Nov 27 2020 at 17:53

अब यह पॉपिंग है

Traceback (most recent call last):
  File "d:/opencv/objdetect_yolo.py", line 67, in <module>
    findobj(output,img)
  File "d:/opencv/objdetect_yolo.py", line 38, in findobj
    w,h = int(det[2]*wT),int(det[3]*hT)
TypeError: only size-1 arrays can be converted to Python scalars
[ WARN:1] global C:\Users\appveyor\AppData\Local\Temp\1\pip-req-build-h4wtvo23\opencv\modules\videoio\src\cap_msmf.cpp (435) `anonymous-namespace'::SourceReaderCB::~SourceReaderCB terminating async callback