from gzip import decompress
import os
from idx2numpy import convert_from_file
from matplotlib import pyplot as plt
mapping = {}
with open("data/emnist-mapping.txt", "r") as mapping_file:
for line in mapping_file:
mapping[line.split(" ")[0]] = chr(int(line.split(" ")[1]))
def main(decompress_files=True):
if decompress_files:
for filename in os.listdir(os.path.join(os.path.dirname(__file__), "data")):
with open(f"data/{filename}", "rb") as compressed:
with open(f"data/{filename.replace(".gz", "")}", "wb+") as new:
new.write(decompress(compressed.read()))
print("Files decompressed")
index = 26
with open("data/emnist-test-images-idx3-ubyte", "rb") as images:
image_data = convert_from_file(images)
image = image_data[index]
with open("data/emnist-test-labels-idx1-ubyte", "rb") as labels:
label_data = convert_from_file(labels)
label = label_data[index]
plt.imshow(image.T, cmap="gray")
plt.title(f"Label: {mapping[str(label)]}")
plt.show()
if __name__ == "__main__":
main(decompress_files=False)