from PIL import Image
import os, glob
import numpy as np
from sklearn import model_selection

classes = ["car", "bycycle", "motorcycle", "pedestrian"]
num_class = len(classes)
image_size = 50

# 画像の読み込み

X = []
Y = []

for index, classlabel in enumerate(classes):
    photos_dir = "./" + classlabel
    files = glob.glob(photos_dir + "/*.jpg")
    for i, file in enumerate(files):
        if i >=237: break
        image = Image.open(file)
        image = image.convert("RGB")
        image = image.resize((image_size, image_size))
        data = np.asarray(image) / 255
        X.append(data)
        Y.append(index)

X = np.array(X)
Y = np.array(Y)

X_train, X_test, y_train, y_test = model_selection.train_test_split(X, Y)
xy = (X_train, X_test, y_train, y_test)
np.save("./vehicle.npy", xy)
[((27, 4, 27, 15), 'numpy.array', 'np.array', ({(27, 13, 27, 14): 'X'}, {}), '(X)', True, 'import numpy as np\n'), ((28, 4, 28, 15), 'numpy.array', 'np.array', ({(28, 13, 28, 14): 'Y'}, {}), '(Y)', True, 'import numpy as np\n'), ((30, 35, 30, 73), 'sklearn.model_selection.train_test_split', 'model_selection.train_test_split', ({(30, 68, 30, 69): 'X', (30, 71, 30, 72): 'Y'}, {}), '(X, Y)', False, 'from sklearn import model_selection\n'), ((32, 0, 32, 28), 'numpy.save', 'np.save', ({(32, 8, 32, 23): '"""./vehicle.npy"""', (32, 25, 32, 27): 'xy'}, {}), "('./vehicle.npy', xy)", True, 'import numpy as np\n'), ((17, 12, 17, 44), 'glob.glob', 'glob.glob', ({(17, 22, 17, 43): "photos_dir + '/*.jpg'"}, {}), "(photos_dir + '/*.jpg')", False, 'import os, glob\n'), ((20, 16, 20, 32), 'PIL.Image.open', 'Image.open', ({(20, 27, 20, 31): 'file'}, {}), '(file)', False, 'from PIL import Image\n'), ((23, 15, 23, 32), 'numpy.asarray', 'np.asarray', ({(23, 26, 23, 31): 'image'}, {}), '(image)', True, 'import numpy as np\n')]