-
Notifications
You must be signed in to change notification settings - Fork 0
/
preprocess.py
94 lines (84 loc) · 2.22 KB
/
preprocess.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# -*- coding: utf-8 -*-
import os
import random
import pickle
import urllib.request
import numpy as np
from scipy.misc import imread, imsave
idols = [
'Hanayo',
'Honoka',
'Nico',
'Kotori',
'Umi',
'Eli',
'Rin',
'Maki',
'Nozomi',
'Kanan',
'Ruby',
'Hanamaru',
'Yoshiko',
'Riko',
'You',
'Chika',
'Mari',
'Dia',
]
def download():
print("downloading images...")
if not os.path.isdir('images'):
os.mkdir('images')
step = 0
with open('data/urls.txt', mode='r') as f:
for line in f:
step += 1
print(f"working on {step}th line", end='\r')
url = line.strip()
if not url:
continue
filename = url.split('/')[-5]
filepath = os.path.join('images', filename)
if not os.path.isfile(filepath):
urllib.request.urlretrieve(url, filepath)
print("\ndone")
def preprocess(cut=0.95):
print("creating dataset...")
if not os.path.isdir('faces'):
os.mkdir('faces')
root = 'images'
filenames = os.listdir(root)
data = []
step = 0
for filename in filenames:
step += 1
print(f"working on {step}th line", end='\r')
if filename.split('.')[-1] != 'png':
continue
tokens = filename.split('_')
freeze = False
for token in tokens:
for label, idol in enumerate(idols):
if token == idol:
freeze = True
break
if freeze:
break
if not freeze:
continue
image = imread(os.path.join(root, filename))[13:77, 35:99, :3]
imsave('faces/{:04d}_{}.jpg'.format(len(data), idol), image)
image = np.rollaxis(image / 255, 2)
hot = np.zeros(len(idols))
hot[label] = 1
data.append((image, hot))
pickle.dump(data, open('data/all.dat', 'wb'))
random.shuffle(data)
n = int(len(data) * cut)
pickle.dump(data[:n], open('data/train.dat', 'wb'))
pickle.dump(data[n:], open('data/test.dat', 'wb'))
print("\ndone")
print("total {} idols".format(len(data)))
if __name__ == '__main__':
download()
preprocess()