예제 #1
0
 def __init__(self, img, config={}):
     self.img = img
     self.step = 1
     self.start = None
     self.width = img.shape[1]
     self.height = img.shape[0]
     self.config = Configuration(config, DEFAULTS)
 def __init__(self, regions, model_path, data_config={}):
     self.model_path = model_path
     self._load_vocab()
     self._load_meta()
     self._scaling = 1.0
     self._max_height = 10000
     self._max_width = 10000
     self.set_regions(regions)
     self.data_config = Configuration(data_config, DEFAULT_DATACONFIG)
     self.augmenter = ImageAugmenter(self.data_config)
예제 #3
0
 def __init__(self, name, transpose=True, data_config={}):
     self.name = name
     self.data_config = Configuration(data_config)
     self.min_width_factor = 15
     self.max_min_width = 400
     self.datapath = os.path.join(util.OUTPUT_PATH, name)
     self._load_vocab()
     self._load_meta()
     self._load_sets()
     self._calc_max_length()
     self._compile_sets()
     self.transpose = transpose
     self.channels = 1
     self._fill_meta()
     self.augmenter = ImageAugmenter(self.data_config)
     self.unfiltered = {}
예제 #4
0
 def __init__(self, **kwargs):
     self.paper_note_path = kwargs.get(
         'paper_note_path', '../paper-notes/data/final')
     self.slice_width = kwargs.get('slice_width', 320)
     self.slice_height = kwargs.get('slice_height', 320)
     self.filter = kwargs.get('filter', True)
     self.binarize = kwargs.get('binarize', False)
     self.single_page = kwargs.get('single_page', False)
     self.slicer = Slicer(**kwargs)
     self.meta = Configuration({})
     self.shuffle = kwargs.get('shuffle', True)
     self.vocab = {}
     self._load_filelists()
     self.augmenter = ImageAugmenter(kwargs.get('config', {
         "otf_augmentations": {}
     }))
     self.otf_mentioned = False
예제 #5
0
    def configure(self, config={}):
        config = Configuration(config)

        def conf(key):
            return config.default(key, self.DEFAULTS.default(key, 1))

        self.translate(prob=conf('translate.prob'),
                       center=conf('translate.center'),
                       stdv=conf('translate.stdv'))
        self.rotate(prob=conf('rotate.prob'),
                    center=conf('rotate.center'),
                    stdv=conf('rotate.stdv'))
        self.shear(prob=conf('shear.prob'),
                   center=conf('shear.center'),
                   stdv=conf('shear.stdv'))
        self.scale(prob=conf('scale.prob'),
                   center=conf('scale.center'),
                   stdv=conf('scale.stdv'))
 def _load_meta(self):
     self.meta = Configuration(util.loadJson(self.model_path, "data_meta"))
예제 #7
0
 def __init__(self, config, defaults, data_format='nhwc'):
     self._config = Configuration(config)
     self._defaults = Configuration(defaults)
     self._format = data_format
예제 #8
0
 def __init__(self, config={}):
     self.config = Configuration(config)
     self.default = Configuration(DEFAULTS)
     self.max_size = (0, 0)
     self.max_height = -1
     self.max_width = -1
 def __init__(self, config, defaults):
     self._config = Configuration(config)
     self._defaults = Configuration(defaults)
예제 #10
0
 def __init__(self, config={}):
     self.config = Configuration(config, DEFAULT_CONFIG)
 def __init__(self, config={}):
     self.config = Configuration(config, DEFAULT_CONFIG)
     self._build_dictionary()
예제 #12
0
 def __init__(self, config={}, globalConfig={}):
     self.config = Configuration(config)
     self.globalConfig = Configuration(globalConfig)
     self._parse_config()
     self.logger = Logger()
     self.config()
