text_html_output_1.png
from pathlib import Path

from sklearn.decomposition import PCA

import cv2

import numpy as np # linear algebra

import os

import pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)

input_path = Path('../input')
train_path = input_path / 'train'
test_path = input_path / 'test'

def get_center_crop(img, d=250):
    cy = img.shape[0] // 2
    cx = img.shape[1] // 2
    return img[cy - d:cy + d, cx - d:cx + d]

def get_blurring(img):
    return np.max(cv2.convertScaleAbs(cv2.Laplacian(img, 3)))

cameras = os.listdir(train_path)
train_images = []
for camera in cameras:
    for fname in sorted(os.listdir(train_path / camera)):
        train_images.append((camera, fname))
train = pd.DataFrame(train_images, columns=['camera', 'fname'])
train.sample(5)

n_components = 5
pca = PCA(n_components=n_components, svd_solver='randomized', whiten=True)

def get_pca_feature(img_channel):
    return pca.fit(img_channel)

random_images = train.sample(100)
random_images[:5]

img_set_reds = []
for i, r in random_images.iterrows():
    x = get_center_crop(cv2.imread('../input/train/' + r['camera'] + '/' + r['fname']))
    img_set_reds.append(np.ravel(x))
img_set_reds = np.asarray(img_set_reds)
pf = pca.fit(np.asarray(img_set_reds))

x = pf.transform(np.ravel(get_center_crop(cv2.imread('../input/train/' + r['camera'] + '/' + r['fname']))).reshape(1, -1))
x

t = get_pca_features(get_center_crop(cv2.imread('../input/train/' + r['camera'] + '/' + r['fname'])))
t[0][1]