File size: 1,512 Bytes
6bccbd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from dotenv import load_dotenv, find_dotenv
import easyocr

_ = load_dotenv(find_dotenv())

out_textbox = gr.TextArea(
             label="Output",
            info="Data extracted",
            lines=10,
            value="",
            interactive= True

        )


def process(filepath):
    reader = easyocr.Reader(['en'],gpu=True)
    result = reader.readtext(filepath, batch_size=3)

    latest_x = 0
    latest_y = 0
    content = ""
    x_to_charactor = 0
    for x,y,z in result:
        current_x = x[0][0]
        current_y = x[0][1]
        if x_to_charactor == 0:
            x_to_charactor = (x[1][0] - x[0][0])/(len(y))
        if current_y - latest_y > 10:
            content += f"""
"""  
            num_space = int((current_x - 0)/x_to_charactor)
            if num_space > 1 and latest_x != 0:
                for i in range(num_space):
                    content += f""" """
            content += f"""{y}"""  
        else:
            num_space = int((current_x - latest_x)/x_to_charactor)
            if num_space > 1 and latest_x != 0:
                for i in range(num_space):
                    content += f""" """
            content += f""" {y}"""
        latest_x = x[1][0]
        latest_y = current_y
    
    yield content

with gr.Blocks() as demo:
    gr.Interface(
        process,
        [gr.Image(type="filepath")],
        [out_textbox],
        flagging_options=[],
        examples=[]
    )

demo.launch(server_name="0.0.0.0", server_port=7860)