File size: 1,952 Bytes
a538f68
eed78bd
 
0169439
eed78bd
 
 
1563c2d
a538f68
eed78bd
 
 
 
 
a538f68
eed78bd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a538f68
eed78bd
 
 
 
 
 
 
 
a538f68
eed78bd
a538f68
eed78bd
 
 
 
 
 
 
 
a538f68
b1d6d03
eed78bd
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
import gradio as gr
from paddleocr import PPStructure, save_structure_res
import cv2
import os
import numpy as np
import shutil
import uuid
from PIL import Image

# Initialize the PP-Structure table model once to avoid reloading for each request
table_engine = PPStructure(
    show_log=False,
    image_orientation=True,
    )

def extract_table(image):
    # Generate a unique ID for the session to avoid conflicts
    session_id = str(uuid.uuid4())
    save_folder = os.path.join('./output', session_id)
    os.makedirs(save_folder, exist_ok=True)
    
    # Convert PIL Image to OpenCV format
    image_cv = cv2.cvtColor(np.array(image), cv2.COLOR_RGB2BGR)
    
    # Perform table recognition on the image
    result = table_engine(image_cv)
    
    # Save the recognized table data
    img_name = 'input_image'  # You can customize this as needed
    save_structure_res(result, save_folder, img_name)
    
    # The save_structure_res function saves an Excel file with extension .xlsx
    excel_file = os.path.join(save_folder, img_name, 'excel', img_name + '.xlsx')
    if os.path.exists(excel_file):
        # Return the path to the Excel file for download
        return excel_file
    else:
        # Handle case where no table was detected
        return "No table detected in the image."

# Define the Gradio interface
iface = gr.Interface(
    fn=extract_table, 
    inputs=gr.Image(type="pil"), 
    outputs=gr.File(label="Extracted Excel File"),
    title="Table Data Extractor",
    description="""
    **Extract Table Data from Images**

    Upload an image of a financial statement (e.g., income statement, balance sheet) to extract the table data into an Excel file.

    **Instructions:**
    1. Click 'Browse' or drag and drop an image file.
    2. Wait for the processing to complete.
    3. Download the extracted Excel file.
    """,
    allow_flagging="never",
    examples=[],
)

if __name__ == "__main__":
    iface.launch()