def __init__(self, path, max_load=None): self.path = path with open(os.path.join(self.path, 'meta.json'), 'r') as file: self.meta = json.loads(file.read())['classes'] self.data = [] files = [e for e in os.listdir(self.path) if e.split('.')[-1] == 'npy'] files = sorted(files) if max_load is not None: idx = np.random.choice(range(len(files)), max_load, replace=False) f = [] m = [] for i in idx: f.append(files[i]) m.append(self.meta[i]) self.meta = m files = f pb = ProgressBar("Loading", 15, len(files), update_every=2000, ea=15) for i, file in enumerate(files): f = os.path.join(self.path, file) self.data.append(np.load(f)) pb.update(i + 1) pb.finish()
def pump_out(time_: int): switch.on() progress_bar = ProgressBar() progress_bar.initialize() for i in range(time_): time.sleep(1) percentage = (i + 1) / time_ * progress_bar.width progress_bar.update(percentage) progress_bar.finish() switch.off()
def load(self): subdirs = [] dirclass = [] names = [] for entry in os.listdir(self.path): inner = os.path.join(self.path, entry) if os.path.isdir(inner): if len(entry) == 2: subdirs.append(inner) dirclass.append(chr(int(entry, 16))) names.append('train_' + entry) self.data = [] pb = ProgressBar("Loading", 15, len(subdirs), update_every=1, ea=15) # We now have a list of directories and their corresponding # characters. We need to load the images with opencv, convert # them to BGR, crop them and resize them. for idx, (c, d, n) in enumerate(zip(dirclass, subdirs, names)): subdir = os.path.join(d, n) for entry in os.listdir(subdir): ext = entry.split('.')[-1].lower() if ext == 'png': self.data.append( HandwrittenCharacter(os.path.join(subdir, entry), c, self.size)) pb.update(idx + 1) pb.finish() pb = ProgressBar("Saving", 15, len(self.data), update_every=2000, ea=15) self.meta = [] for i, d in enumerate(self.data): outpath = os.path.join(self.out, "%06d.np" % i) self.meta.append(d.char) np.save(outpath, d.image) pb.update(i + 1) pb.finish() with open(os.path.join(self.out, 'meta.json'), 'w') as file: file.write(json.dumps({'classes': self.meta}))