Ejemplo n.º 1
0
    def recognise_char_sep_model(self, img):
        """Recognition using character model"""
        # Pre-processing the word
        img = word_normalization(
            img,
            60,
            border=False,
            tilt=True,
            hyst_norm=True)

        # Separate letters
        img = cv2.copyMakeBorder(
            img,
            0, 0, 30, 30,
            cv2.BORDER_CONSTANT,
            value=[0, 0, 0])
        gaps = characters.segment(img, RNN=True)

        chars = []
        for i in range(len(gaps) - 1):
            char = img[:, gaps[i]:gaps[i + 1]]
            char, dim = letter_normalization(char, is_thresh=True, dim=True)
            # TODO Test different values
            if dim[0] > 4 and dim[1] > 4:
                chars.append(char.flatten())

        chars = np.array(chars)
        word = ''
        if len(chars) != 0:
            pred = CHARACTER_MODEL.run(chars)
            for c in pred:
                word += idx2char(c)

        return word
Ejemplo n.º 2
0
def words_norm(location, output):
    output = os.path.join(location, output)
    if not os.path.exists(output):
        os.makedirs(output)
    else:
        print("THIS DATASET IS BEING SKIPPED")
        print("Output folder already exists:", output)
        return 1

    imgs = glob.glob(os.path.join(location, data_folder, '*.png'))
    length = len(imgs)

    for i, img_path in enumerate(imgs):
        image = cv2.imread(img_path)
        # Simple check for invalid images
        if image.shape[0] > 20:
            cv2.imwrite(
                os.path.join(output, os.path.basename(img_path)),
                word_normalization(image,
                                   height=64,
                                   border=False,
                                   tilt=False,
                                   hyst_norm=False))
        print(i)
        print_progress_bar(i, len(imgs))

    print("\tNumber of normalized words:",
          len([n for n in os.listdir(output)]))
Ejemplo n.º 3
0
def recognise(img):
    """Recognising words using CTC Model."""
    img = word_normalization(img,
                             64,
                             border=False,
                             tilt=False,
                             hyst_norm=False)
    length = img.shape[1]
    # Input has shape [batch_size, height, width, 1]
    input_imgs = np.zeros((1, 64, length, 1), dtype=np.uint8)
    input_imgs[0][:, :length, 0] = img

    pred = CTC_MODEL.eval_feed({
        'inputs:0': input_imgs,
        'inputs_length:0': [length],
        'keep_prob:0': 1
    })[0]

    word = ''
    for i in pred:
        word += idx2char(i)
    return word