Ejemplo n.º 1
0
def draw_all_detection(im_array, detections, class_names, scale, cfg, threshold=1e-1):
    """
    visualize all detections in one image
    :param im_array: [b=1 c h w] in rgb
    :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
    :param class_names: list of names in imdb
    :param scale: visualize the scaled image
    :return:
    """
    import cv2
    import random
    color_white = (255, 255, 255)
    im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
    # change to bgr
    im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
    for j, name in enumerate(class_names):
        if name == '__background__':
            continue
        color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256))  # generate a random color
        dets = detections[j]
        for det in dets:
            bbox = det[:4] * scale
            score = det[-1]
            if score < threshold:
                continue
            bbox = map(int, bbox)
            cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=color, thickness=2)
            cv2.putText(im, '%s %.3f' % (class_names[j], score), (bbox[0], bbox[1] + 10),
                        color=color_white, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5)
    return im
Ejemplo n.º 2
0
def show_boxes_with_nms(im, scores, boxes, classes, scale, config):
    import matplotlib.pyplot as plt
    dets_nms = []
    scores = scores.astype('f')
    boxes = boxes.astype('f')
    nms = gpu_nms_wrapper(config.TEST.NMS, 0)
    for j in range(1, scores.shape[1]):
        cls_scores = scores[:, j, np.newaxis]
        cls_boxes = boxes[:, 4:8] * scale
        cls_dets = np.hstack((cls_boxes, cls_scores))
        keep = nms(cls_dets)
        cls_dets = cls_dets[keep, :]
        cls_dets = cls_dets[cls_dets[:, -1] > 0.7, :]
        dets_nms.append(cls_dets)
    # visualize

    # show_boxes(im, dets_nms, classes, 1)
    import cv2
    im = image.transform_inverse(im, config.network.PIXEL_MEANS)
    im = cv2.cvtColor(im, cv2.COLOR_BGR2RGB)
    print 'image',im
    print 'det_nms',len(dets_nms),dets_nms
    print 'classes',len(classes)
    out_im = draw_boxes(im, dets_nms, classes, 1)

    #out_im = np.transpose(np.squeeze(out_im), (1,2,0))
    #print out_im.shape
    plt.imshow(out_im)
    plt.show()
Ejemplo n.º 3
0
def vis_all_detection(im_array, detections, class_names, scale, cfg, threshold=1e-3):
    """
    visualize all detections in one image
    :param im_array: [b=1 c h w] in rgb
    :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
    :param class_names: list of names in imdb
    :param scale: visualize the scaled image
    :return:
    """
    import matplotlib.pyplot as plt
    import random
    im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
    plt.imshow(im)
    for j, name in enumerate(class_names):
        if name == '__background__':
            continue
        color = (random.random(), random.random(), random.random())  # generate a random color
        dets = detections[j]
        for det in dets:
            bbox = det[:4] * scale
            score = det[-1]
            if score < threshold:
                continue
            rect = plt.Rectangle((bbox[0], bbox[1]),
                                 bbox[2] - bbox[0],
                                 bbox[3] - bbox[1], fill=False,
                                 edgecolor=color, linewidth=3.5)
            plt.gca().add_patch(rect)
            plt.gca().text(bbox[0], bbox[1] - 2,
                           '{:s} {:.3f}'.format(name, score),
                           bbox=dict(facecolor=color, alpha=0.5), fontsize=12, color='white')
    plt.show()
Ejemplo n.º 4
0
def draw_all_detection(im_array, detections, class_names, scale, cfg, threshold=1e-1):
    """
    visualize all detections in one image
    :param im_array: [b=1 c h w] in rgb
    :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
    :param class_names: list of names in imdb
    :param scale: visualize the scaled image
    :return:
    """
    import cv2
    import random
    color_white = (255, 255, 255)
    im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
    # change to bgr
    im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
    for j, name in enumerate(class_names):
        if name == '__background__':
            continue
        color = (random.randint(0, 256), random.randint(0, 256), random.randint(0, 256))  # generate a random color
        dets = detections[j]
        for det in dets:
            bbox = det[:4] * scale
            score = det[-1]
            if score < threshold:
                continue
            bbox = map(int, bbox)
            cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]), color=color, thickness=2)
            cv2.putText(im, '%s %.3f' % (class_names[j], score), (bbox[0], bbox[1] + 10),
                        color=color_white, fontFace=cv2.FONT_HERSHEY_COMPLEX, fontScale=0.5)
    return im
