0

I want to detect playing cards and found .cfg and .weights for it. Classes has 52cards names. Following code is giving index out of range error. I couldn't understand the outputs of Yolo and how to get the detected labels. I am new to this, have been trying to understand. Can someone please help!

import cv2
import numpy as np
# Load Yolo
net = cv2.dnn.readNet("yolocards_608.weights", "yolocards.cfg")
classes = []
with open("cards.names", "r") as f:
    classes = [line.strip() for line in f.readlines()]
layer_names = net.getLayerNames()
output_layers = [layer_names[i[0] - 1] for i in net.getUnconnectedOutLayers()]
colors = np.random.uniform(0, 255, size=(len(classes), 3))

# Loading image
img = cv2.imread("playing_cards_image.jpg")
img = cv2.resize(img, None, fx=0.4, fy=0.4)
height, width, channels = img.shape

# Detecting objects
blob = cv2.dnn.blobFromImage(img, 0.00392, (416, 416), (0, 0, 0), True, crop=False)
net.setInput(blob)
outs = net.forward(output_layers)

# Showing informations on the screen
class_ids = []
confidences = []
boxes = []
for out in outs:
    print(out.shape)
    for detection in out:
        scores = detection[:]
        class_id = np.argmax(scores)
        confidence = scores[class_id]
        if confidence > 0.5:
            # Object detected
            center_x = int(detection[0] * width)
            center_y = int(detection[1] * height)
            w = int(detection[2] * width)
            h = int(detection[3] * height)
            # Rectangle coordinates
            x = int(center_x - w / 2)
            y = int(center_y - h / 2)
            boxes.append([x, y, w, h])
            confidences.append(float(confidence))
            class_ids.append(class_id)

indexes = cv2.dnn.NMSBoxes(boxes, confidences, 0.5, 0.4)
font = cv2.FONT_HERSHEY_PLAIN
for j in range(len(boxes)):
    if i in indexes:
        x, y, w, h = boxes[i]
        print(class_ids[i])
        label = str(classes[class_ids[i]])
        print(label)
        color = colors[i]
        cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
        cv2.putText(img, label, (x, y + 30), font, 3, color, 3)

error:
0
Ah
---------------------------------------------------------------------------
IndexError                                Traceback (most recent call last)
<ipython-input-46-adaf82305ab8> in <module>
      6         label = str(classes[class_ids[i]])
      7         print(label)
----> 8         color = colors[i]
      9         cv2.rectangle(img, (x, y), (x + w, y + h), color, 2)
     10         cv2.putText(img, label, (x, y + 30), font, 3, color, 3)

IndexError: index 52 is out of bounds for axis 0 with size 52
  • Probably you are calling 52nd element in the array but 52 element array should have maximum element value is 51 not 52. You are exceeding the array element number by calling 52nd element – Yunus Temurlenk Apr 28 '20 at 05:22
  • @Yunus No actually, the i value has very large numbers also. I don't why 'i' values have large numbers more than 52. – ravi teja Apr 28 '20 at 07:46
  • Either you stick to your Title or ask for help on the error. Please be specific. – Pe Dro May 07 '20 at 08:07

0 Answers0