Esempio n. 1
0
import pickle
import numpy as np
from shutil import copyfile

import solver
import computer_vision.helper as cv
import neural_net.digits as nn
from neural_net.Dataset import Dataset
from Sudoku import classification_mode
from Sudoku import Sudoku

# Static variables, mostly file locations
DIGITS = '0123456789'
IMAGE_DIR = os.path.join('data', 'images')
STAGE_DIR = os.path.abspath(os.path.join(IMAGE_DIR, 'stage'))
CLASSIFIED_DIR = os.path.abspath(os.path.join(IMAGE_DIR, 'classified', classification_mode()))
CL_TRAIN_DIR = os.path.join(CLASSIFIED_DIR, 'train')
CL_TEST_DIR = os.path.join(CLASSIFIED_DIR, 'test')
DIGITS_DIR = os.path.abspath(os.path.join(IMAGE_DIR, 'raw', 'digits'))
GRID_DIR = os.path.abspath(os.path.join(IMAGE_DIR, 'grid', 'all'))
TRAIN_DIR = os.path.abspath(os.path.join(IMAGE_DIR, 'grid', 'train'))
TEST_DIR = os.path.abspath(os.path.join(IMAGE_DIR, 'grid', 'test'))
DATA_DIR = os.path.abspath(os.path.join('data', 'datasets'))
DATA_FILE = os.path.join(DATA_DIR, classification_mode())
MODEL_DIR = os.path.join('data', 'models', classification_mode())
DIGIT_MODEL = os.path.join(MODEL_DIR, 'model.ckpt')


def mkdir(dir_):
    if not os.path.exists(dir_):
        os.mkdir(dir_)
Esempio n. 2
0
import os
import pickle
import unittest

import neural_net.digits as nn
from neural_net.Dataset import Dataset
from Sudoku import classification_mode

DATA_DIR = os.path.abspath(os.path.join('..', 'data', 'datasets'))
DATA_FILE = os.path.join(DATA_DIR, classification_mode())
MODEL = os.path.join('..', 'data', 'models', classification_mode(),
                     'model.ckpt')


def load_data(file_name):
    """Loads Python object from disk."""
    with open(file_name, 'rb') as f:
        data = pickle.load(f)
    return data


class NNTrainer(unittest.TestCase):
    def test_training(self):
        try:
            digits = load_data(DATA_FILE)
            nn.train(digits, MODEL, test_only=True, steps=100)
            self.assertTrue(
                True
            )  # Simply testing if the training can proceed without error
        except Exception as err:
            self.assertRaises(err)
Esempio n. 3
0
import os
import pickle
import unittest

import neural_net.digits as nn
from Sudoku import classification_mode

DATA_DIR = os.path.abspath(os.path.join('..', 'data', 'datasets'))
DATA_FILE = os.path.join(DATA_DIR, classification_mode())
MODEL = os.path.join('..', 'data', 'best-model', 'model.ckpt')


def load_data(file_name):
    """Loads Python object from disk."""
    with open(file_name, 'rb') as f:
        data = pickle.load(f)
    return data


class NNTrainer(unittest.TestCase):
    def test_training(self):
        try:
            digits = load_data(DATA_FILE)
            nn.train(digits, MODEL, test_only=True, steps=100)
            self.assertTrue(
                True
            )  # Simply testing if the training can proceed without error
        except Exception as err:
            self.assertRaises(err)

Esempio n. 4
0
import os
import cv2
import unittest

from neural_net.DigitRecogniser import DigitRecogniser
from Sudoku import classification_mode

IMG_DIR = os.path.join('..', 'data', 'images', 'classified',
                       classification_mode(), 'test')
DIGIT_MODEL = os.path.join('..', 'data', 'models', classification_mode(),
                           'model.ckpt')
TOLERANCE = 0.98  # Percentage of successful recognition to constitute a pass
DIGIT_RECOGNISER = DigitRecogniser(DIGIT_MODEL)


def test_all_digits(digit):
    """Tests all images of a digit in the classified directory using the neural network for recognising digits."""
    digit_dir = os.path.join(IMG_DIR, str(digit))
    all_images = []
    for img_path in os.listdir(digit_dir):
        img = cv2.imread(os.path.join(digit_dir, img_path),
                         cv2.IMREAD_GRAYSCALE)
        if img is not None:  # Skips any hidden files that are not images

            # Resize image if it isn't square
            height, width = img.shape[:2]
            if height != 28 or width != 28:
                img = cv2.resize(img, (28, 28))

            all_images.append(img)
    guesses = DIGIT_RECOGNISER.predict_digit(all_images)
Esempio n. 5
0
import os
import cv2
import unittest

from neural_net.DigitRecogniser import DigitRecogniser
from Sudoku import classification_mode

IMG_DIR = os.path.join('..', 'data', 'images', 'classified', classification_mode(), 'test')
DIGIT_MODEL = os.path.join('..', 'data', 'models', classification_mode(), 'model.ckpt')
TOLERANCE = 0.98  # Percentage of successful recognition to constitute a pass
DIGIT_RECOGNISER = DigitRecogniser(DIGIT_MODEL)


def test_all_digits(digit):
    """Tests all images of a digit in the classified directory using the neural network for recognising digits."""
    digit_dir = os.path.join(IMG_DIR, str(digit))
    all_images = []
    for img_path in os.listdir(digit_dir):
        img = cv2.imread(os.path.join(digit_dir, img_path), cv2.IMREAD_GRAYSCALE)
        if img is not None:  # Skips any hidden files that are not images

            # Resize image if it isn't square
            height, width = img.shape[:2]
            if height != 28 or width != 28:
                img = cv2.resize(img, (28, 28))

            all_images.append(img)
    guesses = DIGIT_RECOGNISER.predict_digit(all_images)
    wrong = len(list(filter(lambda x: int(x) != int(digit), guesses)))
    errors = wrong / len(all_images)
    return errors