@@ -0,0 +1,1793 @@
+import numpy as np%0Aimport itertools%0Afrom scipy.misc import imread%0Afrom matplotlib import pyplot as plt%0A%0Adef crop_img(img_array, crop_size=40):%0A%09img_array = img_array%5B:-crop_size,:,:%5D%0A%0A%09return img_array%0A%0Adef make_training_data(img_array):%0A%09size = 40%0A%09stride = 20%0A%0A%09height = img_array.shape%5B0%5D%0A%09width = img_array.shape%5B1%5D%0A%0A%09height_steps = (height - size) // stride%0A%09width_steps = (width - size) // stride%0A%0A%09total_imgs = height_steps * width_steps%0A%0A%09x_train = np.zeros(%5Btotal_imgs, size, size, 3%5D, dtype=np.uint8)%0A%0A%09i = 0%0A%0A%09for y, x in itertools.product(range(height_steps), range(width_steps)):%0A%09%09img = img_array%5By*stride:y*stride+size, x*stride:x*stride+size, :%5D%0A%09%09x_train%5Bi,:,:,:%5D = img%0A%09%09i += 1%0A%0A%09return x_train%0A%0Adef classify_training(x_train):%0A%09total_imgs = x_train.shape%5B0%5D%0A%0A%09y_train = np.zeros(%5Btotal_imgs, 1%5D, dtype=np.uint8)%0A%09%0A%09plt.ion()%0A%09plt.figure()%0A%09%0A%09for i in range(total_imgs):%0A%09%09img = x_train%5Bi,:,:,:%5D%0A%09%09plt.clf()%0A%09%09plt.imshow(img)%0A%09%09plt.draw()%0A%09%09plt.pause(0.01)%0A%09%09classify = input(%22If the image is a car press 1: %22)%0A%09%09if classify == '1':%0A%09%09%09y_train%5Bi,:%5D = 1.%0A%0A%09print('This set had %7B%7D images of cars.'.format(np.sum(y_train)))%0A%0A%09return y_train%0A%0Adef save_arrays(img_number, x_train, y_train):%0A%09x_fname = 'classified_arrays%5C%7B%7D_X.npy'.format(img_number)%0A%09y_fname = 'classified_arrays%5C%7B%7D_Y.npy'.format(img_number)%0A%0A%09np.save(x_fname, x_train)%0A%09np.save(y_fname, y_train)%0A%0A%0Adef main():%0A%09img_number = input(%22Enter filename to process (e.g. 12, 79, etc.): %22)%0A%09img_fname = 'google_image_dump%5C%7B%7D.png'.format(img_number)%0A%0A%09img_array = imread(img_fname)%0A%0A%09img_array = crop_img(img_array, 40)%0A%0A%09x_train = make_training_data(img_array)%0A%09%0A%09y_train = classify_training(x_train)%0A%0A%09save_arrays(img_number, x_train, y_train)%0A%0Aif __name__ == %22__main__%22:%0A    # execute only if run as a script%0A    main()

Classify images and build training data arrays