Ejemplo n.º 5
0
def vis_all_detection(im_array, detections, class_names, scale, cfg, threshold=1e-3):
    """
    visualize all detections in one image
    :param im_array: [b=1 c h w] in rgb
    :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
    :param class_names: list of names in imdb
    :param scale: visualize the scaled image
    :return:
    """
    import matplotlib.pyplot as plt
    import random
    im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
    plt.imshow(im)
    for j, name in enumerate(class_names):
        if name == '__background__':
            continue
        color = (random.random(), random.random(), random.random())  # generate a random color
        dets = detections[j]
        for det in dets:
            bbox = det[:4] * scale
            score = det[-1]
            if score < threshold:
                continue
            rect = plt.Rectangle((bbox[0], bbox[1]),
                                 bbox[2] - bbox[0],
                                 bbox[3] - bbox[1], fill=False,
                                 edgecolor=color, linewidth=3.5)
            plt.gca().add_patch(rect)
            plt.gca().text(bbox[0], bbox[1] - 2,
                           '{:s} {:.3f}'.format(name, score),
                           bbox=dict(facecolor=color, alpha=0.5), fontsize=12, color='white')
    plt.show()
Ejemplo n.º 6
0
def vis_all_mask(im_array, detections, masks, class_names, scale, cfg):
    """
    visualize all detections in one image
    :param im_array: [b=1 c h w] in rgb
    :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
    :param class_names: list of names in imdb
    :param scale: visualize the scaled image
    :return:
    """
    import matplotlib.pyplot as plt
    import random
    import cv2
    import os
    im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
    plt.cla()
    plt.axis('off')
    plt.imshow(im)
    for j, name in enumerate(class_names):
        if name == '__background__':
            continue
        dets = detections[j]
        msks = masks[j]
        for det, msk in zip(dets, msks):
            if det[-1] < 0.7:
                continue
            color = (random.random(), random.random(), random.random()
                     )  # generate a random color
            bbox = det[:4] * scale
            cod = np.zeros(4).astype(int)
            cod[0] = int(bbox[0])
            cod[1] = int(bbox[1])
            cod[2] = int(bbox[2])
            cod[3] = int(bbox[3])
            if im[cod[1]:cod[3], cod[0]:cod[2], 0].size > 0:
                msk = cv2.resize(msk, im[cod[1]:cod[3], cod[0]:cod[2],
                                         0].T.shape)
                bimsk = msk > cfg.BINARY_THRESH
                bimsk = bimsk.astype(int)
                bimsk = np.repeat(bimsk[:, :, np.newaxis], 3, axis=2)
                mskd = im[cod[1]:cod[3], cod[0]:cod[2], :] * bimsk
                clmsk = np.ones(bimsk.shape) * bimsk
                clmsk[:, :, 0] = clmsk[:, :, 0] * color[0] * 256
                clmsk[:, :, 1] = clmsk[:, :, 1] * color[1] * 256
                clmsk[:, :, 2] = clmsk[:, :, 2] * color[2] * 256
                im[cod[1]:cod[3], cod[0]:cod[2], :] = im[
                    cod[1]:cod[3], cod[0]:cod[2], :] + 0.8 * clmsk - 0.8 * mskd
            score = det[-1]
            plt.gca().text((bbox[2] + bbox[0]) / 2,
                           bbox[1],
                           '{:s} {:.3f}'.format(name, score),
                           bbox=dict(facecolor=color, alpha=0.5),
                           fontsize=12,
                           color='white')
    plt.imshow(im)
    plt.show()
