text_plain_output_1.png
from glob import glob

import cv2

import matplotlib.pyplot as plt

import numpy as np # linear algebra

import os

import os
from glob import glob
TRAIN_DATA = '../input/train'
type_1_files = glob(os.path.join(TRAIN_DATA, 'Type_1', '*.jpg'))
type_2_files = glob(os.path.join(TRAIN_DATA, 'Type_2', '*.jpg'))
type_3_files = glob(os.path.join(TRAIN_DATA, 'Type_3', '*.jpg'))
type_1_ids = np.array([s[len(os.path.join(TRAIN_DATA, 'Type_1')) + 1:-4] for s in type_1_files])
type_2_ids = np.array([s[len(os.path.join(TRAIN_DATA, 'Type_2')) + 1:-4] for s in type_2_files])
type_3_ids = np.array([s[len(os.path.join(TRAIN_DATA, 'Type_3')) + 1:-4] for s in type_3_files])

def get_filename(image_id, image_type):
    """
    Method to get image file path from its id and type   
    """
    try:
        ['Type_1', 'Type_2', 'Type_3'].index(image_type)
    except:
        raise Exception('Image type {} is not recognized'.format(image_type))
    ext = 'jpg'
    data_path = os.path.join(TRAIN_DATA, image_type)
    return os.path.join(data_path, '{}.{}'.format(image_id, ext))
import cv2
def get_image_data(image_id, image_type):
    """
    Method to get image data as np.array specifying image id and type
    """
    fname = get_filename(image_id, image_type)
    img = cv2.imread(fname)
    img = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)
    return img

l = len(type_1_ids)
np.floor(25.6)
tile_size

tile_size = (256, 256)
n = 10
m = int(np.floor(l / n))
test_zeros = np.zeros((2, 4, 3), dtype=np.uint8)
test_zeros

tile_size = (256, 256)
n = 15
complete_images = []
for k, type_ids in enumerate([type_1_ids, type_2_ids, type_3_ids]):
    m = int(np.floor(len(type_ids) / n))
    complete_image = np.zeros((m * (tile_size[0] + 2), n * (tile_size[1] + 2), 3), dtype=np.uint8)
    train_ids = sorted(type_ids)
    counter = 0
    for i in range(m):
        ys = i * (tile_size[1] + 2)
        ye = ys + tile_size[1]
        for j in range(n):
            xs = j * (tile_size[0] + 2)
            xe = xs + tile_size[0]
            image_id = train_ids[counter]
            counter += 1
            img = get_image_data(image_id, 'Type_%i' % (k + 1))
            img = cv2.resize(img, dsize=tile_size)
            complete_image[ys:ye, xs:xe] = img[:, :, :]
    complete_images.append(complete_image)

plt_st()

plt_st(20, 20)
plt.imshow(complete_images[0])
plt.title('Training dataset of type %i' % 1)