コード例 #1
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):
    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 stitch_wrapper import stitch_rects
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = all_rects_r

    pairs = [(all_rects_r, (255, 0, 0)), (acc_rects, (0, 255, 0))]
    for rect_set, color in pairs:
        for rect in rect_set:
            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)), color, 2)

    rects = []
    for rect in acc_rects:
        if rect.confidence > min_conf:
            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
 def invert(width, rects):
     """Inverts the rotation for 90 degrees.
     Args:
         width (int): width of rotated image.
         rects (list): The list of rectangles on rotated image.
     Returns (list):
         The list of annotations for original image.
     """
     return [
         al.AnnoRect(width - r.y2, r.x1, width - r.y1, r.x2) for r in rects
     ]
コード例 #3
0
 def do(image, anno=None):
     """
     Does the rotation for image and rectangles for 90 degrees counterclockwise.
     Args:
         image (Image): The target image to rotate.
         anno (Annotation): The annotations to be rotated with the image.
     Returns (tuple):
         Rotated image and annotations for it.
     """
     w = image.shape[1]
     new_image = imrotate(image, 90, reshape=True)
     if anno is not None:
         anno.rects = [al.AnnoRect(r.y1, w - r.x2, r.y2, w - r.x1) for r in anno.rects]
     return new_image, anno
コード例 #4
0
def get_cell_grid(cell_width, cell_height, region_size):

    cell_regions = []
    for iy in xrange(cell_height):
        for ix in xrange(cell_width):
            cidx = iy * cell_width + ix
            ox = (ix + 0.5) * region_size
            oy = (iy + 0.5) * region_size

            r = al.AnnoRect(ox - 0.5 * region_size, oy - 0.5 * region_size,
                            ox + 0.5 * region_size, oy + 0.5 * region_size)
            r.track_id = cidx

            cell_regions.append(r)

    return cell_regions
コード例 #5
0
def get_rectangles(H,
                   confidences,
                   boxes,
                   use_stitching=False,
                   rnn_len=1,
                   min_conf=0.1,
                   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 stitch_wrapper import stitch_rects
        acc_rects = stitch_rects(all_rects, tau)
    else:
        acc_rects = all_rects_r

    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.confidence
        all_rects.append(r)
        if rect.confidence > min_conf:
            rects.append(r)

    return rects
コード例 #6
0
def load_idl_tf(idlfile, H, jitter):
    """Take the idlfile and net configuration and create a generator
    that outputs a jittered version of a random image from the annolist
    that is mean corrected."""

    annolist = al.parse(
        idlfile,
        root_dir=H['data']['root_dir'] if 'root_dir' in H['data'] else './')

    augmenter = Augmentation()

    annos = []
    for anno in annolist:
        anno.imageName = os.path.join(
            os.path.dirname(os.path.realpath(idlfile)), anno.imageName)
        annos.append(anno)
    random.seed(0)
    if H['data']['truncate_data']:
        annos = annos[:10]
    for epoch in itertools.count():
        random.shuffle(annos)
        for anno in annos:
            try:
                if 'grayscale' in H and 'grayscale_prob' in H:
                    I = imread(anno.imageName,
                               mode='RGB' if
                               random.random() < H['grayscale_prob'] else 'L')
                    if len(I.shape) < 3:
                        I = cv2.cvtColor(I, cv2.COLOR_GRAY2RGB)
                else:
                    if len(I.shape) < 3:
                        continue
                    I = imread(anno.imageName, mode='RGB')

                labels = np.array([[r.x1, r.y1, r.x2, r.y2]
                                   for r in anno.rects])
                if len(labels) == 0:
                    labels = np.zeros((0, 4))

                I, labels, _ = augmenter(I, labels, np.zeros((len(labels), 1)))

                new_rects = []
                for box in labels:
                    r = al.AnnoRect()
                    r.x1, r.y1, r.x2, r.y2 = box
                    new_rects.append(r)

                new_anno = al.Annotation()
                new_anno.imageName = anno.imageName
                new_anno.imagePath = anno.imagePath
                new_anno.rects = new_rects
                anno = new_anno

                # img = I[:, :, (2,1,0)].copy()
                # for r in anno.rects:
                #     cv2.rectangle(img, tuple(map(int, (r.x1, r.y1))), tuple(map(int, (r.x2, r.y2))), (0,0,255))

                # cv2.imwrite('test.jpg', img)
                # pdb.set_trace()

                if I.shape[0] != H["image_height"] or I.shape[1] != H[
                        "image_width"]:
                    anno = rescale_boxes(I.shape, anno, H["image_height"],
                                         H["image_width"])
                    I = imresize(I, (H["image_height"], H["image_width"]),
                                 interp='cubic')
                if jitter:
                    jitter_scale_min = 0.9
                    jitter_scale_max = 1.1
                    jitter_offset = 16
                    I, anno = annotation_jitter(
                        I,
                        anno,
                        target_width=H["image_width"],
                        target_height=H["image_height"],
                        jitter_scale_min=jitter_scale_min,
                        jitter_scale_max=jitter_scale_max,
                        jitter_offset=jitter_offset)

                boxes, flags = annotation_to_h5(H, anno, H["grid_width"],
                                                H["grid_height"], H["rnn_len"])

                yield {"image": I, "boxes": boxes, "flags": flags}
            except Exception as exc:
                print(exc)
コード例 #7
0
 def inv(r):
     rotated_back = al.AnnoRect(width - r.y2, r.x1, width - r.y1, r.x2)
     rotated_back.score = r.score
     return rotated_back