Advertisement
ClaasCode

Untitled

May 29th, 2023
751
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 2.74 KB | None | 0 0
  1. import os
  2. import numpy as np
  3. from PIL import Image, ImageDraw
  4. from tensorflow import keras
  5.  
  6.  
  7. characters = "abcdefghijklmnopqrstuvwxyz"
  8. num_classes = len(characters)
  9.  
  10. ascii_art_folder = "dataset"
  11. image_folder = "images"
  12.  
  13. for character in characters:
  14.     ascii_file = os.path.join(ascii_art_folder, character + ".txt")
  15.     image_file = os.path.join(image_folder, character + ".png")
  16.  
  17.     with open(ascii_file, "r") as file:
  18.         ascii_art = file.read()
  19.  
  20.     image = Image.new("RGB", (100, 100), color="white")
  21.     image_draw = ImageDraw.Draw(image)
  22.     image_draw.text((10, 10), ascii_art, fill="black")
  23.     image.save(image_file)
  24.  
  25. samples = []
  26. labels = []
  27. for character in characters:
  28.     image_file = os.path.join(image_folder, character + ".png")
  29.  
  30.     image = Image.open(image_file).convert("L")
  31.  
  32.     image = image.resize((64, 64))
  33.  
  34.     image_array = np.array(image)
  35.  
  36.     image_array = image_array / 255.0
  37.  
  38.     samples.append(image_array)
  39.     labels.append(characters.index(character))
  40.  
  41. samples = np.array(samples)
  42. labels = np.array(labels)
  43.  
  44. # Split the dataset into training and testing
  45. split_ratio = 0.8
  46. split_index = int(len(samples) * split_ratio)
  47.  
  48. train_samples = samples[:split_index]
  49. train_labels = labels[:split_index]
  50. test_samples = samples[split_index:]
  51. test_labels = labels[split_index:]
  52.  
  53. # Define the model architecture
  54. model = keras.models.Sequential([
  55.     keras.layers.Conv2D(32, (3, 3), activation="relu", input_shape=(64, 64, 1)),
  56.     keras.layers.MaxPooling2D((2, 2)),
  57.     keras.layers.Flatten(),
  58.     keras.layers.Dense(64, activation="relu"),
  59.     keras.layers.Dense(num_classes, activation="softmax")
  60. ])
  61.  
  62. # Compile the model
  63. model.compile(optimizer="adam", loss="sparse_categorical_crossentropy", metrics=["accuracy"])
  64.  
  65. # Train the model
  66. model.fit(train_samples, train_labels, epochs=100, batch_size=32, validation_split=0.2)
  67.  
  68. # Evaluate the model
  69. test_loss, test_acc = model.evaluate(test_samples, test_labels)
  70. print(f"Test accuracy: {test_acc}")
  71.  
  72. # Convert input ASCII art to image and predict the letter
  73. input_ascii_art = """
  74.  _   _
  75. | | | |
  76. | |_| |
  77.  \__, |
  78.  |___/
  79. """
  80. input_image = Image.new("RGB", (100, 100), color="white")
  81. input_image_draw = ImageDraw.Draw(input_image)
  82. input_image_draw.text((10, 10), input_ascii_art, fill="black")
  83. input_image = input_image.convert("L")
  84. input_image = input_image.resize((64, 64))
  85. input_image_array = np.array(input_image) / 255.0
  86. input_image_array = np.expand_dims(input_image_array, axis=0)
  87. input_image_array = np.expand_dims(input_image_array, axis=-1)
  88.  
  89. prediction = model.predict(input_image_array)
  90. predicted_index = np.argmax(prediction)
  91. predicted_character = characters[predicted_index]
  92.  
  93. print(f"Predicted character: {predicted_character}")
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement