net = NetworkReader.readFrom('nnet.xml') print "... Loaded network from file" from PIL import Image from lib_threshold import identifyLetters, makeWhiteOnBlack, matrixToImage, imageToNPMatrix, thresholdMatrix from lib_LetterBBox import fitTo28x28 from lib_dataset import nnetResultToChar, topNResults, chars import numpy # first open image print "Loading image file..." image = Image.open('./test-text.png') print "Extracting letters..." # extract letter data letters = identifyLetters(image.convert('L')) # flatten to array inputs and clamp between 0 and 1 letters_processed = [fitTo28x28(matrixToImage(img)) for (pos, img) in letters] # show us what they look like for (letter, num) in zip(letters_processed, range(0, len(letters_processed))): letter.save('input-' + str(num) + '.png') # create the inputs for the nnet nnetInputs = [ imageToNPMatrix(fitTo28x28(matrixToImage(img))).flatten() for (pos, img) in letters ] # create the outputs for the nnet nnetOutputs = [net.activate(input) for input in nnetInputs]
def charToOutputs(char): arr = [0] * len(chars) arr[chars.index(char)] = 1 return arr for fontName in fontNames: for size in range(24, 28, 2): font = ImageFont.truetype(fontName, size) img = Image.new("L", (1000, 40), (0, 0, 0)) draw = ImageDraw.Draw(img) draw.text((0, 0), string, (255, 255, 255), font=font) img.save(fontName + '.png') letters = identifyLetters(img) _28x28Images = [(symbol, imageToNPMatrix(fitTo28x28(matrixToImage(letter[1]))), charToOutputs(symbol)) for (symbol, letter) in zip(chars, letters)] _28x28Images = [(symbol, image, outputs) for (symbol, image, outputs) in _28x28Images] if isFirst: isFirst = False for (symbol, matrix, outputs) in _28x28Images: printMatrix(matrix) matrixToImage(matrix).save(fontName + '-' + symbol + '.png') datums = [(symbol, map0255to01(list(matrix.flatten())), outputs) for (symbol, matrix, outputs) in _28x28Images] for d in datums:
def charToOutputs(char): arr = [0] * len(chars) arr[chars.index(char)] = 1 return arr for fontName in fontNames: for size in range(24, 28, 2): font = ImageFont.truetype(fontName, size) img = Image.new("L", (1000, 40), (0, 0, 0)) draw = ImageDraw.Draw(img) draw.text((0, 0), string, (255,255,255), font=font) img.save(fontName + '.png') letters = identifyLetters(img) _28x28Images = [(symbol, imageToNPMatrix(fitTo28x28(matrixToImage(letter[1]))), charToOutputs(symbol)) for (symbol, letter) in zip(chars, letters)] _28x28Images = [(symbol, image, outputs) for (symbol, image, outputs) in _28x28Images] if isFirst: isFirst = False for (symbol, matrix, outputs) in _28x28Images: printMatrix(matrix) matrixToImage(matrix).save(fontName + '-' + symbol + '.png') datums = [(symbol, map0255to01(list(matrix.flatten())), outputs) for (symbol, matrix, outputs) in _28x28Images] for d in datums: dataset.append(d) '''
net = NetworkReader.readFrom("nnet.xml") print "... Loaded network from file" from PIL import Image from lib_threshold import identifyLetters, makeWhiteOnBlack, matrixToImage, imageToNPMatrix, thresholdMatrix from lib_LetterBBox import fitTo28x28 from lib_dataset import nnetResultToChar, topNResults, chars import numpy # first open image print "Loading image file..." image = Image.open("./test-text.png") print "Extracting letters..." # extract letter data letters = identifyLetters(image.convert("L")) # flatten to array inputs and clamp between 0 and 1 letters_processed = [fitTo28x28(matrixToImage(img)) for (pos, img) in letters] # show us what they look like for (letter, num) in zip(letters_processed, range(0, len(letters_processed))): letter.save("input-" + str(num) + ".png") # create the inputs for the nnet nnetInputs = [imageToNPMatrix(fitTo28x28(matrixToImage(img))).flatten() for (pos, img) in letters] # create the outputs for the nnet nnetOutputs = [net.activate(input) for input in nnetInputs] def prePostProcessNnetOutput(nnetOutputs):