image_neural_net

Animal recognition using a neural network.
Log | Files | Refs | README

create_training_data.py (1803B)


      1 from PIL import Image
      2 import cv2 as cv
      3 import numpy as np
      4 import random as rand
      5 import glob
      6 
      7 IMG_SIZE = 8
      8 
      9 training = []
     10 trainingLabels = []
     11 
     12 
     13 pics = glob.glob("../train/dog/*.png")
     14 for image in pics:
     15     img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
     16     # ret, img = cv.threshold(img, 127, 255, 1)
     17     labels = [1, 0, 0, 0]
     18     training.append([np.array(img)])
     19     trainingLabels.append([np.array(labels)])
     20 
     21 pics = glob.glob("../train/cat/*.png")
     22 for image in pics:
     23     img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
     24     # ret, img = cv.threshold(img, 127, 255, 1)
     25     labels = [0, 1, 0, 0]
     26     training.append([np.array(img)])
     27     trainingLabels.append([np.array(labels)])
     28 
     29 pics = glob.glob("../train/bird/*.png")
     30 for image in pics:
     31     img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
     32     # ret, img = cv.threshold(img, 127, 255, 1)
     33     labels = [0, 0, 1, 0]
     34     training.append([np.array(img)])
     35     trainingLabels.append([np.array(labels)])
     36 
     37 pics = glob.glob("../train/dolphin/*.png")
     38 for image in pics:
     39     img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE))
     40     # ret, img = cv.threshold(img, 127, 255, 1)
     41     labels = [0, 0, 0, 1]
     42     training.append([np.array(img)])
     43     trainingLabels.append([np.array(labels)])
     44 
     45 training = np.array(training) #as mnist
     46 trainingLabels = np.array(trainingLabels) #as mnist
     47 
     48 training = np.reshape(training, [training.shape[0], training.shape[1] * training.shape[2] * training.shape[3]])
     49 trainingLabels = np.reshape(trainingLabels, [trainingLabels.shape[0], trainingLabels.shape[1] * trainingLabels.shape[2]])
     50 np.save('../train/training', training)
     51 np.save('../train/trainingLabels', trainingLabels)
     52 print(training.shape)
     53 print(trainingLabels.shape)