File size: 3,574 Bytes
90f6245
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
import gradio as gr
import tensorflow as tf
import keras_ocr
import requests
import cv2
import os
import csv
import numpy as np
import pandas as pd
import huggingface_hub
from huggingface_hub import Repository
from datetime import datetime
import scipy.ndimage.interpolation as inter
import easyocr
import datasets
from datasets import load_dataset, Image
from PIL import Image
from paddleocr import PaddleOCR
from save_data import flag
import spaces
import pytesseract
from PIL import Image
import torch
 
"""
Paddle OCR
"""
@spaces.GPU
def ocr_with_paddle(img):
    finaltext = ''
    ocr = PaddleOCR(use_gpu=True,lang='en',use_angle_cls=True)
    # img_path = 'exp.jpeg'
    result = ocr.ocr(img)
    
    for i in range(len(result[0])):
        text = result[0][i][1][0]
        finaltext += ' '+ text
    return finaltext

    
"""
Keras OCR
"""
print("\n\n Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
@spaces.GPU
def ocr_with_keras(img):
    print("\n\n inside Num GPUs Available: ", len(tf.config.experimental.list_physical_devices('GPU')))
    output_text = ''
    pipeline=keras_ocr.pipeline.Pipeline()
    images=[keras_ocr.tools.read(img)]
    predictions=pipeline.recognize(images)
    first=predictions[0]
    for text,box in first:
        output_text += ' '+ text
    return output_text

"""
easy OCR
"""
# gray scale image
def get_grayscale(image):
    return cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)

# Thresholding or Binarization
def thresholding(src):
    return cv2.threshold(src,127,255, cv2.THRESH_TOZERO)[1]

@spaces.GPU
def ocr_with_easy(img):
    gray_scale_image=get_grayscale(img)
    thresholding(gray_scale_image)
    cv2.imwrite('image.png',gray_scale_image)
    reader = easyocr.Reader(['th','en'])
    bounds = reader.readtext('image.png',paragraph="False",detail = 0)
    bounds = ''.join(bounds)
    return bounds
        
"""
Generate OCR
"""
def generate_ocr(Method,img):
    
    text_output = ''
    if img.any() or (img).any():
        add_csv = []
        image_id = 1
        print("Method___________________",Method)
        if Method == 'EasyOCR':
            text_output = ocr_with_easy(img)
        if Method == 'KerasOCR':
            text_output = ocr_with_keras(img)
        if Method == 'PaddleOCR':
            text_output = ocr_with_paddle(img)
       
        try:
            flag(Method,text_output,img)
        except Exception as e:
            print(e)
        return text_output
    else:
        raise gr.Error("Please upload an image!!!!")
    
    # except Exception as e:
    #     print("Error in ocr generation ==>",e)
    #     text_output = "Something went wrong"
    # return text_output
    

"""
Create user interface for OCR demo
"""

# image = gr.Image(shape=(300, 300))
image = gr.Image()
method = gr.Radio(["PaddleOCR","EasyOCR", "KerasOCR"],value="PaddleOCR")
output = gr.Textbox(label="Output")

demo = gr.Interface(
    generate_ocr,
    [method,image],
    output,
    title="Optical Character Recognition",
    css=".gradio-container {background-color: lightgray} #radio_div {background-color: #FFD8B4; font-size: 40px;}",
    article = """<p style='text-align: center;'>Feel free to give us your thoughts on this demo and please contact us at 
                    <a href="mailto:[email protected]" target="_blank">[email protected]</a> 
                    <p style='text-align: center;'>Developed by: <a href="https://www.pragnakalp.com" target="_blank">Pragnakalp Techlabs</a></p>"""
    

)
# demo.launch(enable_queue = False)
demo.launch()