text_plain_output_1.png
from scipy.misc import imread

import cv2 as cv

import glob

import numpy as np

import os

import random

species = ['ALB', 'BET', 'DOL', 'LAG', 'NoF', 'OTHER', 'SHARK', 'YFT']
select = 1000
ROWS = 90
COLS = 160
CHANNELS = 3
PATH = './input/'
def get_image(file):
    pos1 = file.rfind('/img_')
    return file[pos1 + 1:]
def get_id(file):
    pos1 = file.rfind('_')
    pos2 = file.rfind('.')
    return file[pos1 + 1:pos2]
def load_train_data(select):
    train_files = sorted(glob.glob(PATH + '/train/*/*.jpg'), key=lambda x: random.random())[:select]
    train = np.array([imread(img) for img in train_files])
    X_train = np.array([cv.resize(img, (ROWS, COLS)) for img in train])
    y = np.array([species.index(os.path.dirname(img).replace(PATH + '/train/', '')) for img in train_files])
    ids = np.array([get_id(img) for img in train_files])
    X_train = np.array(X_train, dtype=np.float32) / 255
    return (X_train, y, ids)
def load_test_data():
    test_files = sorted(glob.glob(PATH + '/test_stg1/*.jpg'))
    test = np.array([imread(img) for img in test_files])
    X_test = np.array([cv.resize(img, (ROWS, COLS)) for img in test])
    X_test = np.array(X_test, dtype=np.float32) / 255
    ids = np.array([get_image(img) for img in test_files])
    return (X_test, ids)

X, y, ids = load_train_data(select)
print(X.shape)