Ejemplo n.º 7
0
def vis_rois_detection(im_array, detections, class_names, s, scale):
    """
    visualize all detections in one image
    :param im_array: [b=1 c h w] in rgb
    :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
    :param class_names: list of names in imdb
    :param scale: visualize the scaled image
    :return:
    """
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    from matplotlib.pyplot import savefig
    import random
    a = [103.06, 115.9, 123.15]
    a = np.array(a)
    im = image.transform_inverse(im_array, a)
    plt.imshow(im)
    #  print class_names.shape
    for j in range(class_names.shape[0]):
        if class_names[j] == 0:
            continue
        color = (random.random(), random.random(), random.random()
                 )  # generate a random color
        dets = detections[j]

        det = dets

        bbox = det[0:]
        score = s[j]
        if score > 0.5:
            rect = plt.Rectangle((bbox[0], bbox[1]),
                                 bbox[2] - bbox[0],
                                 bbox[3] - bbox[1],
                                 fill=False,
                                 edgecolor=color,
                                 linewidth=3.5)
            plt.gca().add_patch(rect)
            plt.gca().text(bbox[0],
                           bbox[1] - 2,
                           '{:s} {:.3f}'.format(str(class_names[j]), score),
                           bbox=dict(facecolor=color, alpha=0.5),
                           fontsize=12,
                           color='white')
    plt.show()
    name = np.mean(im)

    savefig('vis/' + str(name) + '.png')
    plt.clf()
    plt.cla()

    plt.close(0)
Ejemplo n.º 8
0
def get_rpn_batch(roidb, cfg):
    """
    prototype for rpn batch: data, im_info, gt_boxes
    :param roidb: ['image', 'flipped'] + ['gt_boxes', 'boxes', 'gt_classes']
    :return: data, label
    """
    assert len(roidb) == 1, 'Single batch only'
    imgs, roidb = get_image(roidb, cfg)
    im_array = imgs[0]
    im_info = np.array([roidb[0]['im_info']], dtype=np.float32)

    inv_im_array = transform_inverse(im_array, cfg.network.PIXEL_MEANS)

    # gt boxes: (x1, y1, x2, y2, cls)
    if roidb[0]['gt_classes'].size > 0:
        gt_inds = np.where(roidb[0]['gt_classes'] != 0)[0]
        gt_boxes = np.empty((roidb[0]['boxes'].shape[0], 5), dtype=np.float32)
        gt_boxes[:, 0:4] = roidb[0]['boxes'][gt_inds, :]
        gt_boxes[:, 4] = roidb[0]['gt_classes'][gt_inds]
    else:
        gt_boxes = np.empty((0, 5), dtype=np.float32)

    # print 'im_info[0] = ', im_info[0]
    # print 'roidb[0].keys() = ', roidb[0].keys()
    # print "gt_boxes = ", gt_boxes
    # print "im_array.shape = ", im_array.shape
    # print "inv_im_array.shape = ", inv_im_array.shape
    # print "roidb[0]['image'] = ", roidb[0]['image']
    # print "roidb[0]['boxes'] = ", roidb[0]['boxes']
    # print "roidb[0]['width'], roidb[0]['height'] = ", roidb[0]['width'], roidb[0]['height']
    # print '-----------'

    # Save image for debugging
    _, img_name = osp.split(roidb[0]['image'])
    img_out_path = osp.join('debug', img_name)
    im = Image.fromarray(inv_im_array)
    draw = ImageDraw.Draw(im)
    n_boxes, _ = gt_boxes.shape
    for i in range(n_boxes):
        draw.rectangle(gt_boxes[i, 0:4], outline='yellow')
    del draw

    im.save(img_out_path)

    data = {'data': im_array, 'im_info': im_info}
    label = {'gt_boxes': gt_boxes}

    return data, label
