-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpokemon_data_creator.py
153 lines (111 loc) · 4.25 KB
/
pokemon_data_creator.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
import os
import cv2
import numpy as np
from tqdm import tqdm
import pathlib
import shutil
from config import *
def make_pokemon_dictionary(data_dir):
'''
Parameters
---------
1 parameter
object: string
Directory of of directories with images. Has to be a string
Returns
---------
2 objects
object: list
List of directory names
object: dictionary
dictionary with directories with array labels
'''
dirs_to_ignore = ['backgrounds','README.md', ".DS_Store", "._.DS_Store"]
pokemon = []
for dirs in os.listdir(data_dir):
if dirs in dirs_to_ignore:
continue
else:
pokemon.append(dirs)
pokemon.sort()
directories = [] # List of directories for each pokemon folder
LABELS = {} # Dictionary where the directories of each pokemon and their label will be stored
for i in range(len(pokemon)):
pokemon_directory = os.path.join(data_dir, pokemon[i])
directories.append(pokemon_directory)
LABELS[directories[i]] = i
return pokemon, LABELS
def make_training_data(pokemon, LABELS, IMG_SIZE):
'''
Parameters
---------
3 parameter
object: list
List of directory names
object: dictionary
dictionary with directories with array labels
object: Integer
Size, in pixels, that all images will be resized as
Returns
---------
1 object
object: list of arrays
List of numpy arrays with all the images and numpy arrays with the corresponding labels
'''
training_data = []
counts = [0]*len(pokemon)
click = 0
for label in LABELS:
print(f"Fetching {pokemon[click]}'s images")
for f in tqdm(os.listdir(label)):
try:
path = os.path.join(label, f)
img = cv2.imread(path)
img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB) # It makes sure that cv2 reads images as RGB instead of the default BGR
img = cv2.resize(img, (IMG_SIZE, IMG_SIZE))
if img.shape == (IMG_SIZE,IMG_SIZE): #This was written to check for any grayscale images (single channel images)
print(path)
else:
pass
training_data.append([np.array(img), np.eye(len(pokemon))[LABELS[label]]]) # Training data has both the numpy array of the image and the associated label of the image appended
counts[click] += 1
except Exception as e:
pass
click += 1
print("\nTotal images per Pokémon")
for i in range(len(pokemon)):
print(f"{pokemon[i]}: {counts[i]}")
print(f"\nTotal images: {len(training_data)}\n")
np.random.shuffle(training_data)
return training_data
def save_training_data(training_data, data_dir, numpy_files_directory, IMG_SIZE):
'''
Parameters
---------
4 parameter
object: list of arrays
List of numpy arrays with shape (-1,2)
object: string
Directory of of directories with images. Has to be a string
object: string
Name of directory were numpy files will be saved
object: integer
Size, in pixels, that all images will be resized as
Returns
---------
Numpy file with training_data
'''
current_folder_name = os.path.basename(os.path.normpath(data_dir))
data_save_name = f"Pokemon_Data_Colour_{current_folder_name}_{IMG_SIZE}.npy"
save_path = os.path.join(numpy_files_directory, data_save_name) # Directory path where numpy file will be saved in
if not os.path.exists(numpy_files_directory): # Saving the numpy file in preferred directory
os.makedirs(numpy_files_directory)
np.save(save_path, training_data)
if os.path.exists(numpy_files_directory):
np.save(save_path, training_data)
#########################################################################
IMG_SIZE = 80
data_dir = image_dir+'GenX/'
pokemon, LABELS = make_pokemon_dictionary(data_dir)
training_data =make_training_data(pokemon, LABELS, IMG_SIZE)
save_training_data(training_data, data_dir, "Data/numpy_files", IMG_SIZE)