Example #1
0
def compute_rectangels(H,
                       confidences,
                       boxes,
                       use_stitching=False,
                       rnn_len=1,
                       min_conf=0.1,
                       show_removed=True,
                       tau=0.25):
    num_cells = H["grid_height"] * H["grid_width"]
    boxes_r = np.reshape(boxes,
                         (-1, H["grid_height"], H["grid_width"], rnn_len, 4))
    confidences_r = np.reshape(
        confidences,
        (-1, H["grid_height"], H["grid_width"], rnn_len, H['num_classes']))
    cell_pix_size = H['region_size']
    all_rects = [[[] for _ in range(H["grid_width"])]
                 for _ in range(H["grid_height"])]
    for n in range(rnn_len):
        for y in range(H["grid_height"]):
            for x in range(H["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y
                w = bbox[2]
                h = bbox[3]
                conf = np.max(confidences_r[0, y, x, n, 1:])
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    all_rects_r = [r for row in all_rects for cell in row for r in cell]
    if use_stitching:
        from utils.stitch_wrapper import stitch_rects
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = all_rects_r
Example #2
0
def add_rectangles(H, orig_image, confidences, boxes, use_stitching=False, rnn_len=1, min_conf=0.1, show_removed=True, tau=0.25, show_suppressed=True):
    image = np.copy(orig_image[0])
    num_cells = H["grid_height"] * H["grid_width"]
    boxes_r = np.reshape(boxes, (-1,
                                 H["grid_height"],
                                 H["grid_width"],
                                 rnn_len,
                                 4))
    confidences_r = np.reshape(confidences, (-1,
                                             H["grid_height"],
                                             H["grid_width"],
                                             rnn_len,
                                             H['num_classes']))
    cell_pix_size = H['region_size']
    all_rects = [[[] for _ in range(H["grid_width"])] for _ in range(H["grid_height"])]
    for n in range(rnn_len):
        for y in range(H["grid_height"]):
            for x in range(H["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                abs_cx = int(bbox[0]) + cell_pix_size/2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size/2 + cell_pix_size * y
                w = bbox[2]
                h = bbox[3]
                conf = np.max(confidences_r[0, y, x, n, 1:])
                all_rects[y][x].append(Rect(abs_cx,abs_cy,w,h,conf))

    all_rects_r = [r for row in all_rects for cell in row for r in cell]
    if use_stitching:
        from utils.stitch_wrapper import stitch_rects
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = all_rects_r

    if show_suppressed:
        pairs = [(all_rects_r, (255, 0, 0))]
    else:
        pairs = []
    pairs.append((acc_rects, (0, 255, 0)))
    for rect_set, color in pairs:
        for rect in rect_set:
            if rect.confidence > min_conf:
                cv2.rectangle(image,
                    (int(rect.cx)-int(rect.width/2), int(rect.cy)-int(rect.height/2)),
                    (int(rect.cx)+int(rect.width/2), int(rect.cy)+int(rect.height/2)),
                    color,
                    2)

    rects = []
    for rect in acc_rects:
        r = al.AnnoRect()
        r.x1 = rect.cx - rect.width/2.
        r.x2 = rect.cx + rect.width/2.
        r.y1 = rect.cy - rect.height/2.
        r.y2 = rect.cy + rect.height/2.
        r.score = rect.true_confidence
        rects.append(r)

    return image, rects
Example #3
0
def add_rectangles(H,
                   orig_image,
                   confidences,
                   boxes,
                   arch,
                   use_stitching=False,
                   rnn_len=1,
                   min_conf=0.5,
                   tau=0.25):
    from utils.rect import Rect
    from utils.stitch_wrapper import stitch_rects
    import numpy as np
    image = np.copy(orig_image[0])
    boxes_r = np.reshape(
        boxes, (-1, arch["grid_height"], arch["grid_width"], rnn_len, 4))
    confidences_r = np.reshape(
        confidences, (-1, arch["grid_height"], arch["grid_width"], rnn_len, 2))
    cell_pix_size = H['arch']['region_size']
    all_rects = [[[] for _ in range(arch["grid_width"])]
                 for _ in range(arch["grid_height"])]
    for n in range(0, H['arch']['rnn_len']):
        for y in range(arch["grid_height"]):
            for x in range(arch["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                conf = confidences_r[0, y, x, n, 1]
                abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y
                h = max(1, bbox[3])
                w = max(1, bbox[2])
                #w = h * 0.4
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    if use_stitching:
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = [
            r for row in all_rects for cell in row for r in cell
            if r.confidence > 0.1
        ]

    for rect in acc_rects:
        if rect.confidence > min_conf:
            cv2.rectangle(image, (rect.cx - int(rect.width / 2),
                                  rect.cy - int(rect.height / 2)),
                          (rect.cx + int(rect.width / 2),
                           rect.cy + int(rect.height / 2)), (0, 255, 0), 2)

    rects = []
    for rect in acc_rects:
        r = al.AnnoRect()
        r.x1 = rect.cx - rect.width / 2.
        r.x2 = rect.cx + rect.width / 2.
        r.y1 = rect.cy - rect.height / 2.
        r.y2 = rect.cy + rect.height / 2.
        r.score = rect.true_confidence
        rects.append(r)

    return image, rects
Example #4
0
def add_rectangles(H, orig_image, confidences, boxes, arch, use_stitching=False, rnn_len=1, min_conf=0.5, tau=0.25):
    from utils.rect import Rect
    from utils.stitch_wrapper import stitch_rects
    import numpy as np
    image = np.copy(orig_image[0])
    boxes_r = np.reshape(boxes, (-1,
                                 arch["grid_height"],
                                 arch["grid_width"],
                                 rnn_len,
                                 4))
    confidences_r = np.reshape(confidences, (-1,
                                             arch["grid_height"],
                                             arch["grid_width"],
                                             rnn_len,
                                             2))
    cell_pix_size = H['arch']['region_size']
    all_rects = [[[] for _ in range(arch["grid_width"])] for _ in range(arch["grid_height"])]
    for n in range(0, H['arch']['rnn_len']):
        for y in range(arch["grid_height"]):
            for x in range(arch["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                conf = confidences_r[0, y, x, n, 1]
                abs_cx = int(bbox[0]) + cell_pix_size/2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size/2 + cell_pix_size * y
                h = max(1, bbox[3])
                w = max(1, bbox[2])
                #w = h * 0.4
                all_rects[y][x].append(Rect(abs_cx,abs_cy,w,h,conf))

    if use_stitching:
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = [r for row in all_rects for cell in row for r in cell if r.confidence > 0.1]


    for rect in acc_rects:
        if rect.confidence > min_conf:
            cv2.rectangle(image,
                (rect.cx-int(rect.width/2), rect.cy-int(rect.height/2)),
                (rect.cx+int(rect.width/2), rect.cy+int(rect.height/2)),
                (0,255,0),
                2)

    rects = []
    for rect in acc_rects:
        r = al.AnnoRect()
        r.x1 = rect.cx - rect.width/2.
        r.x2 = rect.cx + rect.width/2.
        r.y1 = rect.cy - rect.height/2.
        r.y2 = rect.cy + rect.height/2.
        r.score = rect.true_confidence
        rects.append(r)

    return image, rects
Example #5
0
    def _assign_rects(self, H, confidences, boxes, use_stitching=False,
                      rnn_len=1, min_conf=0.1, tau=0.25, min_area=100):
        boxes_r = np.reshape(boxes, (-1,
                                     H["grid_height"],
                                     H["grid_width"],
                                     rnn_len,
                                     4))
        confidences_r = np.reshape(confidences, (-1,
                                                 H["grid_height"],
                                                 H["grid_width"],
                                                 rnn_len,
                                                 H['num_classes']))
        cell_pix_size = H['region_size']
        all_rects = [[[] for _ in range(H["grid_width"])]
                     for _ in range(H["grid_height"])]
        for n in range(rnn_len):
            for y in range(H["grid_height"]):
                for x in range(H["grid_width"]):
                    bbox = boxes_r[0, y, x, n, :]
                    abs_cx = int(bbox[0]) + cell_pix_size/2 + cell_pix_size * x
                    abs_cy = int(bbox[1]) + cell_pix_size/2 + cell_pix_size * y
                    w = bbox[2]
                    h = bbox[3]
                    conf = np.max(confidences_r[0, y, x, n, 1:])
                    all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

        all_rects_r = [r for row in all_rects for cell in row for r in cell]
        if use_stitching:
            from utils.stitch_wrapper import stitch_rects
            acc_rects = stitch_rects(all_rects, tau)
        else:
            acc_rects = all_rects_r
        
        bounding_boxes = [r for r in acc_rects
                if r.score > min_conf and r.area > min_area]

        return bounding_boxes
Example #6
0
def add_rectangles(H,
                   orig_image,
                   confidences,
                   boxes,
                   use_stitching=False,
                   rnn_len=1,
                   min_conf=0.1,
                   show_removed=True,
                   tau=0.25,
                   color_removed=(0, 0, 255),
                   color_acc=(0, 0, 255)):
    image = np.copy(orig_image[0])
    num_cells = H["grid_height"] * H["grid_width"]
    boxes_r = np.reshape(boxes,
                         (-1, H["grid_height"], H["grid_width"], rnn_len, 4))
    confidences_r = np.reshape(
        confidences,
        (-1, H["grid_height"], H["grid_width"], rnn_len, H['num_classes']))
    cell_pix_size = H['region_size']
    all_rects = [[[] for _ in range(H["grid_width"])]
                 for _ in range(H["grid_height"])]
    for n in range(rnn_len):
        for y in range(H["grid_height"]):
            for x in range(H["grid_width"]):
                bbox = boxes_r[0, y, x, n, :]
                abs_cx = int(bbox[0]) + cell_pix_size / 2 + cell_pix_size * x
                abs_cy = int(bbox[1]) + cell_pix_size / 2 + cell_pix_size * y
                w = bbox[2]
                h = bbox[3]
                conf = np.max(confidences_r[0, y, x, n, 1:])
                all_rects[y][x].append(Rect(abs_cx, abs_cy, w, h, conf))

    all_rects_r = [r for row in all_rects for cell in row for r in cell]
    if use_stitching:
        from utils.stitch_wrapper import stitch_rects
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = all_rects_r

    if not show_removed:
        all_rects_r = []

    pairs = [(all_rects_r, color_removed), (acc_rects, color_acc)]
    im = Image.fromarray(image.astype('uint8'))
    draw = ImageDraw.Draw(im)
    for rect_set, color in pairs:
        for rect in rect_set:
            if rect.confidence > min_conf:
                _draw_rect(draw, rect, color)

    image = np.array(im).astype('float32')

    rects = []
    for rect in acc_rects:
        r = al.AnnoRect()
        r.x1 = rect.cx - rect.width / 2.
        r.x2 = rect.cx + rect.width / 2.
        r.y1 = rect.cy - rect.height / 2.
        r.y2 = rect.cy + rect.height / 2.
        r.score = rect.true_confidence
        rects.append(r)

    return image, rects