-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathextractor.py
More file actions
36 lines (25 loc) · 918 Bytes
/
extractor.py
File metadata and controls
36 lines (25 loc) · 918 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
import os
import random
import shutil
import pandas as pd
random.seed(42)
#source = "name_of_folder"
source = "train"
ratio = 0.25
PWD = os.getcwd()
root = os.path.join(PWD, source)
os.rename(root, os.path.join(PWD, source + "_ori"))
newf=os.path.join(PWD, source + "_extracted")
shutil.copytree(os.path.join(PWD, source + "_ori"), newf)
files = os.listdir(newf)
for category in files:
images = os.listdir(os.path.join(newf, category))
before_total_num = len(images)
del_num = int(before_total_num*(1.0-ratio))
random_delete_list = random.sample(range(before_total_num), k=del_num)
for i in range(len(images)):
if i in random_delete_list:
os.remove(os.path.join(newf, category + "/" + images[i]))
after_total_num = before_total_num - del_num
images = os.listdir(os.path.join(newf, category))
print('category:{} before:{} after:{}'.format(category, before_total_num, after_total_num))