def init_dim_grid(cell_width, cell_height, pos_ranges, dim_ranges): xpos_min, xpos_max = pos_ranges[0] ypos_min, ypos_max = pos_ranges[1] zpos_min, zpos_max = pos_ranges[2] xdim_min, xdim_max = dim_ranges[0] ydim_min, ydim_max = dim_ranges[1] zdim_min, zdim_max = dim_ranges[2] cell_regions = [] for iy in xrange(cell_height): for ix in xrange(cell_width): cidx = iy * cell_width + ix r = al.AnnoBox( x = (xpos_max - xpos_min) * np.random.random_sample() + xpos_min, y = (ypos_max - ypos_min) * np.random.random_sample() + ypos_min, z = (zpos_max - zpos_min) * np.random.random_sample() + zpos_min, w = (xdim_max - xdim_min) * np.random.random_sample() + xdim_min, h = (ydim_max - ydim_min) * np.random.random_sample() + ydim_min, d = (zdim_max - zdim_min) * np.random.random_sample() + zdim_min ) r.track_id = cidx cell_regions.append(r) return cell_regions
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) 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 origin_anno in annos: tiles = preprocess_image(deepcopy(origin_anno), H) for I, anno in tiles: 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) yield {"image": I, "boxes": boxes, "flags": flags}
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 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) 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 = cv2.imread(anno.imageName) cv2.cvtColor(I, cv2.COLOR_BGR2RGB) if len(I.shape) < 3: I = cv2.cvtColor(I, cv2.COLOR_GRAY2RGB) else: if len(I.shape) < 3: continue I = cv2.imread(anno.imageName) cv2.cvtColor(I, cv2.COLOR_BGR2RGB) if I.shape[0] != H["image_height"] or I.shape[1] != H[ "image_width"]: if epoch == 0: anno = rescale_boxes(I.shape, anno, H["image_height"], H["image_width"]) I = cv2.resize(I, (H["image_width"], H["image_height"]), interpolation=cv2.INTER_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)
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 ]
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
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
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
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) 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: I = imread(anno.imageName) if I.shape[2] == 4: I = I[:, :, :3] if I.shape[0] != H["image_height"] or I.shape[1] != H[ "image_width"]: if epoch == 0: 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}
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)
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