Search code examples
pythonpandasimageknnmnist

Extract properties from image for MNIST dataset predictor


I have created a KNN predictor for the MNIST dataset. The dataset contains images of handwritten images. The images are grayscale and measure 28 X 28 pixels. The information of these images is stored in 784 features, each feature corresponds to the intensity of a single pixel in the image. I want to implement this model to new images which are not in the test or training set. I want to write a piece of code which can take an image convert it into 28 X 28 pixels and then store the intensity of each of these pixels as a value in a unique feature. This will enable me to take new images and make predictions on them. What module should I use for this and what procedure should I follow?


Solution

  • You can use OpenCV and Numpy, Suppose you take a picture of a digit and save it as 'pic.jpg'

    import cv2
    import numpy as np
    
    img = cv2.imread('pic.jpg', 0) # 0 for greyscale 
    resized = cv2.resize(img, (28,28)) 
    features = resized.reshape(-1) #converts into single dimension
    

    The features array will have the 784 features