File size: 10,813 Bytes
5a99cc1
ad5ca2c
 
 
 
bfc7f18
25c9941
cd77918
bfc7f18
 
365522e
6a8bd53
 
 
15cd602
ad5ca2c
bfc7f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
5a99cc1
 
 
 
 
 
6a8bd53
ad5ca2c
 
bfc7f18
ad5ca2c
 
 
bfc7f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad5ca2c
bfc7f18
 
 
 
cd77918
f9cf3d0
d792040
f9cf3d0
bfc7f18
 
 
 
 
 
 
 
 
 
 
 
d792040
bfc7f18
 
 
f9cf3d0
cd77918
f9cf3d0
d792040
bfc7f18
 
 
 
 
 
 
 
 
25c9941
 
 
 
 
 
cd77918
 
 
 
 
 
 
 
 
 
 
25c9941
bfc7f18
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad5ca2c
 
d6482c4
 
 
 
 
 
 
 
 
365522e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ad5ca2c
6a8bd53
 
 
 
 
 
 
 
 
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
import streamlit as st
import pdf2image
import utils
import numpy as np
import cv2
import os
import io
from PIL import Image
import shutil

import time
# get https://github.com/oschwartz10612/poppler-windows/releases/tag/v22.01.0-0
# poppler-utils:
#   Installed: 22.02.0-2ubuntu0.4
# install https://github.com/UB-Mannheim/tesseract/wiki
#page extraction disabled

def is_new_pdf_upload(uploaded_file):
    if 'last_pdf_uploaded_file' in st.session_state:
        # Check if the newly uploaded file is different from the last one
        if (uploaded_file.name != st.session_state.last_pdf_uploaded_file['name'] or
                uploaded_file.size != st.session_state.last_pdf_uploaded_file['size']):
            st.session_state.last_pdf_uploaded_file = {'name': uploaded_file.name, 'size': uploaded_file.size}
            # st.write("A new src image file has been uploaded.")
            return True
        else:
            # st.write("The same src image file has been re-uploaded.")
            return False
    else:
        # st.write("This is the first file upload detected.")
        st.session_state.last_pdf_uploaded_file = {'name': uploaded_file.name, 'size': uploaded_file.size}
        return True
    # Store current file details in session state

big_text = """
    <div style='text-align: center;'>
        <h1 style='font-size: 30x;'>Locked PDF Ingestion</h1>
    </div>
    """
    # Display the styled text
st.markdown(big_text, unsafe_allow_html=True)



if 'is_initialized' not in st.session_state:
    pdf_path = 'uploaded_pdf/data_sheet.pdf'
    st.session_state['is_initialized'] = True
#     page_count = utils.get_pdf_page_count(pdf_path)
#     print("page_count=",page_count)
#     page_count=5
#     print("new page_count=",page_count)
#     read_pdf_progress_bar = st.progress(0)
#     st.session_state.color_image_list = []
#     st.session_state.gray_image_np_list = []
#     for page_number in range(page_count):
#          image = pdf2image.convert_from_path(pdf_path, first_page=page_number+1, last_page=page_number+1)
#          st.session_state.color_image_list.append(image[0])
#          progress_percentage = (page_number) / (page_count-1)
#          read_pdf_progress_bar.progress(progress_percentage)
#     gray_pdf_image_np_list = []
#     read_pdf_progress_bar.progress(0)
#     for index, image in enumerate(st.session_state.color_image_list):
#         image_np = np.array(image)
#         st.session_state.gray_image_np_list.append(cv2.cvtColor(np.array(image_np), cv2.COLOR_BGR2GRAY))
#         progress_percentage = (index) / (page_count - 1)
#         read_pdf_progress_bar.progress(progress_percentage)
#     # cv2.line(st.session_state.gray_image_np_list[37], (174, 227), (174, 1790), 0, 2)
#     # cv2.line(st.session_state.gray_image_np_list[37], (1550, 227), (1550, 1790), 0, 2)
#     # cv2.line(st.session_state.gray_image_np_list[38], (226,227),(226,1444), 0,3)
#     # cv2.line(st.session_state.gray_image_np_list[38], (1601,227),(1601,1444), 0,2)
#     st.session_state.img_index = 0
#     st.session_state.stop_button_clicked=False
# # st.image(st.session_state.gray_image_np_list[38])

