почему я получаю несколько ограничивающих рамок? - PullRequest
0 голосов
/ 26 февраля 2020

Я пытаюсь превратить детектор объектов для изображений в детектор объектов для видео.

Но я получаю несколько ограничивающих рамок и не знаю почему.

Похоже, что в первом кадре видео указано правильное количество ограничивающих прямоугольников, а именно 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

1 Ответ

0 голосов
/ 04 марта 2020

Итак, ошибка была в функции 'draw_boxes'.

Я изменил draw_boxes, и это сработало.


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
...