Error in yolo detection

asked 2019-02-20 15:37:11 -0600

error: OpenCV(3.4.5) /io/opencv/modules/dnn/src/layers/region_layer.cpp:97: error: (-215:Assertion failed) inputs[0][3] == (1 + coords + classes)*anchors in function 'getMemoryShapes'

import cv2 as cv
import sys
import numpy as np
import os.path

# Initialize the parameters
confThreshold = 0.5  #Confidence threshold
nmsThreshold = 0.4   #Non-maximum suppression threshold
inpWidth = 416       #Width of network's input image
inpHeight = 416      #Height of network's input image

PATH = '/home/ivan/YOLO/VIDEO.mp4'

# Load names of classes
classesFile ='/home/ivan/YOLO/coco.names'
classes = None
with open(classesFile, 'rt') as f:
    classes = f.read().rstrip('\n').split('\n')

# Give the configuration and weight files for the model and load the network using them.
modelConfiguration = '/home/ivan/YOLO/yolov3.cfg'
modelWeights = '/home/ivan/YOLO/yolov3.weights'

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

# Get the names of the output layers
def getOutputsNames(net):
    # Get the names of all the layers in the network
    layersNames = net.getLayerNames()
    # Get the names of the output layers, i.e. the layers with unconnected outputs
    return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]

# Draw the predicted bounding box
def drawPred(classId, conf, left, top, right, bottom):
    # Draw a bounding box.
    cv.rectangle(frame, (left, top), (right, bottom), (0,255,0), 3)

#     print(right,left,top,bottom)
#     cv.circle(frame,(left+(right-left)//2,bottom+(top-bottom)//2), 3, (0,255,0), -1)

    label = '%.2f' % conf

    # Get the label for the class name and its confidence
    if classes:
        assert(classId < len(classes))
        label = '%s:%s' % (classes[classId], label)

    #Display the label at the top of the bounding box
    labelSize, baseLine = cv.getTextSize(label, cv.FONT_HERSHEY_SIMPLEX, 0.5, 1)
    top = max(top, labelSize[1])
    cv.rectangle(frame, (left, top - round(1.5*labelSize[1])), (left + round(1.5*labelSize[0]), top + baseLine), (255, 255, 255), cv.FILLED)
    cv.putText(frame, label, (left, top), cv.FONT_HERSHEY_SIMPLEX, 0.75, (0,0,0), 1)

def PerspectiveTransform(frame, left, top, right, bottom):

    # Draw a bounding box.
    cv.rectangle(frame, (left, top), (right, bottom), (0,255,0), 3)
    # Points of the corners of the bounding box
    pts1 = np.float32([[left,top],[right,top],[right,bottom],[left,top-bottom]])
    # Points of the corners of the perspective transformation 600*600
    pts2 = np.float32([[0,0],[600,0],[0,600],[600,600]])

    # Compute the perspective transform matrix and then apply it
    M = cv.getPerspectiveTransform(pts1,pts2)
    dst = cv.warpPerspective(frame,M,(600,600))

    # Return the warped image
    return dst   

# Remove the bounding boxes with low confidence using non-maxima suppression
def postprocess(frame, outs):
    frameHeight = frame.shape[0]
    frameWidth = frame.shape[1]

    # Scan through all the bounding boxes output from the network and keep only the
    # ones with high confidence scores. Assign the box's class label as the class with the highest score.
    classIds = []
    confidences = []
    boxes = []
    point = []
    for out in outs:
        for detection in out:
            scores = detection[5:]
            classId = np.argmax(scores)
            confidence = scores[classId]
            if confidence > confThreshold:
                center_x = int(detection[0 ...
(more)
edit retag flag offensive close merge delete

Comments

that's a lot of code, most of it being irrelevant here. can you try to reduce it to a minimal, reproducable example ?

berak gravatar imageberak ( 2019-02-21 03:40:07 -0600 )edit

This error write on this row outs = net.forward(getOutputsNames(net))

vanya_29_ gravatar imagevanya_29_ ( 2019-02-21 13:01:51 -0600 )edit

Have you tried to run OpenCV's object detection sample: object_detection.py?

dkurt gravatar imagedkurt ( 2019-02-26 03:24:18 -0600 )edit