create_test_data.py (2012B)
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 testingDog = [] 10 testingCat = [] 11 testingBird = [] 12 testingDolphin = [] 13 14 pics = sorted(glob.glob("../test/dog/*.png")) 15 for image in pics: 16 img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE)) 17 # ret, img = cv.threshold(img, 127, 255, 1) 18 testingDog.append([np.array(img)]) 19 20 pics = sorted(glob.glob("../test/cat/*.png")) 21 for image in pics: 22 img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE)) 23 # ret, img = cv.threshold(img, 127, 255, 1) 24 testingCat.append([np.array(img)]) 25 26 pics = sorted(glob.glob("../test/bird/*.png")) 27 for image in pics: 28 img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE)) 29 # ret, img = cv.threshold(img, 127, 255, 1) 30 testingBird.append([np.array(img)]) 31 32 pics = sorted(glob.glob("../test/dolphin/*.png")) 33 for image in pics: 34 img = cv.resize(cv.imread(image, cv.IMREAD_GRAYSCALE), (IMG_SIZE, IMG_SIZE)) 35 # ret, img = cv.threshold(img, 127, 255, 1) 36 testingDolphin.append([np.array(img)]) 37 38 testingDog = np.array(testingDog) #as mnist 39 testingCat = np.array(testingCat) #as mnist 40 testingBird = np.array(testingBird) #as mnist 41 testingDolphin = np.array(testingDolphin) #as mnist 42 43 testingDog = np.reshape(testingDog, [testingDog.shape[0], testingDog.shape[1] * testingDog.shape[2] * testingDog.shape[3]]) 44 testingCat= np.reshape(testingCat, [testingCat.shape[0], testingCat.shape[1] * testingCat.shape[2] * testingCat.shape[3]]) 45 testingBird= np.reshape(testingBird, [testingBird.shape[0], testingBird.shape[1] * testingBird.shape[2] * testingBird.shape[3]]) 46 testingDolphin= np.reshape(testingDolphin, [testingDolphin.shape[0], testingDolphin.shape[1] * testingDolphin.shape[2] * testingDolphin.shape[3]]) 47 48 np.save('../test/testingDog', testingDog) 49 np.save('../test/testingCat', testingCat) 50 np.save('../test/testingBird', testingBird) 51 np.save('../test/testingDolphin', testingDolphin)