def get_cropped_test_img(fname, bbox_pred, pad=None, as_grey=False, return_bbox=False): img = imread(fname, as_grey=as_grey) h = img.shape[0] w = img.shape[1] bbox_pred = bbox_pred * [w, h, w, h] bbox_pred = np.round(bbox_pred).astype(int) l = min(max(bbox_pred[0], 0), w) t = min(max(bbox_pred[1], 0), h) r = min(max(l + bbox_pred[2], 0), w) b = min(max(t + bbox_pred[3], 0), h) if pad is not None: l, t, r, b = add_padding_to_bbox(l, t, (r - l), (b - t), pad / 100.0, img.shape[1], img.shape[0], format='ltrb') cropped_img = img[t:b, l:r] if return_bbox: return cropped_img, bbox_pred else: return cropped_img
def get_cropped_test_img(fname, bbox_pred, pad=None, as_grey=False, return_bbox=False): img = imread(fname, as_grey=as_grey) h = img.shape[0] w = img.shape[1] bbox_pred = bbox_pred * [w, h, w, h] bbox_pred = np.round(bbox_pred).astype(int) l = min(max(bbox_pred[0], 0), w) t = min(max(bbox_pred[1], 0), h) r = min(max(l + bbox_pred[2], 0), w) b = min(max(t + bbox_pred[3], 0), h) if pad is not None: l, t, r, b = add_padding_to_bbox( l, t, (r - l), (b - t), pad / 100.0, img.shape[1], img.shape[0], format='ltrb' ) cropped_img = img[t:b, l:r] if return_bbox: return cropped_img, bbox_pred else: return cropped_img
X_fp = np.memmap(X_fname, dtype=np.float32, mode='w+', shape=X_shape) y_fp = np.memmap(y_fname, dtype=np.int32, mode='w+', shape=y_shape) for i, row in tqdm(df.iterrows(), total=len(df)): fname = os.path.join('data/imgs/', row['Image']) whale_id = row['whaleID'] x = int(row['x']) y = int(row['y']) w = int(row['width']) h = int(row['height']) try: img = imread(fname, as_grey=args.as_grey) if args.pad > 0: x, y, w, h = add_padding_to_bbox(x, y, w, h, args.pad / 100.0, img.shape[1], img.shape[0]) img = img[y:y + h, x:x + w] img = resize(img, (args.size, args.size)) if args.as_grey: img = img.reshape(-1, args.size, args.size) else: img = img.transpose(2, 0, 1) img = img.astype(np.float32) X_fp[i] = img y_fp[i] = whale_id X_fp.flush() y_fp.flush()
X_fp = np.memmap(X_fname, dtype=np.float32, mode='w+', shape=X_shape) y_fp = np.memmap(y_fname, dtype=np.int32, mode='w+', shape=y_shape) for i, row in tqdm(df.iterrows(), total=len(df)): fname = os.path.join('data/imgs/', row['Image']) whale_id = row['whaleID'] x = int(row['x']) y = int(row['y']) w = int(row['width']) h = int(row['height']) try: img = imread(fname, as_grey=args.as_grey) if args.pad > 0: x, y, w, h = add_padding_to_bbox( x, y, w, h, args.pad / 100.0, img.shape[1], img.shape[0] ) img = img[y:y + h, x:x + w] img = resize(img, (args.size, args.size)) if args.as_grey: img = img.reshape(-1, args.size, args.size) else: img = img.transpose(2, 0, 1) img = img.astype(np.float32) X_fp[i] = img y_fp[i] = whale_id X_fp.flush()