예제 #13
0
 def augment(self, img, get_settings=False):
     augmentation_settings = {}
     if "warp" in self.config["otf_augmentations"]:
         if np.random.uniform() < self.config['otf_augmentations.warp.prob']:
             if(not self.config.default('preprocess.invert', False)):
                 img = 255 - img
             reshaped = False
             if len(img.shape) > 2:
                 reshaped = True
                 img = np.reshape(img, (img.shape[0], img.shape[1]))
             img = convert._cv2pil(img)
             img, mat = warp._warp(
                 img,
                 gridsize=self.config['otf_augmentations.warp.gridsize'],
                 deviation=self.config['otf_augmentations.warp.deviation'],
                 return_mat=True)
             augmentation_settings["warp"] = {
                 "gridsize": self.config['otf_augmentations.warp.gridsize'],
                 "mat": mat
             }
             img = convert._pil2cv2(img)
             if reshaped:
                 img = np.reshape(img, (img.shape[0], img.shape[1], 1))
             if(not self.config.default('preprocess.invert', False)):
                 img = 255 - img
     if "affine" in self.config["otf_augmentations"]:
         if(self.config.default('preprocess.invert', False)):
             img = 255 - img
         img, mat = affine._affine(
             img, self.config["otf_augmentations.affine"], return_mat=True)
         augmentation_settings["affine"] = {
             "mat": mat
         }
         if(self.config.default('preprocess.invert', False)):
             img = 255 - img
     if "morph" in self.config["otf_augmentations"]:
         img, op_name, op_values = morph._random_morph(
             img, self.config["otf_augmentations.morph"], self.config.default('preprocess.invert', False), True)
         augmentation_settings["affine"] = {
             "op_name": op_name,
             "op_values": op_values
         }
     if "binarize" in self.config["otf_augmentations"]:
         if np.random.uniform() < self.config['otf_augmentations.binarize.prob']:
             img = binarize._binarize(img)
             augmentation_settings["binarize"] = {}
     if "blur" in self.config["otf_augmentations"]:
         if np.random.uniform() < self.config['otf_augmentations.blur.prob']:
             img = cv2.GaussianBlur(
                 img, tuple(self.config['otf_augmentations.blur.kernel']), self.config['otf_augmentations.blur.sigma'])
             augmentation_settings["blur"] = {
                 "kernel": self.config['otf_augmentations.blur.kernel'],
                 "sigma": self.config['otf_augmentations.blur.sigma']
             }
     if "sharpen" in self.config["otf_augmentations"]:
         if np.random.uniform() < self.config['otf_augmentations.sharpen.prob']:
             img = self._unsharp_mask_filter(
                 img, tuple(self.config['otf_augmentations.sharpen.kernel']), self.config['otf_augmentations.sharpen.sigma'])
             augmentation_settings["sharpen"] = {
                 "kernel": self.config['otf_augmentations.sharpen.kernel'],
                 "sigma": self.config['otf_augmentations.sharpen.sigma']
             }
     if "brighten" in self.config["otf_augmentations"]:
         if np.random.uniform() < self.config['otf_augmentations.brighten.prob']:
             factor = np.random.normal(
                 self.config['otf_augmentations.brighten.center'], self.config['otf_augmentations.brighten.stdv'])
             factor = factor if factor >= 1 else 1
             img = np.uint8(np.clip(img * factor, 0, 255))
             augmentation_settings["brighten"] = {
                 "factor": factor
             }
     if "darken" in self.config["otf_augmentations"]:
         if np.random.uniform() < self.config['otf_augmentations.darken.prob']:
             factor = np.random.normal(
                 self.config['otf_augmentations.darken.center'], self.config['otf_augmentations.darken.stdv'])
             factor = factor if factor >= 1 else 1
             img = 255 - np.uint8(np.clip((255 - img) * factor, 0.0, 255.0))
             augmentation_settings["darken"] = {
                 "factor": factor
             }
     if not get_settings:
         return self.add_graychannel(img)
     else:
         return self.add_graychannel(img), Configuration(augmentation_settings)
