Ejemplo n.º 1
0
def process_image(imag):
    # %matplotlib inline
    IMG = imag
    plt.rcParams['figure.figsize'] = (15.0, 10.0)
    """### Global Variables"""
    """## Load image"""
    image = cv2.cvtColor(cv2.imread(IMG), cv2.COLOR_BGR2RGB)
    implt(image)

    # Crop image and get bounding boxes
    crop = page.detection(image)
    implt(crop)
    boxes = words.detection(crop)
    lines = words.sort_words(boxes)
    implt(crop)
    output_file = open("templates/output.html", 'w+')
    output_file.write("")
    output_file.close()
    output_file = open("templates/output.html", 'a+')
    for line in lines:
        print(" ".join(
            [recognise(crop[y1:y2, x1:x2]) for (x1, y1, x2, y2) in line]))
        for (x1, y1, x2, y2) in line:
            text_det = recognise(crop[y1:y2, x1:x2])
            output_file.write(text_det + " ")
        output_file.write("\n")
    output_file.close()
Ejemplo n.º 2
0
def recognize():
    if request.method == 'POST':
        # start = current_milli_time()
        if 'image' not in request.files:
            print('No file part')
            return render_template('index.html')
        file = request.files['image']
        if file.filename == '':
            print('No selected file')
            return render_template('index.html')
        if file:
            file.save(os.path.join('images',file.filename))
            filename = file.filename
            # load the trained model
            charClass = Graph(MODEL_LOC)
            # load the saved image
            image = cv2.cvtColor(cv2.imread("images/"+filename), cv2.COLOR_BGR2RGB)
            crop = page.detection(image)
            bBoxes = words.detection(crop)
            cycler = Cycler.Cycler(crop,bBoxes,charClass)
            allWords = []
            for i in range(len(bBoxes)-1):
                allWords.append(cycler.idxImage(i))
                # print(allWords[i])
    # stop = current_milli_time()
    # print(stop - start)

    return jsonify(allWords=allWords)
Ejemplo n.º 3
0
 def apply_with_source_info(self, im, source_info):
     
     boxes = words.detection(im)
     lines = words.sort_words(boxes)
     print('lines')
     print(lines)
     
     
     self.add_info('bbxy', boxes)       
     self.add_info('lines', lines)
     
     return im
def get_bboxes(img_path):
    img = get_image_from_file(img_path)

    img_gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
    img[:, :, 0][img_gray > 180] = 255
    img[:, :, 0][img_gray <= 180] = 0
    img[:, :, 1][img_gray > 180] = 255
    img[:, :, 1][img_gray <= 180] = 0
    img[:, :, 2][img_gray > 180] = 255
    img[:, :, 2][img_gray <= 180] = 0

    bboxes = words.detection(img)
    return bboxes
def test_recognize():
    charClass = Graph(MODEL_LOC)
    # load the saved image
    image = cv2.cvtColor(cv2.imread(FILE_LOC), cv2.COLOR_BGR2RGB)
    crop = page.detection(image)
    bBoxes = words.detection(crop)
    cycler = Cycler.Cycler(crop, bBoxes, charClass)
    allWords = []
    for i in range(len(bBoxes) - 1):
        allWords.append(cycler.idxImage(i))

    isStructEmpty = structEmpty(allWords)
    assert isStructEmpty == False
Ejemplo n.º 6
0
def main():
    Infilename = sys.argv[1]

    image = cv2.cvtColor(cv2.imread(Infilename), cv2.COLOR_BGR2RGB)
    crop = page.detection(image)
    boxes = words.detection(crop)
    lines = words.sort_words(boxes)
    crop = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY)
    imLines = []
    for line in lines:
        imLine = []
        for (x1, y1, x2, y2) in line:
            imLine.append(crop[y1:y2, x1:x2])
        imLines.append(imLine)

    decoderType = DecoderType.WordBeamSearch
    #decoderType = DecoderType.BeamSearch
    #decoderType = DecoderType.BestPath

    model = Model(open('../model/charList.txt').read(),
                  decoderType,
                  mustRestore=True)
    file1 = open("myfile.txt", "w")
    recognizedL = []

    print(
        "-------------------Predicted Handwritten Text-------------------------"
    )
    for line in imLines:
        imgs = []
        for word in line:
            imgs.append(preprocess(word, Model.imgSize))
        batch = Batch(None, imgs)
        (recognized, probability) = model.inferBatch(batch, True)
        l = ""
        for pw in recognized:
            l += pw
            l += ' '
            print(pw, end=" ")
        print()
        l += '\n'
        recognizedL.append(l)
    file1.writelines(recognizedL)
    file1.close()
def preprocess(empty_img_path, img_path):
    bboxes = get_bboxes(empty_img_path)

    img = get_image_from_file(img_path)
    img = cv2.cvtColor(img, cv2.COLOR_RGB2GRAY)
    img[img > 180] = 255
    img[img <= 180] = 0

    for bx in bboxes:
        x1, y1, x2, y2 = bx
        img[0:256, 0:x2 + 5] = 255

    img_rgb = np.zeros((HEIGHT, WIDTH, 3))
    img_rgb[:, :, 0] = img
    img_rgb[:, :, 1] = img
    img_rgb[:, :, 2] = img

    bboxes = words.detection(img_rgb)

    xx1, yy1, xx2, yy2 = np.inf, np.inf, 0, 0

    for bx in bboxes:
        x1, y1, x2, y2 = bx
        if x1 < xx1:
            xx1 = x1
        if y1 < yy1:
            yy1 = y1
        if x2 > xx2:
            xx2 = x2
        if y2 > yy2:
            yy2 = y2

    img = cv2.resize(img[yy1:yy2, xx1:xx2], dsize=(O_WIDTH, O_HEIGHT))
    img = img.astype(np.float32) / 255
    img[img > 0.75] = 1
    img[img <= 0.75] = 0

    return img
