def crop_sample(sample, contextFactor=2): """ Given a sample image with bounding box, this method returns the image crop at the bounding box location with twice the width and height for context. """ output_sample = {} opts = {} image, bb = sample['image'], sample['bb'] orig_bbox = BoundingBox(bb[0], bb[1], bb[2], bb[3]) orig_bbox.kContextFactor = contextFactor (output_image, pad_image_location, edge_spacing_x, edge_spacing_y) = cropPadImage(orig_bbox, image) new_bbox = BoundingBox(0, 0, 0, 0) new_bbox.kContextFactor = contextFactor new_bbox = new_bbox.recenter(pad_image_location, edge_spacing_x, edge_spacing_y, new_bbox) output_sample['image'] = output_image output_sample['bb'] = new_bbox.get_bb_list() # additional options for visualization opts['edge_spacing_x'] = edge_spacing_x opts['edge_spacing_y'] = edge_spacing_y opts['search_location'] = pad_image_location opts['search_region'] = output_image return output_sample, opts
def shift_crop_training_sample(sample, bb_params): """ Given an image with bounding box, this method randomly shifts the box and generates a training example. It returns current image crop with shifted box (with respect to current image). """ output_sample = {} opts = {} currimg = sample['image'] currimg_x2 = sample['image'] currbb = sample['bb'] bbox_curr_gt = BoundingBox(currbb[0], currbb[1], currbb[2], currbb[3]) bbox_curr_shift = BoundingBox(0, 0, 0, 0) bbox_curr_shift = bbox_curr_gt.shift(currimg, bb_params['lambda_scale_frac'], bb_params['lambda_shift_frac'], bb_params['min_scale'], bb_params['max_scale'], True, bbox_curr_shift) (rand_search_region, rand_search_location, edge_spacing_x, edge_spacing_y) = cropPadImage(bbox_curr_shift, currimg) bbox_curr_shift.kContextFactor = 4 (rand_search_region_x2, rand_search_location_x2, edge_spacing_x_x2, edge_spacing_y_x2) = cropPadImage(bbox_curr_shift, currimg_x2) bbox_curr_shift.kContextFactor = 2 bbox_curr_gt = BoundingBox(currbb[0], currbb[1], currbb[2], currbb[3]) bbox_gt_recentered = BoundingBox(0, 0, 0, 0) bbox_gt_recentered = bbox_curr_gt.recenter(rand_search_location, edge_spacing_x, edge_spacing_y, bbox_gt_recentered) output_sample['image'] = rand_search_region output_sample['image_x2'] = rand_search_region_x2 output_sample['bb'] = bbox_gt_recentered.get_bb_list() # additional options for visualization opts['edge_spacing_x'] = edge_spacing_x opts['edge_spacing_y'] = edge_spacing_y opts['search_location'] = rand_search_location opts['search_region'] = rand_search_region return output_sample, opts