예제 #14
0
class AffineTransformation(object):

    DEFAULTS = Configuration({
        "translate": {
            "prob": 0.5,
            "center": 2,
            "stdv": 0.02
        },
        "rotate": {
            "prob": 0.5,
            "center": 0,
            "stdv": 0.1
        },
        "shear": {
            "prob": 0.5,
            "center": 0,
            "stdv": 0.25
        },
        "scale": {
            "prob": 0.5,
            "center": 0,
            "stdv": 0.06
        }
    })

    def __init__(self, img):
        self.reset()
        self.img = img

    def reset(self):
        self.M = np.eye(3, 3)

    def set(self, M):
        self.M = M

    def __call__(self, background=None):
        if background is None:
            return cv2.warpAffine(self.img, self.M[:2, :],
                                  (self.img.shape[1], self.img.shape[0]))
        else:
            return cv2.warpAffine(self.img,
                                  self.M[:2, :],
                                  (self.img.shape[1], self.img.shape[0]),
                                  borderMode=cv2.BORDER_CONSTANT,
                                  borderValue=(255, 255, 255))

    ######################################
    # Random Transformations            #
    #####################################
    def translate(self, prob=0.5, stdv=0.02, center=2):
        if np.random.uniform() < prob:
            t = np.random.normal(center, stdv)
            self._apply(self._translate(t, t))

    def rotate(self, prob=0.5, stdv=0.2, center=0):
        if np.random.uniform() < prob:
            stdv = np.sqrt(
                1 / max(self.img.shape[0] / self.img.shape[1],
                        self.img.shape[1] / self.img.shape[0])) * stdv
            alpha = np.random.normal(center, stdv)
            self._centered(self._rotate(alpha))

    def shear(self, prob=0.5, stdv=0.25, center=0):
        if np.random.uniform() < prob:
            s = np.random.normal(center, stdv)
            self._centered(self._shear(s, 0))

    def scale(self, prob=0.5, stdv=0.12, center=0):
        if np.random.uniform() < prob:
            f = np.exp(np.random.normal(center, stdv))
            self._centered(self._scale(f, f))

    def configure(self, config={}):
        config = Configuration(config)

        def conf(key):
            return config.default(key, self.DEFAULTS.default(key, 1))

        self.translate(prob=conf('translate.prob'),
                       center=conf('translate.center'),
                       stdv=conf('translate.stdv'))
        self.rotate(prob=conf('rotate.prob'),
                    center=conf('rotate.center'),
                    stdv=conf('rotate.stdv'))
        self.shear(prob=conf('shear.prob'),
                   center=conf('shear.center'),
                   stdv=conf('shear.stdv'))
        self.scale(prob=conf('scale.prob'),
                   center=conf('scale.center'),
                   stdv=conf('scale.stdv'))

        ######################################
        # Tranformation Matrix Builder       #
        ######################################

    def _apply(self, D):
        self.M = np.matmul(self.M, D)

    def _centered(self, D):
        tx = self.img.shape[1] / 2
        ty = self.img.shape[0] / 2
        C = self._translate(tx, ty)
        Cm = self._translate(-tx, -ty)
        self.M = np.matmul(np.matmul(C, D), Cm)

    def _rotate(self, alpha):
        alpha = np.deg2rad(alpha)
        return np.float32([[np.cos(alpha), np.sin(alpha), 0],
                           [-np.sin(alpha), np.cos(alpha), 0], [0, 0, 1]])

    def _translate(self, tx, ty):
        return np.float32([[1, 0, tx], [0, 1, ty], [0, 0, 1]])

    def _scale(self, fx, fy):
        return np.float32([[fx, 0, 0], [0, fy, 0], [0, 0, 1]])

    def _shear(self, sx, sy):
        return np.float32([[1, sx, 0], [sy, 1, 0], [0, 0, 1]])
class PaperNoteSlicesSingle(Dataset):

    slices = []
    img = None
    vocab = {}
    meta = Configuration({})

    def __init__(self, **kwargs):
        self.slice_width = kwargs.get('slice_width', 320)
        self.slice_height = kwargs.get('slice_height', 320)
        self.binarize = kwargs.get('binarize', False)
        self.binarize_method = kwargs.get('binarize_method', "fast_threshold")
        self.slicer = Slicer(**kwargs)

    def load_file(self, filepath):
        return self.set_image(cv2.imread(filepath, cv2.IMREAD_GRAYSCALE))

    def set_image(self, image):
        if len(image.shape) > 2 and image.shape[2] == 3:
            image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
        self.img = image
        self.slices = self.slicer(self.img)
        if self.binarize:
            self.slices = [self.binarization(slc) for slc in self.slices]
        return self.img

    def info(self):
        pass

    def compile(self, text):
        return text

    def decompile(self, values):
        return values

    def merge_slices(self, slices, original_shape):
        return self.slicer.merge(slices, original_shape)

    def binarization(self, img):
        if (self.binarize_method == "fast_threshold"):
            _, out = cv2.threshold(img, 254, 255, cv2.THRESH_BINARY)
        elif (self.binarize_method == "sauvola"):
            out = self._sauvola(img)
        elif (self.binarize_method == "mean"):
            out = np.uint8((img > np.mean(img)) * 255)
        elif (self.binarize_method == "mean_threshold"):
            mean = np.mean(self.img)
            _, out = cv2.threshold(img, mean, 255, cv2.THRESH_BINARY)
        return self.graychannel(out)

    def _sauvola(self, img):
        thresh_sauvola = threshold_sauvola(
            img, window_size=19)  # todo: make configurable
        return np.uint8((img > thresh_sauvola) * 255)

    def graychannel(self, img):
        if len(img.shape) > 2:
            return img
        return np.reshape(img, [img.shape[0], img.shape[1], 1])

    def generateBatch(self,
                      batch_size=0,
                      max_batches=0,
                      dataset="",
                      with_filepath=False):
        for idx in range(self.getBatchCount(batch_size, max_batches)):
            slices = np.asarray(self.slices[(idx * batch_size):(
                (idx + 1) * batch_size)]) / 255.0
            if with_filepath:
                yield slices, [], [], []
            else:
                yield slices, [], []
        pass

    def generateEpochs(self,
                       batch_size,
                       num_epochs,
                       max_batches=0,
                       dataset="train",
                       with_filepath=False):
        return [self.generateBatch()]

    def getBatchCount(self, batch_size, max_batches=0, dataset="train"):
        batch_count = np.ceil(len(self.slices) / batch_size)
        return int(batch_count)
