#!/usr/bin/env python
# -*- coding: utf-8 -*-
from __future__ import print_function
import os

from PIL import Image
import numpy as np


dpath_home = os.path.expanduser('~')
dpath_train = os.path.join(dpath_home, 'data/Caltech-101/train/all')

img_arrays = []

for label in range(6):
    dpath = os.path.join(dpath_train, str(label))
    img_names = os.listdir(dpath)

    for i, img_name in enumerate(img_names):
        img_path = os.path.join(dpath, img_name)
        img = Image.open(img_path)
        img = img.convert('RGB')
        img_array = np.array(img)
        img_array = np.rollaxis(img_array, 2)
        img_arrays.append(img_array)

img_arrays = np.asarray(img_arrays, np.float64) / 255

print('# number of images')
print(len(img_arrays))

print('# mean')
for channel_index in range(3):
    mean = np.mean(img_arrays[:, channel_index, :, :])
    mean = round(mean, 3)
    print(str.format("channel {}: {:.3f}", channel_index, mean))

print('# std')
for channel_index in range(3):
    std = np.std(img_arrays[:, channel_index, :, :])
    std = round(std, 3)
    print(str.format("channel {}: {:.3f}", channel_index, std))