Ejemplo n.º 9
0
def vis_all_detection(im_array, detections, class_names, scale, cfg, threshold = 1e-3):
    import matplotlib.pyplot as plt
    import random
    im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
    plt.imshow(im)
    for j, name in enumerate(class_names):
        if name == '__background__':
            continue
        color = (random.random(), random.random(),random.random())
        dets = detections[j]
        for det in dets:
            bbox = det[:4] * scale
            score = det[-1]
            if score < threshold:
                continue
            rec = plt.Rectangle((bbox[0],bbox[1]),bbox[2]-bbox[0],bbox[3]-bbox[1],fill = False,
                                edgecolor = color, linewidth = 3.5)
            plt.gca().add_patch(rect)
            plt.gca().text(bbox[0],bbox[1]-2,
                           '{:s} {:.3f}'.format(name, score),
                           bbox = dict(facecolor = color, alpha = 0.5), fontsize = 12, color = 'white')
Ejemplo n.º 10
0
def vis_all_detection(im_array, detections, scale):
    """
    visualize all detections in one image
    :param im_array: [b=1 c h w] in rgb
    :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
    :param class_names: list of names in imdb
    :param scale: visualize the scaled image
    :return:
    """
    import matplotlib
    matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    from matplotlib.pyplot import savefig
    import random
    a = [103.06, 115.9, 123.15]
    a = np.array(a)
    im = image.transform_inverse(im_array, a)
    plt.imshow(im)
    for j in range(len(detections)):
        color = (random.random(), random.random(), random.random()
                 )  # generate a random color
        dets = detections[j]
        det = dets
        bbox = det[1:]
        score = det[0]
        rect = plt.Rectangle((bbox[0], bbox[1]),
                             bbox[2] - bbox[0],
                             bbox[3] - bbox[1],
                             fill=False,
                             edgecolor=color,
                             linewidth=3.5)
        plt.gca().add_patch(rect)

    plt.show()
    name = np.mean(im)
    savefig('vis/' + str(name) + '.png')
    plt.clf()
    plt.cla()

    plt.close(0)
def vis_double_all_detection(im_array,
                             detections,
                             ref_im_array,
                             ref_detections,
                             class_names,
                             scale,
                             cfg,
                             threshold=1e-4):
    """
    visualize all detections in one image
    :param im_array: [b=1 c h w] in rgb
    :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
    :param class_names: list of names in imdb
    :param scale: visualize the scaled image
    :return:
    """
    import matplotlib.pyplot as plt
    import random
    im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
    ref_im = image.transform_inverse(ref_im_array, cfg.network.PIXEL_MEANS)
    fig, axeslist = plt.subplots(ncols=2,
                                 nrows=2,
                                 figsize=(10, 8),
                                 tight_layout={'pad': 0})

    figures = {'cur': im, 'ref': ref_im}
    detections_dict = {'cur': detections, 'ref': ref_detections}
    for ind, title in enumerate(figures):
        im = figures[title]
        detections = detections_dict[title]
        axeslist.ravel()[ind].imshow(im)
        axeslist.ravel()[ind].set_title(title)
        axeslist.ravel()[ind].set_axis_off()
        for j, name in enumerate(class_names):
            if name == '__background__':
                continue
            origin_color = (random.random(), random.random(), random.random()
                            )  # generate a random color
            target_color = (random.random(), random.random(), random.random()
                            )  # generate a random color
            dets = detections[j]
            for det in dets:
                bbox = det[:4] * scale
                score = det[4]
                condition_score = det[-1]
                if score < threshold or score == 1:
                    continue
                linewidth = 0.5
                # if score == 1:
                #     linewidth = 3.5
                # elif score == 2:
                #     linewidth = 1.5
                color = origin_color
                if score > 1:
                    color = target_color
                    linewidth = 2.5
                    score -= 2
                rect = plt.Rectangle((bbox[0], bbox[1]),
                                     bbox[2] - bbox[0],
                                     bbox[3] - bbox[1],
                                     fill=False,
                                     edgecolor=color,
                                     linewidth=linewidth,
                                     alpha=0.7)
                axeslist.ravel()[ind].add_patch(rect)
                axeslist.ravel()[ind].text(bbox[0],
                                           bbox[1] - 2,
                                           '{:s} {:.3f} {:.3f}'.format(
                                               name, score, condition_score),
                                           bbox=dict(facecolor=color,
                                                     alpha=0.5),
                                           fontsize=8,
                                           color='white')
    for i, title in enumerate(figures):
        ind = i + 2
        im = figures[title]
        detections = detections_dict[title]
        axeslist.ravel()[ind].imshow(im)
        axeslist.ravel()[ind].set_title(title)
        axeslist.ravel()[ind].set_axis_off()
        for j, name in enumerate(class_names):
            if name == '__background__':
                continue
            origin_color = (random.random(), random.random(), random.random()
                            )  # generate a random color
            target_color = (random.random(), random.random(), random.random())
            dets = detections[j]
            for det in dets:
                bbox = det[:4] * scale
                score = det[4]
                condition_score = det[-1]
                if score < 1:
                    continue
                linewidth = 0.5
                if score == 1:
                    linewidth = 3.5
                    color = origin_color
                elif score > 1:
                    linewidth = 1.5
                    color = target_color
                    score -= 2
                rect = plt.Rectangle((bbox[0], bbox[1]),
                                     bbox[2] - bbox[0],
                                     bbox[3] - bbox[1],
                                     fill=False,
                                     edgecolor=color,
                                     linewidth=linewidth,
                                     alpha=0.7)
                axeslist.ravel()[ind].add_patch(rect)
                axeslist.ravel()[ind].text(bbox[0],
                                           bbox[1] - 2,
                                           '{:s} {:.3f} {:.3f}'.format(
                                               name, score, condition_score),
                                           bbox=dict(facecolor=color,
                                                     alpha=0.5),
                                           fontsize=8,
                                           color='white')
    # plt.subplots_adjust(wspace=0, hspace=0.05)
    plt.show()
