import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import tensorflow as tf
import cv2
from ocr.normalization import imageNorm, letterNorm
from ocr import page, words, charSeg
from ocr.helpers import implt, resize
from ocr.tfhelpers import Graph
from ocr.datahelpers import idx2char
IMG = '1'
LANG = 'cz'
MODEL_LOC = 'models/char-clas/' + LANG + '/CharClassifier'
charClass = Graph(MODEL_LOC)
image = cv2.cvtColor(cv2.imread("test/%s.jpg" % IMG), cv2.COLOR_BGR2RGB)
implt(image)
crop = page.detection(image)
implt(crop)
bBoxes = words.detection(crop)
class Cycler:
""" Cycle through the words and recognise them """
height = 60
def __init__(self, image, boxes):
self.boxes = boxes
self.image = image
def recognise(self, img):
""" Recognising word and printing it """
img = imageNorm(img, 60, border=False, tilt=True, hystNorm=True)
img = cv2.copyMakeBorder(img, 0, 0, 30, 30,cv2.BORDER_CONSTANT, value=[0, 0, 0])
gaps = charSeg.segmentation(img, RNN=True, debug=True)
chars = []
for i in range(len(gaps)-1):
char = img[:, gaps[i]:gaps[i+1]]
char, dim = letterNorm(char, is_thresh=True, dim=True)
if dim[0] > 4 and dim[1] > 4:
chars.append(char.flatten())
chars = np.array(chars)
word = ''
if len(chars) != 0:
pred = charClass.run(chars)
for c in pred:
word += idx2char(c)
print("Word: " + word)
def idxImage(self, index):
""" Getting next image from the array """
if index < len(self.boxes):
b = self.boxes[index]
x1, y1, x2, y2 = b
img = self.image[y1:y2, x1:x2]
implt(img, t='Index: ' + str(index))
self.recognise(img)
cycler = Cycler(crop, bBoxes)
for i in range(len(bBoxes)):
cycler.idxImage(i)