Make Predictions - AI

On hand drawn digit

# Make sure image is hand drawn 28 x 28 pixel image
# Also make sure background is black and drawing is white
# These requirements may not be necessary with CNN model, but for the perceptron model this is necessary


from PIL import Image
import numpy as np

for num in range(1,10):
    im = Image.open(f"/home/steven/ai/MyTestData/{num}.png")

    # convert to grayscale because thats what we trained on
    gray = im.convert('L')

    # convert to 2x2
    two_dim_array = []
    flattened_pixel_map = list(gray.getdata())
    i=0
    for j in range(28):
        i=0
        two_dim_array.append([])
        while i < 28:
            two_dim_array[-1].append(flattened_pixel_map[j * 28 + i])
            i += 1

    arr = np.array(two_dim_array)
    new = arr[np.newaxis, :, :, np.newaxis]

    prediction = model.predict(new)

    print(prediction)