Advertisement
Abhisek92

Skip files with discrepency

Jan 28th, 2020
347
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.46 KB | None | 0 0
  1. labeled_data = None
  2. fgroups = aoi_df.groupby(['FileName'])
  3. for f in u_files:
  4.     g = fgroups.get_group(f)
  5.     plist, clist = g['Polygon'].tolist(), g['ClassName'].tolist()
  6.     fpath = img_dir / f
  7.     class_im = None
  8.     with rio.open(fpath) as imptr:
  9.         class_im = np.zeros(shape=imptr.shape, dtype=int)
  10.         for ply, lbl in zip(plist, clist):
  11.             class_im += create_image_mask(a_polygon=ply, class_name=lbl, img_shape=imptr.shape)
  12.         if class_im.max() <= max(label_map.values()):
  13.             data_stack = np.concatenate((img_arr, class_im.reshape(-1, *class_im.shape)), axis=0)
  14.             flat_mask = class_im == 0
  15.             im_mask = np.repeat(flat_mask.reshape(-1, *flat_mask.shape), data_stack.shape[0], axis=0)
  16.             masked_data = np.ma.masked_array(data_stack, im_mask, fill_value=0, dtype=int)
  17.             mshp = masked_data.shape
  18.             flattened_data = np.moveaxis(masked_data, 0, -1).reshape(-1, *mshp[:1])
  19.             flattened_data = flattened_data.filled()
  20.             # Filter if class==0. Can be commented if unnecessary
  21.             flattened_data = flattened_data[flattened_data[:, -1] != 0]
  22.             if labeled_data is None:
  23.                 labeled_data = flattened_data
  24.             else:
  25.                 labeled_data = np.concatenate((labeled_data, flattened_data), axis=0)
  26.         else:
  27.             print('Error in file: ' + fpath.name + fpath.suffix)
  28.  
  29. #Save Prepared data to file
  30. labeled_data.dump('foo.npy')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement