#!/usr/bin/python
import argparse
from random import randint

from numpy import random
from pylab import *
from scipy import ndimage as ndi
from dlinputs import gopen, utils

parser = argparse.ArgumentParser("extract patches for training rot/skew models")
parser.add_argument("--display", type=int, default=-1)
parser.add_argument("--npatches", type=int, default=64)
parser.add_argument("--minmean", type=float, default=0.02)
parser.add_argument("--bad", default="")
parser.add_argument("input")
parser.add_argument("output")
args = parser.parse_args()

if args.display > 0:
    rc("image", cmap="gray")
    ion()

bad = set(args.bad.split())


def get_patch(image, center, scale=1.0, alpha=0.0, shape=(256, 256)):
    yx = array(center, 'f')
    hw = array(shape, 'f')
    m = array([[cos(alpha), -sin(alpha)], [sin(alpha), cos(alpha)]], 'f')/scale
    offset = yx - dot(m, hw/2.0)
    return ndi.affine_transform(image, m, offset=offset, output_shape=shape, order=1)


def get_patches(image, npatches=64, shape=(256, 256), ntrials=1024, minmean=0.02, ralpha=5.0):
    patches = []
    h, w = image.shape
    for i in range(ntrials):
        if len(patches) >= npatches:
            break
        y, x = randint(0, h-1), randint(0, w-1)
        alpha = random.uniform(-ralpha*pi/180, ralpha*pi/180)
        scale = random.uniform(0.7, 1.4)
        patch = get_patch(image, (y, x), alpha=alpha, scale=scale, shape=shape)
        if mean(patch) < minmean:
            continue
        patches.append((patch, dict(offset=(y, x), alpha=alpha, scale=scale)))
    return patches


data = gopen.sharditerator_once(args.input)
sink = gopen.open_sink(args.output)
count = 0
for sample in data:
    if sample["__key__"] in bad:
        continue
    utils.print_sample(sample)
    page = sample.get("framed.png")
    if page is None: page = sample["png"]
    patches = get_patches(page, args.npatches, minmean=args.minmean)
    for i, (patch, params) in enumerate(patches[:args.npatches]):
        if args.display > 0 and count % args.display == 0:
            clf()
            imshow(patch)
            ginput(1, 0.001)
        result = {
            "__key__": "{}-{}".format(sample["__key__"], i),
            "patch.png": patch,
            "params.json": params
        }
        sink.write(result)
        count += 1

sink.close()
print "wrote", count, "records"
