-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathaugment.py
74 lines (58 loc) · 2.34 KB
/
augment.py
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
import Augmentor
import os
import shutil
import ujson
from PIL import Image
def augment_data(path: str):
p = Augmentor.Pipeline(path, path)
p.random_distortion(1, 4, 4, 4)
p.sample(200)
def move_img_to_folders(path: str):
for file in os.listdir(path):
if file.endswith('.png'):
new_folder = os.path.join(path, file).split('.')[0]
os.makedirs(new_folder)
shutil.move(os.path.join(path, file), new_folder)
def augment_emojis(path: str):
for dir in os.listdir(path):
path_to_dir = os.path.join(path, dir)
augment_data(path_to_dir)
def transform_to_jpeg(path: str):
for dir in os.listdir(path):
path_to_dir = os.path.join(path, dir)
for image in os.listdir(path_to_dir):
im = Image.open(os.path.join(path_to_dir, image))
rgb_im = im.convert('RGB')
rgb_im.save(os.path.join(path_to_dir, image).split('.')[0] + ".jpg")
print(f'saved: {os.path.join(path_to_dir, image).split(".")[0] + ".jpg"}')
def rename_folders(path: str, json_path: str):
with open(json_path) as f:
data = ujson.load(f)
for emoji in data:
try:
os.rename(os.path.join(path, emoji['unicode']), os.path.join(path, emoji['description']))
print(f'renamed {emoji["unicode"]} to {emoji["description"]}')
except Exception as ex:
print(f'dir has been already renamed: {emoji["unicode"]}')
def get_min(path):
min = 200
for dir in os.listdir(path):
path_to_dir = os.path.join(path, dir)
print(len(os.listdir(path_to_dir)))
if len(os.listdir(path_to_dir)) < min:
min = len(os.listdir(path_to_dir))
print(min)
def enumerate_classes(path: str):
sorted_dir = sorted(os.listdir(path))
with open('./classes.json', 'w+') as f:
ujson.dump({sorted_dir.index(x): x for x in sorted_dir}, f)
if __name__ == '__main__':
# need to use one time for futher data augmentation
move_img_to_folders('/home/atticus/emoji-dataset/emoji_imgs_V5')
#
# # than apply augmentation!
augment_emojis('/home/atticus/emojis/train')
#
# # rename folder to emoji description
rename_folders('/home/atticus/emoji-dataset/emoji_imgs_V5', '/home/atticus/emojis/V5_data.json')
enumerate_classes('/home/atticus/emojis/train')