Ejemplo n.º 8
0
    def visual_feedback(self, im):
        plt.rcParams['figure.figsize'] = (15.0, 10.0)
        
        
        # Crop image and get bounding boxes
        boxes = words.detection(im)
        
        if self.values['inverted colors feedback']:
            im[:,:,:] = 255-im[:,:,:]
        
        for box in boxes:
            cv2.rectangle(im,(box[0], box[1]),(box[2], box[3]),(0,255,0),3)
        
        '''
        for line in lines:
            if self.values['model'] == 'base_model':
                print(" ".join([self.recognise_char_sep_model(crop[y1:y2, x1:x2]) for (x1, y1, x2, y2) in line]))
            else:
                print(" ".join([self.recognise_ctc(crop[y1:y2, x1:x2]) for (x1, y1, x2, y2) in line]))
        '''

        
        return im
Ejemplo n.º 9
0
def textRecog(infilename):
    image = cv2.cvtColor(cv2.imread(infilename), cv2.COLOR_BGR2RGB)
    crop = page.detection(image)
    boxes = words.detection(crop)
    lines = words.sort_words(boxes)
    crop = cv2.cvtColor(crop, cv2.COLOR_RGB2GRAY)
    imLines = []
    for line in lines:
        imLine = []
        for (x1, y1, x2, y2) in line:
            imLine.append(crop[y1:y2, x1:x2])
        imLines.append(imLine)

    #decoderType = DecoderType.WordBeamSearch
    #decoderType = DecoderType.BeamSearch
    decoderType = DecoderType.BestPath

    model = Model(open('./model/charList.txt').read(),
                  decoderType,
                  mustRestore=True)
    #file1 = open("myfile.txt", "w")
    recognizedText = ""

    for line in imLines:
        imgs = []
        for word in line:
            imgs.append(preprocess(word, Model.imgSize))
        batch = Batch(None, imgs)
        (recognized, probability) = model.inferBatch(batch, True)

        l = ""
        for pw in recognized:
            l += pw
            l += ' '
        recognizedText += l
    return recognizedText
Ejemplo n.º 10
0
def fun(list1):
    import numpy as np
    import pandas as pd
    import matplotlib.pyplot as plt
    import tensorflow as tf
    import cv2
    import random
    # Import costume functions, corresponding to notebooks
    from ocr.normalization import imageNorm, letterNorm
    from ocr import page, words
    #from ocr import charSeg
    from ocr.helpers import implt, resize
    from ocr.tfhelpers import Graph
    from ocr.datahelpers import idx2char
    from src import gsmain
    import glob
    import os
    # ### Global Variables
    for x_file in list1:
        tf.reset_default_graph()
        str1 = ''.join(x_file)
        # Settings
        IMG = str1  # 1, 2, 3
        # ## Load image

        image = cv2.cvtColor(cv2.imread(IMG), cv2.COLOR_BGR2RGB)
        implt(image)

        # Crop image and get bounding boxes
        crop = page.detection(image)
        implt(crop)
        bBoxes = words.detection(crop)
        lines = words.sort_words(bBoxes)
        filelist = glob.glob("./new3/*.png")
        for file in filelist:
            os.remove(file)
        indeximg = 0
        nl = 0
        for line in lines:
            for (x1, y1, x2, y2) in line:
                cv2.imwrite("new3/" + str(indeximg) + ".png", crop[y1:y2,
                                                                   x1:x2])
                #implt(cv2.imread("outcheck.png"))
                indeximg = indeximg + 1
                #gsmain.FilePaths.fnInfer = ["outcheck.png"]
                #wordreco = gsmain.main()
                #file.write(wordreco + ' ')
            cv2.imwrite("new3/" + str(nl) + "space.png", crop[y1:y2, x1:x2])
            nl = nl + 1

        #Get all segmented words
        list2 = glob.glob("./new3/*.png")
        list2.sort(key=os.path.getmtime)
        '''list2=list()
        for ii in range(0,indeximg):
            list2.append("")'''
        #all files which have to be infer are loaded
        rand_num = random.randint(100, 1000)
        cv2.imwrite('final_output/' + str(rand_num) + '.jpg', image)
        for i in range(0, len(list2)):
            gsmain.FilePaths.fnInfer = gsmain.FilePaths.fnInfer + [list2[i]]
        gsmain.main('final_output/' + str(rand_num))
Ejemplo n.º 11
0
charClass = Graph(MODEL_LOC)

# ## Load image

# In[4]:

image = cv2.cvtColor(cv2.imread("test/%s.jpg" % IMG), cv2.COLOR_BGR2RGB)
implt(image)

# In[5]:

# Crop image and get bounding boxes
crop = page.detection(image)
implt(crop)
bBoxes = words.detection(crop)

# # Simple UI using widgets

# In[6]:


class Cycler:
    """ Cycle through the words and recognise them """
    height = 60

    def __init__(self, image, boxes):
        self.boxes = boxes  # Array of bounding boxes
        self.image = image  # Whole image

    def recognise(self, img):