uploaded_locked_pdf_file = st.file_uploader("Upload a locked pdf",
                                           type=['pdf'])
st.markdown(
    f'<a href="https://ikmtechnology.github.io/ikmtechnology/data_sheet.pdf" target="_blank">Sample 1 download and then upload to above</a>',
    unsafe_allow_html=True)

if uploaded_locked_pdf_file is not None:
    if is_new_pdf_upload(uploaded_locked_pdf_file):
        # To see details
        # file_details = {"FileName": uploaded_driving_video_file.name, "FileType": uploaded_driving_video_file.type, "FileSize": uploaded_driving_video_file.size}
        # st.write(file_details)
        save_path = './uploaded_videos'
        if not os.path.exists(save_path):
            os.makedirs(save_path)
        with open(os.path.join(save_path, uploaded_locked_pdf_file.name), "wb") as f:
            f.write(uploaded_locked_pdf_file.getbuffer())  # Write the file to the specified location
            st.success(f'Saved file temp_{uploaded_locked_pdf_file.name} in {save_path}')
            st.session_state.uploaded_pdf_path=os.path.join(save_path, uploaded_locked_pdf_file.name)
            st.session_state.page_count = utils.get_pdf_page_count(st.session_state.uploaded_pdf_path)
            print("page_count=",st.session_state.page_count)

        if 'extracted_text' in st.session_state:
            del st.session_state.extracted_text
        st.rerun()
if 'page_count' in st.session_state:
    st.write(f"total page count = {st.session_state.page_count}")
    if 'num_pages_to_extract'not in st.session_state:
        st.session_state.num_pages_to_extract = st.slider('Number of pages to extract:', min_value=1, max_value=st.session_state.page_count, value=5, key='num_pages_to_extract_slider')
    else:
        st.session_state.num_pages_to_extract = st.slider('Number of pages to extract:', min_value=1, max_value=st.session_state.page_count, value=st.session_state.num_pages_to_extract , key='num_pages_to_extract_slider')

    st.write(f"num of pages to extract {st.session_state.num_pages_to_extract}")
    if 'run_button' in st.session_state and st.session_state.run_button == True:
        st.session_state.running = True
    else:
        st.session_state.running = False
    read_pdf_progress_bar = st.progress(0)
    if st.button('Extract Pages', disabled=st.session_state.running, key='run_button'):

        st.session_state.color_image_list = []
        st.session_state.gray_image_np_list = []
        st.session_state.pdf_figures_image_list=[]
        pdf_tables_image_list=[]
        st.session_state.pdf_text_list=[]

        for page_number in range(st.session_state.num_pages_to_extract):
             image = pdf2image.convert_from_path(st.session_state.uploaded_pdf_path, first_page=page_number+1, last_page=page_number+1)
             st.session_state.color_image_list.append(image[0])
             progress_percentage = (page_number) / (st.session_state.num_pages_to_extract-1)
             read_pdf_progress_bar.progress(progress_percentage)
        read_pdf_progress_bar.progress(0)
        for index, image in enumerate(st.session_state.color_image_list):
            image_np = np.array(image)
            st.session_state.gray_image_np_list.append(cv2.cvtColor(np.array(image_np), cv2.COLOR_BGR2GRAY))
            progress_percentage = (index) / (st.session_state.num_pages_to_extract - 1)
            read_pdf_progress_bar.progress(progress_percentage)
        st.session_state.extracted_text = ""

        for index, gray_pdf_image_np in enumerate(st.session_state.gray_image_np_list):
          print("index="+str(index))

          figures_image_list,tables_image_list,text=utils.gray_pdf_image_np_to_text(index,gray_pdf_image_np, debug=True)
          st.session_state.pdf_figures_image_list.append(figures_image_list)
          pdf_tables_image_list.append(tables_image_list)
          st.session_state.pdf_text_list.append(text)
          st.session_state.extracted_text=st.session_state.extracted_text+f"<Page {index+1} start>\n" + text + f"\n<Page {index+1} end>\n>"
          # st.write(text)
          # print(text)
          progress_percentage = (index) / (st.session_state.num_pages_to_extract - 1)
          read_pdf_progress_bar.progress(progress_percentage)
        #add_animation_to_image()
        #st.session_state['video_generated'] = True
        st.rerun()
    if 'extracted_text' in st.session_state:
        string_buffer = io.StringIO(st.session_state.extracted_text)
        txt_file_path=uploaded_locked_pdf_file.name.replace(".pdf", ".txt")
        st.download_button(label="Download Extraction txt File",
                           data=string_buffer.getvalue(),
                           file_name=txt_file_path,
                           mime="text/plain")
        #  st.image(Image.fromarray(bgr_image))
        # for index,pdf_text in enumerate(st.session_state.pdf_text_list):
        for index, gray_pdf_image_np in enumerate(st.session_state.gray_image_np_list):
            st.write(f"Page {index+1} \n\n {st.session_state.pdf_text_list[index]}\n")
            if not st.session_state.pdf_figures_image_list[index]:
                st.write("no figures")
            else:
                for pdf_figure_text_image in st.session_state.pdf_figures_image_list[index]:
                    st.write(pdf_figure_text_image[0])
                    st.image(Image.fromarray(pdf_figure_text_image[1]))


