File size: 1,000 Bytes
fdf06c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import gradio as gr
from img2table.document import Image
from img2table.ocr import EasyOCR
import pandas as pd

# Initialize the OCR engine (this will download models on first run)
ocr = EasyOCR(lang=["en"])

def extract_table(input_img):
    # 1. Load the image from the path provided by Gradio
    doc = Image(input_img)
    
    # 2. Extract tables using EasyOCR as the text reader
    # 'borderless_tables=True' helps if the PDF/Image has no lines
    extracted_tables = doc.extract_tables(ocr=ocr, borderless_tables=True)
    
    if not extracted_tables:
        return "No tables detected."
    
    # 3. Convert the first detected table to a Pandas DataFrame
    df = extracted_tables[0].df
    return df

# Build a simple UI
interface = gr.Interface(
    fn=extract_table,
    inputs=gr.Image(type="filepath"),
    outputs=gr.Dataframe(),
    title="img2table Explorer",
    description="Upload a scanned image or screenshot of a table to convert it to a digital grid."
)

interface.launch()