Ejemplo n.º 12
0
def draw_all_detection(im_array,
                       detections,
                       class_names,
                       scale,
                       cfg,
                       threshold=0.1):
    """
    visualize all detections in one image
    :param im_array: [b=1 c h w] in rgb
    :param detections: [ numpy.ndarray([[x1 y1 x2 y2 score]]) for j in classes ]
    :param class_names: list of names in imdb
    :param scale: visualize the scaled image
    :return:
    """
    import cv2
    import random
    color_white = (255, 255, 255)
    from utils import image
    im = image.transform_inverse(im_array, cfg.network.PIXEL_MEANS)
    # change to bgr
    im = cv2.cvtColor(im, cv2.COLOR_RGB2BGR)
    for j, name in enumerate(class_names):
        if name == '__background__':
            continue
        color = (random.randint(0, 256), random.randint(0, 256),
                 random.randint(0, 256))  # generate a random color
        if name == 'airplane' or name == 'fox' or name == 'domestic_cat' or name == 'giant_panda':
            color = (0, 255, 0)
        else:
            color = (0, 0, 255)
        dets = detections[j]
        for det in dets:
            bbox = det[:4] * scale
            score = det[-1]
            if score < threshold:
                continue
            bbox = map(int, bbox)
            text = '%s %.3f' % (class_names[j], score)
            font = cv2.FONT_HERSHEY_COMPLEX
            # font_scale = 1.5
            font_scale = 1.0
            rectangle_bgr = (255, 0, 0)

            (text_width, text_height) = cv2.getTextSize(text,
                                                        font,
                                                        fontScale=font_scale,
                                                        thickness=2)[0]
            # make the coords of the box with a small padding of two pixels
            box_coords = ((bbox[0], bbox[1] + 30),
                          (bbox[0] + text_width - 2,
                           bbox[1] + 30 - text_height - 2))
            cv2.rectangle(im, box_coords[0], box_coords[1], rectangle_bgr,
                          cv2.FILLED)

            cv2.rectangle(im, (bbox[0], bbox[1]), (bbox[2], bbox[3]),
                          color=color,
                          thickness=3)
            cv2.putText(im,
                        '%s %.3f' % (class_names[j], score),
                        (bbox[0], bbox[1] + 30),
                        color=color_white,
                        fontFace=cv2.FONT_HERSHEY_COMPLEX,
                        fontScale=font_scale,
                        thickness=2)
    return im