# for index, gray_pdf_image_np in enumerate(st.session_state.gray_image_np_list[0:5], start=0):
#   print("index="+str(index))
#
#   text=utils.gray_pdf_image_np_to_text(index,gray_pdf_image_np, debug=True)
#   st.write(text)















#if 'img_index' not in st.session_state:

# if st.button("Stop"):
#     st.session_state.stop_button_clicked = True
# st.write(str(st.session_state.img_index+1) +"/" + str(len(st.session_state.color_image_list)))
# st.image(st.session_state.gray_image_np_list[st.session_state.img_index], use_column_width=True)
# if not st.session_state.stop_button_clicked:
#     if st.session_state.img_index < len(st.session_state.color_image_list) - 1:
#         st.session_state.img_index += 1
#         time.sleep(3)
#         st.rerun()
# col1, col2 = st.columns(2)
# with col1:
#     if st.button("Previous"):
#         print("Previous pressed")
#         # Decrease index, wrap around if it goes below 0
#         print("st.session_state.img_index =", str(st.session_state.img_index))
#         if st.session_state.img_index > 0:
#             print("case 1 before st.session_state.img_index =",str(st.session_state.img_index))
#             st.session_state.img_index -= 1
#             print("case 2 after  st.session_state.img_index =", str(st.session_state.img_index))
#         else:
#             print("case 2 st.session_state.img_index =", str(st.session_state.img_index))
#             st.session_state.img_index = len(st.session_state.color_image_list) - 1
# with col2:
#     if st.button("Next"):
#
#         print("Next pressed")
#         # Increase index, wrap around if it goes past the last image
#         if st.session_state.img_index < len(st.session_state.color_image_list) - 1:
#             st.session_state.img_index += 1
#
#         else:
#             st.session_state.img_index = 0
# #
# total_pages = 100
# print(f"total_pages = {total_pages}")
# st.write(f"total_pages = {total_pages}")
# for page_number in range(total_pages):
#     pdf_image_list = convert_from_path(pdf_path)
#     images = convert_from_path(pdf_path, first_page=page_number + 1, last_page=page_number + 1)
#     progress = (page_number + 1) / total_pages * 100
#     print(f"Progress: {progress:.2f}%")
# print("done")