Advertisement
cd62131

image files partition

Dec 23rd, 2018
1,264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 0.74 KB | None | 0 0
  1. #!/usr/bin/python3
  2. from pathlib import Path
  3. from shutil import copy2
  4. from random import shuffle
  5.  
  6.  
  7. def main():
  8.     imgs = list(Path('./img_data').glob('**/*.jpg'))
  9.     if not imgs:
  10.         return
  11.     shuffle(imgs)
  12.     trainImgDir = Path('./train_img_data')
  13.     testImgDir = Path('./test_img_data')
  14.     for i in range(1, 100 + 1):
  15.         d = f'{i:03d}_data'
  16.         trainImgDir.joinpath(d).mkdir(parents=True, exist_ok=True)
  17.         testImgDir.joinpath(d).mkdir(parents=True, exist_ok=True)
  18.     imgs_len_80 = len(imgs) * 4 / 5
  19.     for i, p in enumerate(imgs):
  20.         if i < imgs_len_80:
  21.             copy2(p, trainImgDir.joinpath(*p.parts[-2:]))
  22.             continue
  23.         copy2(p, testImgDir.joinpath(*p.parts[-2:]))
  24.  
  25.  
  26. main()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement