Hi , I have error related to object detection project
Asked Answered
A

2

6

I have error related to simple object detection .

output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
IndexError: invalid index to scalar variable.

import cv2.cv2 as cv
import numpy as np

# Load Yolo

net = cv.dnn.readNet('yolov3.weights','yolov3.cfg')
classes = []
with open ("coco.names","r") as f:
 classes = [line.strip() for line in f.readlines()]

layer_names = net.getLayerNames()
otputlayers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]

 #Loading image

 img = cv.imread("room_ser.jpg")

cv.imshow("Image",img)
cv.waitKey(0)
cv.destroyAllWindows()
Ararat answered 28/10, 2021 at 15:21 Comment(1)
The reason is explained in #69834835Menon
A
20

getUnconnectedOutLayers() returns an integer, not an iterable. Instead, use

outputlayers = [layer_names[i-1] for i in net.getUnconnectedOutLayers()]

The examples shown here are incorrect. More information on the method can be found on the cv2 docs here.

The error itself (IndexError) tells you that you are trying to index something that is a scalar.

Asphalt answered 28/10, 2021 at 15:29 Comment(1)
It's best just to let people upvote material organically. If you want to get to the 50 mark, try to write one good answer per day for a week, and by the end of the week you should be there. Rep is fun, but don't make it the sole reason you are here - satisfaction can be had in helping people.Globetrotter
E
0

That index issue because it can not iterate

unconnected_indices = neural_network.getUnconnectedOutLayers()
output_names = [layer_names[index - 1] for index in unconnected_indice

bounding_box = all_bounding_boxes[index]

if class_ids[index] == 0:
    cv2.rectangle(img, (x, y), (x+w, y+h), (255, 0, 0), 2)
    class_with_confidence = 'PERSON' + str(int(confidence_values[index] * 100)) + '%'
    cv2.putText(img, class_with_confidence, (x, y-10), cv2.FONT_HERSHEY_COMPLEX_SMALL, 0.5, (255, 0, 0), 1)
Eclampsia answered 25/8, 2023 at 9:29 Comment(2)
Can you post traceback?Thermo
If you have a new question, please ask it by clicking the Ask Question button. Include a link to this question if it helps provide context. - From ReviewMaggie

© 2022 - 2024 — McMap. All rights reserved.