File size: 7,774 Bytes
40d3327
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
"""
Attribution: https://github.com/AIPI540/AIPI540-Deep-Learning-Applications/

Jon Reifschneider
Brinnae Bent 

"""

import streamlit as st
from PIL import Image
import numpy as np
import os
import numpy as np
import pandas as pd
import pandas as pd
import os
import json
import pandas as pd
import torch
import numpy as np
import pandas as pd
import torch.nn as nn
import torch.nn.functional as F
import matplotlib.pyplot as plt
from ultralytics import YOLO
from PIL import Image, ImageDraw, ImageFont
import numpy as np
import cv2
import pytesseract
from PIL import ImageEnhance
import numpy as np
import os
import json
from transformers import GPT2LMHeadModel, GPT2Tokenizer, Trainer, TrainingArguments
from datasets import load_dataset
from transformers import DataCollatorForLanguageModeling
from PIL import Image, ImageEnhance
from io import StringIO


def crop_image(model, original_image):
    """
    Crop the region of interest (table) from an image using a YOLO model.

    Inputs:
        model (YOLO): The YOLO model used for object detection.
        original_image (PIL.image): The image to be processed.

    Returns:
        PIL.Image: The cropped image containing the detected table.
    """
    image_array = np.array(image)
    results = model(image_array)

    for r in results:
        boxes = r.boxes  
        
        for box in boxes:
            if box.cls == 3:  
                x1, y1, x2, y2 = box.xyxy[0]  
                x1, y1, x2, y2 = int(x1), int(y1), int(x2), int(y2)
                
                table_image = original_image.crop((x1, y1, x2, y2))
                
                return table_image
        return

def process_image(model, image):
    """
    Process the uploaded image with YOLO model and draw bounding boxes with class-specific colors.
    
    Inputs:
        model: The trained YOLO model
        image: The image file uploaded through Streamlit.

    Returns:
        PIL.Image: The processed image with bounding boxes and labels.
    """
    colors = {'title': (255, 0, 0),
            'text': (0, 255, 0),
            'figure': (0, 0, 255),
            'table': (255, 255, 0),
            'list': (0, 255, 255)}

    image_array = np.array(image)
    results = model(image_array)
    
    for result in results:
        boxes = result.boxes.cpu().numpy()
        for box in boxes:
            r = box.xyxy[0].astype(int)
            label = result.names[int(box.cls)]
            color = colors.get(label.lower(), (255, 255, 255))
            
            cv2.rectangle(image_array, r[:2], r[2:], color, 2)
            
            label_size, baseline = cv2.getTextSize(label, cv2.FONT_HERSHEY_SIMPLEX, 0.5, 1)
            top_left = (r[0], r[1] - label_size[1] - baseline)
            bottom_right = (r[0] + label_size[0], r[1])
            cv2.rectangle(image_array, top_left, bottom_right, color, cv2.FILLED)
            cv2.putText(image_array, label, (r[0], r[1] - baseline), 
                        cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 0), 1)
    
    return Image.fromarray(image_array)

def improve_ocr_accuracy(img):
    """
    Preprocess the image to improve OCR accuracy.

    This function resizes the image, increases contrast, and applies thresholding
    to enhance the image for better OCR results.

    Inputs:
        img (PIL.Image): The input image to be processed.

    Returns:
        numpy.ndarray: A binary thresholded image as a numpy array.
    """
    img = img.resize((img.width * 4, img.height * 4))
    
    enhancer = ImageEnhance.Contrast(img)
    img = enhancer.enhance(2)

    _, thresh = cv2.threshold(np.array(img), 127, 255, cv2.THRESH_BINARY_INV)
    
    return thresh

def ocr_core(image):
    """
    Perform OCR on the given image and process the extracted text.

    This function uses pytesseract to extract text from the image and then
    processes the extracted data to format it with appropriate line breaks
    and spacing.

    Inputs:
        image (numpy.ndarray): The preprocessed image as a numpy array.

    Returns:
        str: The extracted and formatted text from the image.
    """
    data = pytesseract.image_to_data(image, output_type=pytesseract.Output.DICT)
    df = pd.DataFrame(data)
    df = df[df['conf'] != -1]
    df['left_diff'] = df.groupby('block_num')['left'].diff().fillna(0).astype(int)
    df['prev_width'] = df['width'].shift(1).fillna(0).astype(int)
    df['spacing'] = (df['left_diff'] - df['prev_width']).fillna(0).astype(int)
    df['text'] = df.apply(lambda x: '\n' + x['text'] if (x['word_num'] == 1) & (x['block_num'] != 1) else x['text'], axis=1)
    df['text'] = df.apply(lambda x: ',' + x['text'] if x['spacing'] > 80 else x['text'], axis=1)
    ocr_text = ""
    for text in df['text']:
        ocr_text += text + ' '
    return ocr_text

def generate_csv_from_text(tokenizer, model, ocr_text):
    """
    Generate CSV text from OCR extracted text using the gpt model

    This function takes the OCR extracted text, processes it through a language model,
    and generates CSV formatted text.

    Inputs:
        tokenizer: The tokenizer for the gpt model
        model: The gpt model used for csv
        ocr_text (str): The text extracted from OCR

    Returns:
        str: The generated CSV formatted text.
    """
    inputs = tokenizer.encode(ocr_text, return_tensors='pt')
    outputs = model.generate(inputs, max_length=1000, num_return_sequences=1)
    csv_text = tokenizer.decode(outputs[0], skip_special_tokens=True)
    
    return csv_text

if __name__ == '__main__':
    # pytesseract.pytesseract.tesseract_cmd = r'C:/Program Files/Tesseract-OCR/tesseract.exe'  # Update this path for your system

    device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
  
    model = YOLO(os.getcwd() + '/models/trained_yolov8.pt')
    gpt_model = GPT2LMHeadModel.from_pretrained(os.getcwd() + '/models/gpt_model')
    tokenizer = GPT2Tokenizer.from_pretrained(os.getcwd() + '/models/gpt_model')    
    
    st.header('''
        Intelligent Document Processing: Table Extraction
    ''')
    
    header_img = Image.open('assets/header_img.png')
    st.image(header_img, use_column_width=True)
    
    st.subheader("Please upload an image of a scanned document with a table using the sidebar")
    
    with st.sidebar:
        user_image = st.file_uploader("Upload an image of a scanned document", type=["png", "jpg", "jpeg"])

    if user_image is not None:
        st.divider() 
        image = Image.open(user_image)
        st.image(image, caption='Uploaded Image', use_column_width=True)
        
        st.divider() 
        st.subheader("Document Classes:")
        processed_image = process_image(model, image)
        st.image(processed_image, caption='Processed Image', use_column_width=True)
        
        try:
            cropped_table = crop_image(model, image)
            st.divider() 
            st.subheader("Table Cropped Image:")
            st.image(cropped_table, caption='Cropped Table', use_column_width=True)

            improved_image = improve_ocr_accuracy(cropped_table)
            st.divider() 
            st.subheader("Improved Table Image:")
            st.image(improved_image, caption='Improved Table Image', use_column_width=True)
            
            ocr_text = ocr_core(improved_image)
            st.divider() 
            st.subheader("OCR Text:")
            st.write(ocr_text)
            
            csv_output = generate_csv_from_text(tokenizer,gpt_model,ocr_text)
            st.divider() 
            st.subheader("CSV Output:")
            st.write(csv_output.encode('utf-8'))
        except:
            st.divider() 
            st.subheader("Error:")
            st.write("Please upload a scanned document with a table")