File size: 11,115 Bytes
071a451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dc14ebf
071a451
 
 
dc14ebf
071a451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
541ff5e
071a451
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
from PIL import Image, ImageFilter
import cv2
import pytesseract
from pytesseract import Output
from os import listdir, getcwd
from os.path import isfile, join
import numpy as np
import json
import matplotlib.pyplot as plt
from pdf2image import convert_from_path
from matplotlib import pyplot as plt
import re
import requests
import json

def getResponse(prompt) :
    url = "https://muryshev-mixtral-api.hf.space/completion"
    
    payload = json.dumps({
      "prompt": '[INST]' + prompt + '[/INST]'
    })
    
    headers = {
      'Content-Type': 'application/json'
    }
    
    response = requests.request("POST", url, headers = headers, data = payload)
    result = response.content.decode('utf-8')
    return result

def getOrgAddr(application) :
#     prefix = ''''Отвечайте всегда ТОЛЬКО НА РУССКОМ языке. Я предоставляю тебе "материал". Идентифицируй организацию и адрес организации в зависимости от указанного филиала.
# Используй такой формат: "Организация: *название*; Отделение/филиал: *название* если есть; Адрес: *адрес*;".
# Ты не комментируешь, не объясняешь, не выражаешь мысли, вообще ничего больше не говоришь.
# Материал: '''

    prefix = ''''Отвечайте всегда ТОЛЬКО НА РУССКОМ языке. Я предоставляю тебе "материал". Идентифицируй организацию и адрес организации в зависимости от указанного филиала.
Используй такой формат: "Организация: *название*; Адрес: *адрес*;".
Ты не комментируешь, не объясняешь, не выражаешь мысли, вообще ничего больше не говоришь.
Материал: '''
    
    prompt = prefix + application
    response = getResponse(prompt)

    s = response.strip()
    # x = s.find('Адрес:')
    # if x != -1 :
    #     y = s.find('(', x)
    #     if y != -1 :
    #         s = s[: y]
    l = response.split('\n')
    ll = []
    for s in l :
        s = s.strip()
        if ('Адрес:' in s or 'Организация:' in s) and s not in ll :
            ll.append(s)

    result = '\n'.join(ll)
    
    return result 

