为什么我会得到多个边界框?

2024-04-27 02:47:18 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试将图像对象检测器转换为视频对象检测器

但是,我得到了多个边界框,我不知道为什么

似乎视频的第一帧具有正确数量的边界框,即1。但是当它循环时,函数draw_boxes输出具有多个或重叠边界框的图像

如果你能帮忙,我将不胜感激。谢谢

以下是一些框架的示例:

enter image description here

代码如下:


for i in tqdm(range(nb_frames)):

        _, frame = video_reader.read()
        cv2.imwrite("framey.jpg", frame)
        filename = "framey.jpg"

        image, image_w, image_h = load_image_pixels(filename, (input_w, input_h))
        yhat = model.predict(image)

        for i in range(len(yhat)):
            # decode the output of the network
            boxes += decode_netout(yhat[i][0], anchors[i], class_threshold, input_h, input_w)
         # correct the sizes of the bounding boxes for the shape of the image
        correct_yolo_boxes(boxes, image_h, image_w, input_h, input_w)
         # suppress non-maximal boxes
        do_nms(boxes, 0.5)

         # get the details of the detected objects
        v_boxes, v_labels, v_scores = get_boxes(boxes, labels, class_threshold)

         # draw what we found
        imagex = draw_boxes(filename, v_boxes, v_labels, v_scores)

        video_writer.write(imagex)

video_reader.release()
video_writer.release()

下面是显示上图的函数:


def draw_boxes(filename, v_boxes, v_labels, v_scores):
        # load the image
        data = pyplot.imread(filename)
        # plot the image
        pyplot.imshow(data)
        # get the context for drawing boxes
        ax = pyplot.gca()
        # plot each box
        for i in range(len(v_boxes)):
                box = v_boxes[i]
                # get coordinates
                y1, x1, y2, x2 = box.ymin, box.xmin, box.ymax, box.xmax
                # calculate width and height of the box
                width, height = x2 - x1, y2 - y1
                # create the shape
                rect = Rectangle((x1, y1), width, height, fill=False, color='white')
                # draw the box
                ax.add_patch(rect)
                # draw text and score in top left corner
                label = "%s (%.3f)" % (v_labels[i], v_scores[i])
                pyplot.text(x1, y1, label, color='white')
        # show the plot
        pyplot.savefig('detected.jpg')
        filename = "detected.jpg"
        image = load_img(filename)
        image_array = img_to_array(image)
        image_array = (image_array*255).astype(np.uint8)

        return image_array

Tags: oftheinimageboxforinputlabels
1条回答
网友
1楼 · 发布于 2024-04-27 02:47:18

因此,错误出现在“draw_Box”函数中

我改了“画框”,效果不错


def draw_bounding_boxes(image, v_boxes, v_labels, v_scores):

    for i in range(len(v_boxes)):
        box = v_boxes[i]
        y1, x1, y2, x2 = box.ymin, box.xmin, box.ymax, box.xmax
        width, height = x2 - x1, y2 - y1
        label = "%s (%.3f)" % (v_labels[i], v_scores[i])
        region = np.array([[x1 - 3,        y1], 
                           [x1-3,        y1 - height-26], 
                           [x1+width+13, y1-height-26], 
                           [x1+width+13, y1]], dtype='int32')
        cv2.rectangle(image, (x1, y1), (x2, y2), (255, 0, 0), 5)
        cv2.fillPoly(image,[region], (255, 0, 0))
        cv2.putText(image,
                    label,
                    (x1+13, y1-13),
                    cv2.FONT_HERSHEY_SIMPLEX,
                    1e-3 * image.shape[0],
                    (0,0,0),
                    2)
    return image

相关问题 更多 >