예제 #16
0
 def __init__(self, config={}):
     self.config = Configuration(config, DEFAULTS)
     self.astar = AStarLineSegmentation(self.config["extractor"])
 def __init__(self, config={}, default_config=DEFAULT_CONFIG):
     self.config = Configuration(config, default_config)
 def __init__(self, **kwargs):
     self.config = Configuration(kwargs.get('config', {}))
예제 #19
0
from lib.Configuration import Configuration

c = Configuration()


def add_tracking_code(request):
    if c.is_tracking_active():
        return {"tracking_code": c.get_tracking_code()}
    else:
        return {}
예제 #20
0
 def _load_meta(self):
     self.meta = Configuration(util.loadJson(self.datapath, "meta"))
예제 #21
0
 def __init__(self, config):
     self.config = Configuration(config)
예제 #22
0
import cv2
import numpy as np
from lib.Configuration import Configuration
from data.datasets import identifyDataset
from data.steps.blender import PageHandwritingBlender
from . import util
import os
import sys
import random

CONFIG = Configuration({
    "data": {
        "line": "iam",
        "line_subset": "lines",
        "pages": "paper/png"
    },
    "lines_per_page": 7,
    "blending": {}
})

pdfpath = os.path.join(util.RAW_PATH, CONFIG['data.pages'])
basepath = os.path.join(util.OUTPUT_PATH, "blended")
util.rmkdir(basepath)
truthpath = os.path.join(basepath, "truth")
imgpath = os.path.join(basepath, "img")
os.makedirs(truthpath)
os.makedirs(imgpath)