def processFiles(pdfs, verbose = False) :
    images_per_pdf_2d = [convert_from_path(file) for file in pdfs]
    
    images_per_pdf = []
    docfilenames = []
    pagenames = []
    fileindices = []
    for i in range(len(images_per_pdf_2d)) :
        docfilenames.append(pdfs[i][:-4])
        pageindices = []
        for j in range(len(images_per_pdf_2d[i])) :
            images_per_pdf.append(images_per_pdf_2d[i][j])
            pagenames.append(pdfs[i][:-4] + '_page_' + str(j))
            pageindices.append(len(pagenames) - 1)
            # print(i, j, len(pagenames) - 1, pagenames[-1])
    
        fileindices.append(pageindices)
    
    gray_images_per_pdf_cropped = []
    for i in range(len(images_per_pdf)) :
        image = images_per_pdf[i]
        crop = image.convert("L").crop((
                                    750, 150,      # left top point
                                    1654, 850       # right bottom point
                                    ))
        gray_images_per_pdf_cropped.append(crop)
    
    texts = [pytesseract.image_to_string(image, lang='rus') for image in gray_images_per_pdf_cropped]
    fulltexts = [pytesseract.image_to_string(image, lang='rus') for image in images_per_pdf]
    
    cropped_images = gray_images_per_pdf_cropped
    init_size = cropped_images[0].size
    thresh_imgs = [
                image.resize(
                    (init_size[0] //4, init_size[1] // 4)
                    ).point(
                        lambda x: 0 if x < 220 else 255
                        ).filter(
                            ImageFilter.MedianFilter(5)
                            ).filter(
                                ImageFilter.MinFilter(15) #15
                                )  for i,(name,image) in enumerate(zip(pagenames, cropped_images)) 
    ]
    
    masks = thresh_imgs
    masks_arr = [np.array(img) for img in masks]
    mask_shape = masks_arr[0].shape
    
    str_size = 7
    masks = []
    masks_bw = []
    for name, mask in zip(pagenames, masks_arr):
        cleaned_mask = mask.copy()
    
        for iter in range(mask_shape[0] // str_size):
            temp_mean = int(cleaned_mask[iter*str_size : iter*str_size + str_size, :].mean())
    
            if (temp_mean < 49) or (temp_mean > 160):
                cleaned_mask[iter*str_size : iter*str_size + str_size, :] = 255
    
        vertical_threshold = 200
    
        for i in range(mask_shape[1] // str_size + 1):
            if (i*str_size + str_size) > mask_shape[1]:
                temp_mean_vertical = int(cleaned_mask[:, i*str_size : mask_shape[1]].mean())
    
                if temp_mean_vertical > vertical_threshold:
                    cleaned_mask[:, i*str_size : mask_shape[1]] = 255
            else:
                temp_mean_vertical = int(cleaned_mask[:, i*str_size : i*str_size + str_size].mean())
    
                if temp_mean_vertical > vertical_threshold:
                    cleaned_mask[:, i*str_size : i*str_size + str_size] = 255
        
        image = Image.fromarray(cleaned_mask).filter(
                                        ImageFilter.MedianFilter(13)
                                    ).filter(
                                        ImageFilter.MinFilter(25) #15
                                    )
        masks.append(image)
        masks_bw.append(image.convert('1'))
    
    masks_bw_arr = [np.array(img) for img in masks_bw]
    
    # check which pages have address box: if there is no address box the mask is empty
    
    addressexists = [bool((~mask_bw).sum()) for mask_bw in masks_bw_arr]
    
    # this is a list of CB names that may be used in address
    
    CBnames = [
        'цб рф',
        'центральный банк',
        'центрального банка',
        'банк россии',
        'банка россии',
    ]
    
    # check which pages have address box addressed to CB
    
    toCB = []
    for i in range(len(addressexists)) :
        iftoCB = False
        for j in range(len(CBnames)) :
            if addressexists[i] and CBnames[j] in texts[i].lower() :
                iftoCB = True
                break
    
        toCB.append(iftoCB)
    
    # build 3-level list: file -> doc -> page
    
    docindices = []
    doctypes = []
    for i in range(len(fileindices)) :
        docs = []
        types = []
        pages = []
        doctype = False
        for j in range(len(fileindices[i])) :
            index = fileindices[i][j]
            ifaddress = addressexists[index]
            iftoCB = toCB[index]
            if ifaddress :
                if len(pages) > 0 :
                    docs.append(pages)
                    types.append(doctype)
                    
                pages = []
                doctype = iftoCB
            
            pages.append(index)
    
        docs.append(pages)
        types.append(doctype)
        docindices.append(docs)
        doctypes.append(types)
    
    cropped = cropped_images
    orig_size = cropped[0].size
    masks = [mask.convert('L').resize((orig_size)) for mask in masks]
    
    if verbose :
        for i in range(len(masks)) :
            img = np.array(masks[i])
            out = np.array(cropped[i])
            
            bw = cv2.inRange(img, 0, 12)
            contours, hierarchy = cv2.findContours(bw, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
            
            aaa = cv2.drawContours(out, contours, -1, (0, 255, 0), 5,  cv2.LINE_AA, hierarchy, 1)
        
            print()
            print(pagenames[i])
            print('Address exists :', addressexists[i])
            print('To CB :', toCB[i])
            # if addressflags[i] :
                
            #     if toCB[i] :
            #         print('text :', texts[i])
            plt.imshow(Image.fromarray(aaa))
            plt.show()
    
    # print recognized text with marks: file - > doc # and doc type -> page number and text
    
    docs_info = []
    for i in range(len(docindices)) :
        docs = []
        if verbose : 
            print('File =', docfilenames[i])
            
        for j in range(len(docindices[i])) :
            doc = {}
            doctype = 'Сопроводительное письмо'
            if doctypes[i][j] :
                doctype = 'Обращение'
    
            doc['Тип документа'] = doctype
            text = ''
            if verbose :
                print('Doc =', j, 'Type =', doctype)

            index = docindices[i][j][0]
            orginfo = ''
            if toCB[index] :
                orginfo = getOrgAddr(texts[index])

            doc['Атрибуты'] = orginfo
                
            for k in range(len(docindices[i][j])) :
                index = docindices[i][j][k]
                text += fulltexts[index]
                if verbose :
                    print('Page =', pagenames[index])
                    print(fulltexts[index])
                    print('--- end of page ---')
                    print()

            text = re.sub(r'\n +', r'\n', text)
            text = re.sub(r'\n+', r'\n', text)
            doc['Текст документа'] = text
            docs.append(doc)
    
        docs_info.append(docs)
    
    for i in range(len(docindices)) :
        for j in range(len(docindices[i])) :
            for k in range(len(docindices[i][j])) :
                index = docindices[i][j][k]
                if toCB[index] :
                    orginfo = getOrgAddr(texts[index])
                    # print(file)
                    print()
                    print(orginfo)
                    print()
                    # print(texts[index].strip())
                    # print()
                # if toCB[index] :
                #     if verbose :
                #         print('Page =', pagenames[index])
                #         print(texts[index].strip())
                #         print('------------------------')
                #         companie
                #         print()

    return docs_info

def processSingleFile(file) :
    return processFiles([file])[0]
    
# file = 'test.pdf'
# docs_info = processSingleFile(file) 

# docs_info =  
#  [ 
#    {
#      'Имя поля' : 'Текст поля',
#      ...
#    },
#    ...
#  ]
# то есть это массив документов, содержащихся в файле, для каждого документа задан словарь 'Имя поля' : 'Текст поля' (сейчас там 2 поля для каждого документа)