def getNext(self):
     batchRange = range(self.currIdx, self.currIdx + self.batchSize)
     gtTexts = [self.samples[i].gtText for i in batchRange]
     imgs = [preprocessor(cv2.imread(self.samples[i].filePath, cv2.IMREAD_GRAYSCALE), self.imgSize)
         for i in batchRange]
     self.currIdx += self.batchSize
     return Batch(gtTexts, imgs)
def image_generator(files, max_label_length, batch_size=16, imgSize=(256, 32)):
    index_ = 0
    while True:
        # Select files (paths/indices) for the batch
        batch_paths = files[index_ * batch_size:(index_ + 1) * batch_size]

        training_img = []
        training_txt = []
        train_label_length = []
        train_input_length = []
        max_label_len = max_label_length
        # Read in each input, perform preprocessing and get labels
        for input_path in batch_paths:
            gtText1 = input_path.split('/')[-1].split('.')[0]
            # print(gtText1)
            encode_to_labels(gtText1)
            training_txt.append(encode_to_labels(gtText1))
            train_label_length.append(len(gtText1))

            img = preprocessor(filePath=input_path, imgSize=imgSize, rotation=True)
            training_img.append(img)
            train_input_length.append(max_label_len)

        train_padded_txt = pad_sequences(training_txt, maxlen=max_label_len, padding='post', value=len(char_list))
        train_input_length = np.array(train_input_length)
        train_label_length = np.array(train_label_length)
        training_img = np.array(training_img)
        output = np.zeros(len(training_img))

        input_ = [training_img, train_padded_txt, train_input_length, train_label_length]
        index_ = index_ + 1
        if index_ >= len(files) // batch_size:
            index_ = 0
        yield (input_, output)
 def _getData(self, ):
     self.gtTexts = []
     self.imgs = []
     for sample in self.samples:
         self.gtTexts.append(sample.gtText)
         self.imgs.append(
             preprocessor(cv2.imread(sample.filePath, cv2.IMREAD_GRAYSCALE),
                          self.imgSize))
def load_different_image():
    imgs = []
    for i in range(1, Model.batchSize):
        imgs.append(
            preprocessor(cv2.imread("../data/check_image/a ({}).png".format(i),
                                    cv2.IMREAD_GRAYSCALE),
                         Model.imgSize,
                         enhance=False))
    return imgs
예제 #5
0
def load_different_image():
    imgs = []
    for i in range(1, Model.batchSize):
        imgs.append(
            preprocessor(cv2.imread(
                "/content/drive/My Drive/Handwritten-Line-Text-Recognition-using-Deep-Learning-with-Tensorflow-master/data/check_image/a ({}).png"
                .format(i), cv2.IMREAD_GRAYSCALE),
                         Model.imgSize,
                         enhance=False))
    return imgs
def infer(model, fnImg):
    """ Recognize text in image provided by file path """
    img = preprocessor(cv2.imread(fnImg, cv2.IMREAD_GRAYSCALE), imgSize=Model.imgSize)
    if img is None:
        print("Image not found")

    imgs = load_different_image()
    imgs = [img] + imgs
    batch = Batch(None, imgs)
    recognized = model.inferBatch(batch)  # recognize text

    print("Without Correction", recognized[0])
    print("With Correction", correct_sentence(recognized[0]))
    return recognized[0]
def image_generator_test(files, imgSize=(256, 32), batch_size=16):
    index_ = 0
    while True:
        # Select files (paths/indices) for the batch
        batch_paths = files[index_ * batch_size:(index_ + 1) * batch_size]

        training_img = []

        # Read in each input, perform preprocessing and get labels
        for input_path in batch_paths:
            img = preprocessor(filePath=input_path, imgSize=imgSize, rotation=True)
            training_img.append(img)

        training_img = np.array(training_img)

        index_ = index_ + 1
        if index_ >= len(files) // batch_size:
            index_ = 0

        yield training_img
예제 #8
0
파일: CRNN.py 프로젝트: Sangeerththan/DLOCR
valid_input_length = []
valid_label_length = []
valid_orig_txt = []
max_label_len = 56

i = 1
flag = 0

train_paths = open(fnTrain, 'r').read().split('\n')[:-1]
test_paths = open(fnTest, 'r').read().split('\n')[:-1]

for i1 in train_paths:
    gtText1 = i1.split('_')[-1].split('.')[0]
    training_txt.append(encode_to_labels(gtText1))
    train_label_length.append(len(gtText1))
    img = preprocessor(i1, (256, 64), rotation=True)
    training_img.append(img)
    train_input_length.append(max_label_len)

for i2 in test_paths:
    gtText2 = i2.split('_')[-1].split('.')[0]
    valid_orig_txt.append(gtText2)
    valid_txt.append(encode_to_labels(gtText2))
    valid_label_length.append(len(gtText2))
    img = preprocessor(i2, (256, 64), rotation=True)
    valid_img.append(img)
    valid_input_length.append(max_label_len)

train_padded_txt = pad_sequences(training_txt, maxlen=max_label_len, padding='post', value=len(char_list))
valid_padded_txt = pad_sequences(valid_txt, maxlen=max_label_len, padding='post', value=len(char_list))