# 1. Load Lines
line_dataset = identifyDataset(CONFIG['data.line'])
lines, _ = line_dataset.getFilesAndTruth(util.RAW_PATH,
 def __init__(self, config={}):
     self.config = Configuration(config, DEFAULTS)
     self.region_extractor = WordDetector(self.config["extractor"])
예제 #24
0
 def __init__(self, page, config={}):
     self.page = page
     self.config = Configuration(config)
     self.truth = np.full(page.shape, self['background'])
     self._augment_page()
예제 #25
0
# Cellz Configuration Management

from lib.Configuration import Configuration

CONFIG = Configuration()

# Django settings for homepage_cellz project.

DEBUG = True
TEMPLATE_DEBUG = DEBUG

ADMINS = (
    # ('Your Name', '*****@*****.**'),
)

MANAGERS = ADMINS
""" THE POSTGRES STUFF
'default': {
    'ENGINE': 'django.db.backends.postgresql_psycopg2',  # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
    'NAME': "lootgame", #CONFIG.get_DatabasePath(),  # Or path to database file if using sqlite3.
    'USER': '******',                      # Not used with sqlite3.
    'PASSWORD': '******',                  # Not used with sqlite3.
    'HOST': 'localhost',                      # Set to empty string for localhost. Not used with sqlite3.
    'PORT': '5432',                      # Set to empty string for default. Not used with sqlite3.
}
"""

DATABASES = {
    'default': {
        'ENGINE':
        'django.db.backends.sqlite3',  # Add 'postgresql_psycopg2', 'mysql', 'sqlite3' or 'oracle'.
예제 #26
0
class PageHandwritingBlender(object):

    DEFAULTS = Configuration({
        "background": 255,
        "augmentation": {
            "line": {
                "scale": {
                    "prob": 1.0,
                    "center": -.25,
                    "stdv": 0.15
                }
            },
            "page": [{
                'type': 'blur',
                'prob': 0.5,
                'kernel': (3, 3),
                'sigma': 1
            }, {
                'type': 'sharpen',
                'prob': 0.5,
                'kernel': (3, 3),
                'sigma': 1
            }, {
                'type': 'warp',
                'prob': 0.5,
                'config': {
                    'deviation': 2.7,
                    'gridsize': [100, 30]
                }
            }]
        },
        "filters": {
            'blur':
            lambda i, c: cv2.GaussianBlur(i, c['kernel'], c['sigma']),
            'sharpen':
            lambda i, c: PageHandwritingBlender._unsharp_mask_filter(
                i, c['kernel'], c['sigma']),
            'warp':
            lambda i, c: PageHandwritingBlender._warp_filter(i, c['config']),
            'affine':
            lambda i, c: PageHandwritingBlender._affine_filter(i, c['config'])
        }
    })

    #################################
    # PUBLIC METHODS
    ###############################

    def __init__(self, page, config={}):
        self.page = page
        self.config = Configuration(config)
        self.truth = np.full(page.shape, self['background'])
        self._augment_page()

    def __call__(self, line):
        line = self._augment_line(line)
        h, w, _ = line.shape
        x, y = self._random_position(h, w)
        self._insert(line, x, y)

    def save(self, pagefile, truthfile):
        cv2.imwrite(pagefile, self.page)
        cv2.imwrite(truthfile, self.truth)

    def __getitem__(self, key):
        return self.config.default(key, self.DEFAULTS.default(key, None))

    ############################
    # PRIVATE METHODS
    ################################

    def _random_position(self, h, w):
        ph, pw, pc = self.page.shape

        def rand(mx):
            # loc = np.random.uniform(0, 1)
            # x = abs(np.random.normal(0.0, mx/15.0))
            # return int(x if loc < 0.5 else mx - x)
            x = np.random.uniform(0, mx)
            return int(x)

        return rand(pw - w), rand(ph - h)

    def _insert(self, line, x, y):
        ph, pw, pc = self.page.shape
        lh, lw, lc = line.shape
        off_x = x if lw + x <= pw else x - (lw + x - pw)
        off_y = y if lh + y <= ph else y - (lh + y - ph)
        self.page[off_y:off_y + lh, off_x:off_x + lw, :] &= line
        self.truth[off_y:off_y + lh, off_x:off_x + lw, :] &= line

    def _augment_line(self, line):
        line = cv2.cvtColor(line, cv2.COLOR_BGR2GRAY)
        line = _threshold(line, False)
        line = cv2.cvtColor(line, cv2.COLOR_GRAY2BGR)
        at = AffineTransformation(line)
        at.configure(self['augmentation.line'])
        return at(background=[self['background']] * 3)

    def _augment_page(self):
        for _filter in self['augmentation.page']:
            if _filter['prob'] > np.random.rand():
                self.page = self['filters'][_filter['type']](self.page,
                                                             _filter)

    #######################################
    # STATIC METHODS
    #######################################

    @staticmethod
    def _affine_filter(image, config):
        at = AffineTransformation(image)
        at.configure(config)
        return at()

    @staticmethod
    def _warp_filter(image, config):
        image = _cv2pil(image, 'RGB')
        image = _warp(image, config['gridsize'], config['deviation'])
        return _pil2cv2(image, 'RGB')

    @staticmethod
    def _unsharp_mask_filter(image, kernel, sigma):
        gaussian_3 = cv2.GaussianBlur(image, kernel, sigma)
        return cv2.addWeighted(image, 1.5, gaussian_3, -0.5, 0, image)
예제 #27
0
from django.core.context_processors import csrf
from django.contrib.auth.decorators import login_required
from django.template import RequestContext

from core.content import SkillCategory
from core.controller.tutorial.TutorialRedirectDecorator import TutorialRedirectDecorator
from lib.api.mission import missionAPI
from lib.api.user import userAPI

import lib.log.logger as _logger

_loggerinstance = _logger.getInstance()

from lib.Configuration import Configuration

_configuration = Configuration()


@login_required(login_url='/index/')
@TutorialRedirectDecorator
def view_planet(request):
    if missionAPI.user_participates_in_mission(request.user):
        return HttpResponseRedirect("/current_mission/")
    mission = missionAPI.get_possible_missions_for_player(request.user)
    c = RequestContext(request, {"missions": mission})
    return render_to_response('mission/planet.html', c)


@login_required(login_url='/index/')
def prepare_mission(request, mission_id):
    """ Method for starting a mission """
 def __init__(self, **kwargs):
     self.subset = kwargs.get('subset', 'train')
     self.logger = kwargs.get('logger', None)
     self.every_epoch = kwargs.get('every_epoch', 1)
     self.dataset = kwargs.get('dataset', None)
     self.config = Configuration(kwargs.get('config', {}))