Test Data

The purpose of this notebook is to prepare images, downloaded from Internet, to be classified by developed neural network. At the end of processing two pickle files will generated.


In [1]:
# Import libraries
import tensorflow as tf
from PIL import Image
import numpy as np
import pickle

In [2]:
# Define and load image data
filelist = ['./sign13.png', './sign23.png', './sign28.png', './sign35.png', './sign38.png'] # Hardcoded to keep a sequence
X_images = np.array([np.array(Image.open(fname).convert('RGB').resize((32,32),Image.BICUBIC)) for fname in filelist])

In [3]:
# Normalize images
def normalize_img(image_data):
    """
    Normalize the image data with Min-Max scaling to a range of [0.1, 0.9],
        :param image_data: The image data to be normalized,
        :return: Normalized image data.
    """
    a = 0.1
    b = 0.9
    scale_min = 0
    scale_max = 255
    return a + (((image_data - scale_min)*(b - a))/(scale_max - scale_min))

X_images_norm = normalize_img(X_images)

In [4]:
# Transform images to grayscale
tf.reset_default_graph()
X_images2gray  = tf.image.rgb_to_grayscale(X_images_norm)

with tf.Session() as sess:
    X_images_gray = sess.run(X_images2gray)

In [5]:
# Define sign classes
y_images = ([13, 23, 28, 35, 38])

In [6]:
# Make sure that the number of features equals the number of labels
assert(len(X_images_gray) == len(y_images))

In [7]:
# Save results
with open('./test_img_features.pickle', 'wb') as output:
    pickle.dump(X_images_gray, output)

with open('./test_img_labels.pickle', 'wb') as output:
    pickle.dump(y_images, output)