stc_vid2frames_1.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. def main():
  2. parser = argparse.ArgumentParser(add_help=True)
  3. parser.add_argument('--dataroot',
  4. default='.',
  5. help='Dataset root directory')
  6. parser.add_argument('--src_vid_path', default='archive/training/videos/',
  7. help='Name of folder where `avi` files exist')
  8. parser.add_argument('--tar_vid_frame_path', default='converted/train',
  9. help='Name of folder to save extracted frames.')
  10. parser.add_argument('--src_npy_path', default='archive/test_pixel_mask/',
  11. help='Name of folder where `npy` frame mask exist')
  12. parser.add_argument('--tar_anno_path', default='converted/pixel_mask',
  13. help='Name of folder to save extracted frame annotation')
  14. parser.add_argument('--extension', default='jpg',
  15. help="File extension format for the output image")
  16. args = parser.parse_args()
  17. src_dir = os.path.join(args.dataroot, args.src_vid_path)
  18. tar_dir = os.path.join(args.dataroot, args.tar_vid_frame_path)
  19. try:
  20. os.makedirs(tar_dir)
  21. except FileExistsError:
  22. print(F'{tar_dir} already exists, remove whole tree and recompose ...')
  23. shutil.rmtree(tar_dir)
  24. os.makedirs(tar_dir)
  25. vid_list = os.listdir(src_dir)
  26. for i, vidname in enumerate(tqdm(vid_list)):
  27. vid = torchvision.io.read_video(os.path.join(src_dir, vidname), pts_unit='sec')[0]
  28. target_folder = os.path.join(tar_dir, vidname[:-4])
  29. try:
  30. os.makedirs(target_folder)
  31. except FileExistsError:
  32. print(F'{target_folder} already exists, remove the directory recompose ...')
  33. shutil.rmtree(target_folder)
  34. os.makedirs(target_folder)
  35. for i, frame in enumerate(vid):
  36. frame = (frame / 255.).permute(2, 0, 1) #HWC2CHW
  37. torchvision.utils.save_image(frame,
  38. F'{target_folder}/{i:03}.{args.extension}')
  39. src_dir = os.path.join(args.dataroot, args.src_npy_path)
  40. tar_dir = os.path.join(args.dataroot, args.tar_anno_path)
  41. try:
  42. os.makedirs(tar_dir)
  43. except FileExistsError:
  44. print(F"{tar_dir} already exists, remove whole tree and recompose ...")
  45. shutil.rmtree(tar_dir)
  46. os.makedirs(tar_dir)
  47. frame_anno = os.listdir(src_dir)
  48. for _f in tqdm(frame_anno):
  49. fn = _f[:-4]
  50. target_folder = os.path.join(tar_dir, fn)
  51. os.makedirs(target_folder)
  52. px_anno = np.load(F"{src_dir}/{fn}.npy").astype(np.float)
  53. for i, px_frame in enumerate(px_anno):
  54. torchvision.utils.save_image(torch.from_numpy(px_frame).unsqueeze(0), # CHW, 1 channel
  55. F"{target_folder}/{i:03}.{args.extension}")
  56. if __name__ == '__main__':
  57. main()