yolov3_with_opencv
yolov3_with_opencv copied to clipboard
安装好环境后,运行代码出错
outs = net.forward(getOutputsNames(net))
File "F:\刘毅\研究生\机器学习\python\yolov3_with_opencv\object_detection_yolo.py", line 42, in getOutputsNames
return [layersNames[i[0] - 1] for i in net.getUnconnectedOutLayers()]
File "F:\刘毅\研究生\机器学习\python\yolov3_with_opencv\object_detection_yolo.py", line 42, in
我也是,有什么解决办法没
There exists some issues in the code, here is the part fixed by me.
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()]
# In some OpenCV versions, getUnconnectedOutLayers() returns a list of int, in others, a list of numpy arrays
unconnectedOutLayers = net.getUnconnectedOutLayers()
if isinstance(unconnectedOutLayers[0], np.ndarray):
# OpenCV version returns indices as numpy arrays
return [layersNames[i[0] - 1] for i in unconnectedOutLayers]
else:
# OpenCV version returns indices as integers
return [layersNames[i - 1] for i in unconnectedOutLayers]
and
# Remove the bounding boxes with low confidence using non-maxima suppression
def postprocess(frame, outs):
frameHeight = frame.shape[0]
frameWidth = frame.shape[1]
classIds = []
confidences = []
boxes = []
# 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 = []
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] * frameWidth)
center_y = int(detection[1] * frameHeight)
width = int(detection[2] * frameWidth)
height = int(detection[3] * frameHeight)
left = int(center_x - width / 2)
top = int(center_y - height / 2)
classIds.append(classId)
confidences.append(float(confidence))
boxes.append([left, top, width, height])
# Perform non maximum suppression to eliminate redundant overlapping boxes with
# lower confidences.
indices = cv.dnn.NMSBoxes(boxes, confidences, confThreshold, nmsThreshold)
for i in indices:
if isinstance(i, np.ndarray):
i = i[0]
box = boxes[i]
left = box[0]
top = box[1]
width = box[2]
height = box[3]
drawPred(classIds[i], confidences[i], left, top, left + width, top + height)