使用opencv+yolo检测目标时,输出的图像中含有前一次检测时的框框,怎么回事?

weightsPath = os.path.join('yolov3.weights')  # 权重文件
configPath = os.path.join('yolov3.cfg')  # 配置文件
labelsPath = os.path.join( 'coco.names')  # label名称
# 测试图像 imgPath = os.path.join('dog.jpg')
CONFIDENCE = 0.5  # 过滤弱检测的最小概率
THRESHOLD = 0.4  # 非最大值抑制阈值


#初始化一些参数
LABELS = open(labelsPath).read().strip().split("\n")
boxes = []
confidences = []
classIDs = []

#加载 网络配置与训练的权重文件 构建网络
net = cv.dnn.readNetFromDarknet(configPath,weightsPath)
input_path='D:/test/ori/'
output_path='D:/test/result/'
number=range(3)
for index in number:
    # 读入待检测的图像
    img_path = input_path + str(index) + '.jpg'
    image = cv.imread(img_path)
    # 得到图像的高和宽
    (H, W) = image.shape[0:2]
    #boxes=[]

    # 得到 YOLO需要的输出层
    ln = net.getLayerNames()
    out = net.getUnconnectedOutLayers()  # 得到未连接层得序号  [[200] /n [267]  /n [400] ]
    x = []
    for i in out:  # 1=[200]
        x.append(ln[i[0] - 1])  # i[0]-1    取out中的数字  [200][0]=200  ln(199)= 'yolo_82'
    ln = x
    # ln  =  ['yolo_82', 'yolo_94', 'yolo_106']  得到 YOLO需要的输出层


    # 从输入图像构造一个blob,然后通过加载的模型,给我们提供边界框和相关概率
    # blobFromImage(image, scalefactor=None, size=None, mean=None, swapRB=None, crop=None, ddepth=None)
    blob = cv.dnn.blobFromImage(image, 1 / 255.0, (416, 416), swapRB=True,
                                crop=False)  # 构造了一个blob图像,对原图像进行了图像的归一化,缩放了尺寸 ,对应训练模型

    net.setInput(blob)  # 将blob设为输入??? 具体作用还不是很清楚
    layerOutputs = net.forward(ln)  # ln此时为输出层名称  ,向前传播  得到检测结果

    for output in layerOutputs:  # 对三个输出层 循环
        for detection in output:  # 对每个输出层中的每个检测框循环
            scores = detection[5:]  # detection=[x,y,h,w,c,class1,class2] scores取第6位至最后
            classID = np.argmax(scores)  # np.argmax反馈最大值的索引
            confidence = scores[classID]
            if confidence > 0.5:  # 过滤掉那些置信度较小的检测结果
                box = detection[0:4] * np.array([W, H, W, H])
                (centerX, centerY, width, height) = box.astype("int")
                # 边框的左上角
                x = int(centerX - (width / 2))
                y = int(centerY - (height / 2))
                # 更新检测出来的框
                boxes.append([x, y, int(width), int(height)])
                confidences.append(float(confidence))
                classIDs.append(classID)

    idxs = cv.dnn.NMSBoxes(boxes, confidences, 0.2, 0.3)
    box_seq = idxs.flatten()  # [ 2  9  7 10  6  5  4]
    if len(idxs) > 0:
        for seq in box_seq:
            print(seq)
            (x, y) = (boxes[seq][0], boxes[seq][1])  # 框左上角
            (w, h) = (boxes[seq][2], boxes[seq][3])  # 框宽高
            if classIDs[seq] == 0:  # 根据类别设定框的颜色
                color = [0, 0, 255]
            else:
                color = [255, 0, 0]
            cv.rectangle(image, (x, y), (x + w, y + h), color, 2)  # 画框
            text = "{}: {:.4f}".format(LABELS[classIDs[seq]], confidences[seq])
            cv.putText(image, text, (x, y - 5), cv.FONT_HERSHEY_SIMPLEX,2,color,3)
            #cv.putText(image, text, (x, y - 5), cv.FONT_HERSHEY_SIMPLEX, 3, cv.Font_Overstriking,3,color, 1)  # 写字
        save_path = output_path + str(index) + '_change.jpg'
        #print(str(index))
        cv.imwrite(save_path, image)

上式是代码,下图是出现问题的结果,请问有好心人帮帮忙吗?

你把boxes给注释了就这样子。把注释的boxes恢复就可以了,对每一张图需要重新将boxes设置为空就可以了

for index in number:
    # 读入待检测的图像
    img_path = input_path + str(index) + '.jpg'
    image = cv.imread(img_path)
    # 得到图像的高和宽
    (H, W) = image.shape[0:2]
    boxes=[]
    confidences=[]
    classIDs=[]