pierreguillou commited on
Commit
08d0375
1 Parent(s): e2bf74f

Create functions.py

Browse files
Files changed (1) hide show
  1. files/functions.py +816 -0
files/functions.py ADDED
@@ -0,0 +1,816 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import os
2
+ import gradio as gr
3
+ import re
4
+ import string
5
+ import torch
6
+
7
+ from operator import itemgetter
8
+ import collections
9
+
10
+ import pypdf
11
+ from pypdf import PdfReader
12
+ from pypdf.errors import PdfReadError
13
+
14
+ import pdf2image
15
+ from pdf2image import convert_from_path
16
+ import langdetect
17
+ from langdetect import detect_langs
18
+
19
+ import pandas as pd
20
+ import numpy as np
21
+ import random
22
+ import tempfile
23
+ import itertools
24
+
25
+ from matplotlib import font_manager
26
+ from PIL import Image, ImageDraw, ImageFont
27
+ import cv2
28
+
29
+ # Tesseract
30
+ print(os.popen(f'cat /etc/debian_version').read())
31
+ print(os.popen(f'cat /etc/issue').read())
32
+ print(os.popen(f'apt search tesseract').read())
33
+ import pytesseract
34
+
35
+ ## Key parameters
36
+
37
+ # categories colors
38
+ label2color = {
39
+ 'Caption': 'brown',
40
+ 'Footnote': 'orange',
41
+ 'Formula': 'gray',
42
+ 'List-item': 'yellow',
43
+ 'Page-footer': 'red',
44
+ 'Page-header': 'red',
45
+ 'Picture': 'violet',
46
+ 'Section-header': 'orange',
47
+ 'Table': 'green',
48
+ 'Text': 'blue',
49
+ 'Title': 'pink'
50
+ }
51
+
52
+ # bounding boxes start and end of a sequence
53
+ cls_box = [0, 0, 0, 0]
54
+ sep_box = cls_box
55
+
56
+ # model
57
+ from transformers import AutoTokenizer, AutoModelForTokenClassification
58
+ device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
59
+
60
+ model_id = "pierreguillou/lilt-xlm-roberta-base-finetuned-with-DocLayNet-base-at-linelevel-ml384"
61
+
62
+ tokenizer = AutoTokenizer.from_pretrained(model_id)
63
+ model = AutoModelForTokenClassification.from_pretrained(model_id);
64
+ model.to(device);
65
+
66
+ # get labels
67
+ id2label = model.config.id2label
68
+ label2id = model.config.label2id
69
+ num_labels = len(id2label)
70
+
71
+ # (tokenization) The maximum length of a feature (sequence)
72
+ if str(384) in model_id:
73
+ max_length = 384
74
+ elif str(512) in model_id:
75
+ max_length = 512
76
+ else:
77
+ print("Error with max_length of chunks!")
78
+
79
+ # (tokenization) overlap
80
+ doc_stride = 128 # The authorized overlap between two part of the context when splitting it is needed.
81
+
82
+ # max PDF page images that will be displayed
83
+ max_imgboxes = 3
84
+ examples_dir = 'files/'
85
+ image_wo_content = examples_dir + "wo_content.png" # image without content
86
+ pdf_blank = examples_dir + "blank.pdf" # blank PDF
87
+ image_blank = examples_dir + "blank.png" # blank image
88
+
89
+ ## get langdetect2Tesseract dictionary
90
+ t = "files/languages_tesseract.csv"
91
+ l = "files/languages_iso.csv"
92
+
93
+ df_t = pd.read_csv(t)
94
+ df_l = pd.read_csv(l)
95
+
96
+ langs_t = df_t["Language"].to_list()
97
+ langs_t = [lang_t.lower().strip().translate(str.maketrans('', '', string.punctuation)) for lang_t in langs_t]
98
+ langs_l = df_l["Language"].to_list()
99
+ langs_l = [lang_l.lower().strip().translate(str.maketrans('', '', string.punctuation)) for lang_l in langs_l]
100
+ langscode_t = df_t["LangCode"].to_list()
101
+ langscode_l = df_l["LangCode"].to_list()
102
+
103
+ Tesseract2langdetect, langdetect2Tesseract = dict(), dict()
104
+ for lang_t, langcode_t in zip(langs_t,langscode_t):
105
+ try:
106
+ if lang_t == "Chinese - Simplified".lower().strip().translate(str.maketrans('', '', string.punctuation)): lang_t = "chinese"
107
+ index = langs_l.index(lang_t)
108
+ langcode_l = langscode_l[index]
109
+ Tesseract2langdetect[langcode_t] = langcode_l
110
+ except:
111
+ continue
112
+
113
+ langdetect2Tesseract = {v:k for k,v in Tesseract2langdetect.items()}
114
+
115
+ ## General
116
+
117
+ # get text and bounding boxes from an image
118
+ # https://stackoverflow.com/questions/61347755/how-can-i-get-line-coordinates-that-readed-by-tesseract
119
+ # https://medium.com/geekculture/tesseract-ocr-understanding-the-contents-of-documents-beyond-their-text-a98704b7c655
120
+ def get_data(results, factor, conf_min=0):
121
+
122
+ data = {}
123
+ for i in range(len(results['line_num'])):
124
+ level = results['level'][i]
125
+ block_num = results['block_num'][i]
126
+ par_num = results['par_num'][i]
127
+ line_num = results['line_num'][i]
128
+ top, left = results['top'][i], results['left'][i]
129
+ width, height = results['width'][i], results['height'][i]
130
+ conf = results['conf'][i]
131
+ text = results['text'][i]
132
+ if not (text == '' or text.isspace()):
133
+ if conf >= conf_min:
134
+ tup = (text, left, top, width, height)
135
+ if block_num in list(data.keys()):
136
+ if par_num in list(data[block_num].keys()):
137
+ if line_num in list(data[block_num][par_num].keys()):
138
+ data[block_num][par_num][line_num].append(tup)
139
+ else:
140
+ data[block_num][par_num][line_num] = [tup]
141
+ else:
142
+ data[block_num][par_num] = {}
143
+ data[block_num][par_num][line_num] = [tup]
144
+ else:
145
+ data[block_num] = {}
146
+ data[block_num][par_num] = {}
147
+ data[block_num][par_num][line_num] = [tup]
148
+
149
+ # get paragraphs dicionnary with list of lines
150
+ par_data = {}
151
+ par_idx = 1
152
+ for _, b in data.items():
153
+ for _, p in b.items():
154
+ line_data = {}
155
+ line_idx = 1
156
+ for _, l in p.items():
157
+ line_data[line_idx] = l
158
+ line_idx += 1
159
+ par_data[par_idx] = line_data
160
+ par_idx += 1
161
+
162
+ # get lines of texts, grouped by paragraph
163
+ lines = list()
164
+ row_indexes = list()
165
+ row_index = 0
166
+ for _,par in par_data.items():
167
+ count_lines = 0
168
+ for _,line in par.items():
169
+ if count_lines == 0: row_indexes.append(row_index)
170
+ line_text = ' '.join([item[0] for item in line])
171
+ lines.append(line_text)
172
+ count_lines += 1
173
+ row_index += 1
174
+ # lines.append("\n")
175
+ row_index += 1
176
+ # lines = lines[:-1]
177
+
178
+ # get paragraphes boxes (par_boxes)
179
+ # get lines boxes (line_boxes)
180
+ par_boxes = list()
181
+ par_idx = 1
182
+ line_boxes = list()
183
+ line_idx = 1
184
+ for _, par in par_data.items():
185
+ xmins, ymins, xmaxs, ymaxs = list(), list(), list(), list()
186
+ for _, line in par.items():
187
+ xmin, ymin = line[0][1], line[0][2]
188
+ xmax, ymax = (line[-1][1] + line[-1][3]), (line[-1][2] + line[-1][4])
189
+ line_boxes.append([int(xmin/factor), int(ymin/factor), int(xmax/factor), int(ymax/factor)])
190
+ xmins.append(xmin)
191
+ ymins.append(ymin)
192
+ xmaxs.append(xmax)
193
+ ymaxs.append(ymax)
194
+ line_idx += 1
195
+ xmin, ymin, xmax, ymax = min(xmins), min(ymins), max(xmaxs), max(ymaxs)
196
+ par_boxes.append([int(xmin/factor), int(ymin/factor), int(xmax/factor), int(ymax/factor)])
197
+ par_idx += 1
198
+
199
+ return lines, row_indexes, par_boxes, line_boxes #data, par_data #
200
+
201
+ # rescale image to get 300dpi
202
+ def set_image_dpi_resize(image):
203
+ """
204
+ Rescaling image to 300dpi while resizing
205
+ :param image: An image
206
+ :return: A rescaled image
207
+ """
208
+ length_x, width_y = image.size
209
+ factor = min(1, float(1024.0 / length_x))
210
+ size = int(factor * length_x), int(factor * width_y)
211
+ image_resize = image.resize(size, Image.Resampling.LANCZOS)
212
+ temp_file = tempfile.NamedTemporaryFile(delete=False, suffix='1.png')
213
+ temp_filename = temp_file.name
214
+ image_resize.save(temp_filename, dpi=(300, 300))
215
+ return factor, temp_filename
216
+
217
+ # it is important that each bounding box should be in (upper left, lower right) format.
218
+ # source: https://github.com/NielsRogge/Transformers-Tutorials/issues/129
219
+ def upperleft_to_lowerright(bbox):
220
+ x0, y0, x1, y1 = tuple(bbox)
221
+ if bbox[2] < bbox[0]:
222
+ x0 = bbox[2]
223
+ x1 = bbox[0]
224
+ if bbox[3] < bbox[1]:
225
+ y0 = bbox[3]
226
+ y1 = bbox[1]
227
+ return [x0, y0, x1, y1]
228
+
229
+ # convert boundings boxes (left, top, width, height) format to (left, top, left+widght, top+height) format.
230
+ def convert_box(bbox):
231
+ x, y, w, h = tuple(bbox) # the row comes in (left, top, width, height) format
232
+ return [x, y, x+w, y+h] # we turn it into (left, top, left+widght, top+height) to get the actual box
233
+
234
+ # LiLT model gets 1000x10000 pixels images
235
+ def normalize_box(bbox, width, height):
236
+ return [
237
+ int(1000 * (bbox[0] / width)),
238
+ int(1000 * (bbox[1] / height)),
239
+ int(1000 * (bbox[2] / width)),
240
+ int(1000 * (bbox[3] / height)),
241
+ ]
242
+
243
+ # LiLT model gets 1000x10000 pixels images
244
+ def denormalize_box(bbox, width, height):
245
+ return [
246
+ int(width * (bbox[0] / 1000)),
247
+ int(height * (bbox[1] / 1000)),
248
+ int(width* (bbox[2] / 1000)),
249
+ int(height * (bbox[3] / 1000)),
250
+ ]
251
+
252
+ # get back original size
253
+ def original_box(box, original_width, original_height, coco_width, coco_height):
254
+ return [
255
+ int(original_width * (box[0] / coco_width)),
256
+ int(original_height * (box[1] / coco_height)),
257
+ int(original_width * (box[2] / coco_width)),
258
+ int(original_height* (box[3] / coco_height)),
259
+ ]
260
+
261
+ def get_blocks(bboxes_block, categories, texts):
262
+ # get list of unique block boxes
263
+ bbox_block_dict, bboxes_block_list, bbox_block_prec = dict(), list(), list()
264
+ for count_block, bbox_block in enumerate(bboxes_block):
265
+ if bbox_block != bbox_block_prec:
266
+ bbox_block_indexes = [i for i, bbox in enumerate(bboxes_block) if bbox == bbox_block]
267
+ bbox_block_dict[count_block] = bbox_block_indexes
268
+ bboxes_block_list.append(bbox_block)
269
+ bbox_block_prec = bbox_block
270
+
271
+ # get list of categories and texts by unique block boxes
272
+ category_block_list, text_block_list = list(), list()
273
+ for bbox_block in bboxes_block_list:
274
+ count_block = bboxes_block.index(bbox_block)
275
+ bbox_block_indexes = bbox_block_dict[count_block]
276
+ category_block = np.array(categories, dtype=object)[bbox_block_indexes].tolist()[0]
277
+ category_block_list.append(category_block)
278
+ text_block = np.array(texts, dtype=object)[bbox_block_indexes].tolist()
279
+ text_block = [text.replace("\n","").strip() for text in text_block]
280
+ if id2label[category_block] == "Text" or id2label[category_block] == "Caption" or id2label[category_block] == "Footnote":
281
+ text_block = ' '.join(text_block)
282
+ else:
283
+ text_block = '\n'.join(text_block)
284
+ text_block_list.append(text_block)
285
+
286
+ return bboxes_block_list, category_block_list, text_block_list
287
+
288
+ # function to sort bounding boxes
289
+ def get_sorted_boxes(bboxes):
290
+
291
+ # sort by y from page top to bottom
292
+ sorted_bboxes = sorted(bboxes, key=itemgetter(1), reverse=False)
293
+ y_list = [bbox[1] for bbox in sorted_bboxes]
294
+
295
+ # sort by x from page left to right when boxes with same y
296
+ if len(list(set(y_list))) != len(y_list):
297
+ y_list_duplicates_indexes = dict()
298
+ y_list_duplicates = [item for item, count in collections.Counter(y_list).items() if count > 1]
299
+ for item in y_list_duplicates:
300
+ y_list_duplicates_indexes[item] = [i for i, e in enumerate(y_list) if e == item]
301
+ bbox_list_y_duplicates = sorted(np.array(sorted_bboxes, dtype=object)[y_list_duplicates_indexes[item]].tolist(), key=itemgetter(0), reverse=False)
302
+ np_array_bboxes = np.array(sorted_bboxes)
303
+ np_array_bboxes[y_list_duplicates_indexes[item]] = np.array(bbox_list_y_duplicates)
304
+ sorted_bboxes = np_array_bboxes.tolist()
305
+
306
+ return sorted_bboxes
307
+
308
+ # sort data from y = 0 to end of page (and after, x=0 to end of page when necessary)
309
+ def sort_data(bboxes, categories, texts):
310
+
311
+ sorted_bboxes = get_sorted_boxes(bboxes)
312
+ sorted_bboxes_indexes = [bboxes.index(bbox) for bbox in sorted_bboxes]
313
+ sorted_categories = np.array(categories, dtype=object)[sorted_bboxes_indexes].tolist()
314
+ sorted_texts = np.array(texts, dtype=object)[sorted_bboxes_indexes].tolist()
315
+
316
+ return sorted_bboxes, sorted_categories, sorted_texts
317
+
318
+ # sort data from y = 0 to end of page (and after, x=0 to end of page when necessary)
319
+ def sort_data_wo_labels(bboxes, texts):
320
+
321
+ sorted_bboxes = get_sorted_boxes(bboxes)
322
+ sorted_bboxes_indexes = [bboxes.index(bbox) for bbox in sorted_bboxes]
323
+ sorted_texts = np.array(texts, dtype=object)[sorted_bboxes_indexes].tolist()
324
+
325
+ return sorted_bboxes, sorted_texts
326
+
327
+ ## PDF processing
328
+
329
+ # get filename and images of PDF pages
330
+ def pdf_to_images(uploaded_pdf):
331
+
332
+ # Check if None object
333
+ if uploaded_pdf is None:
334
+ path_to_file = pdf_blank
335
+ filename = path_to_file.replace(examples_dir,"")
336
+ msg = "Invalid PDF file."
337
+ images = [Image.open(image_blank)]
338
+ else:
339
+ # path to the uploaded PDF
340
+ path_to_file = uploaded_pdf.name
341
+ filename = path_to_file.replace("/tmp/","")
342
+
343
+ try:
344
+ PdfReader(path_to_file)
345
+ except PdfReadError:
346
+ path_to_file = pdf_blank
347
+ filename = path_to_file.replace(examples_dir,"")
348
+ msg = "invalid PDF file."
349
+ images = [Image.open(image_blank)]
350
+ else:
351
+ try:
352
+ images = convert_from_path(path_to_file, last_page=max_imgboxes)
353
+ num_imgs = len(images)
354
+ msg = f'The PDF "{filename}" was converted into {num_imgs} images.'
355
+ except:
356
+ msg = f'Error with the PDF "{filename}": it was not converted into images.'
357
+ images = [Image.open(image_wo_content)]
358
+
359
+ return filename, msg, images
360
+
361
+ # Extraction of image data (text and bounding boxes)
362
+ def extraction_data_from_image(images):
363
+
364
+ num_imgs = len(images)
365
+
366
+ if num_imgs > 0:
367
+
368
+ # https://pyimagesearch.com/2021/11/15/tesseract-page-segmentation-modes-psms-explained-how-to-improve-your-ocr-accuracy/
369
+ custom_config = r'--oem 3 --psm 3 -l eng+por+spa' # default config PyTesseract: --oem 3 --psm 3 -l eng+deu+fra+jpn+por+spa+rus+hin+chi_sim
370
+ # custom_config = r'--oem 3 --psm 3 -l eng' # default config PyTesseract: --oem 3 --psm 3
371
+ results, lines, row_indexes, par_boxes, line_boxes = dict(), dict(), dict(), dict(), dict()
372
+ images_ids_list, lines_list, par_boxes_list, line_boxes_list, images_list, page_no_list, num_pages_list = list(), list(), list(), list(), list(), list(), list()
373
+
374
+ try:
375
+ for i,image in enumerate(images):
376
+ # image preprocessing
377
+ # https://docs.opencv.org/3.0-beta/doc/py_tutorials/py_imgproc/py_thresholding/py_thresholding.html
378
+ img = image.copy()
379
+ factor, path_to_img = set_image_dpi_resize(img) # Rescaling to 300dpi
380
+ img = Image.open(path_to_img)
381
+ img = np.array(img, dtype='uint8') # convert PIL to cv2
382
+ img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) # gray scale image
383
+ ret,img = cv2.threshold(img,127,255,cv2.THRESH_BINARY)
384
+ # img_filepath = f"img{i}.png"
385
+ # img.save(img_filepath)
386
+ # cv2.imwrite(img_filepath, img)
387
+
388
+ # OCR PyTesseract | get langs of page
389
+ txt = pytesseract.image_to_string(img, config=custom_config)
390
+
391
+ # txt = os.popen(f'tesseract {img_filepath} - {custom_config}').read()
392
+
393
+ try:
394
+ langs = detect_langs(txt)
395
+ langs = [langdetect2Tesseract[langs[i].lang] for i in range(len(langs))]
396
+ langs_string = '+'.join(langs)
397
+ except:
398
+ langs_string = "eng"
399
+ langs_string += '+osd'
400
+ custom_config = f'--oem 3 --psm 3 -l {langs_string} tsv' # default config PyTesseract: --oem 3 --psm 3
401
+ # print("langs", i, "-", langs_string)
402
+
403
+ # OCR PyTesseract | get data
404
+ results[i] = pytesseract.image_to_data(img, config=custom_config, output_type=pytesseract.Output.DICT)
405
+
406
+ # results[i] = os.popen(f'tesseract {img_filepath} - {custom_config}').read()
407
+ # print("results[i].keys()", i, "-",results[i].keys())
408
+
409
+ # print("factor", factor)
410
+ lines[i], row_indexes[i], par_boxes[i], line_boxes[i] = get_data(results[i], factor, conf_min=0)
411
+ lines_list.append(lines[i])
412
+ par_boxes_list.append(par_boxes[i])
413
+ line_boxes_list.append(line_boxes[i])
414
+ images_ids_list.append(i)
415
+ images_list.append(images[i])
416
+ page_no_list.append(i)
417
+ num_pages_list.append(num_imgs)
418
+ # print("i - lines[i], row_indexes[i], par_boxes[i], line_boxes[i]",i,"-",lines[i], row_indexes[i], par_boxes[i], line_boxes[i])
419
+ # print("***************************************************************")
420
+
421
+ except:
422
+ print(f"There was an error within the extraction of PDF text by the OCR!")
423
+ else:
424
+ from datasets import Dataset
425
+ dataset = Dataset.from_dict({"images_ids": images_ids_list, "images": images_list, "page_no": page_no_list, "num_pages": num_pages_list, "texts": lines_list, "bboxes_line": line_boxes_list})
426
+
427
+ print(f"The text data was successfully extracted by the OCR!")
428
+
429
+ return dataset, lines, row_indexes, par_boxes, line_boxes
430
+
431
+ ## Inference
432
+
433
+ # def prepare_inference_features(example, cls_box=cls_box, sep_box=sep_box):
434
+ def prepare_inference_features(example):
435
+
436
+ images_ids_list, chunks_ids_list, input_ids_list, attention_mask_list, bb_list = list(), list(), list(), list(), list()
437
+
438
+ # get batch
439
+ # batch_page_hash = example["page_hash"]
440
+ batch_images_ids = example["images_ids"]
441
+ batch_images = example["images"]
442
+ batch_bboxes_line = example["bboxes_line"]
443
+ batch_texts = example["texts"]
444
+ batch_images_size = [image.size for image in batch_images]
445
+
446
+ batch_width, batch_height = [image_size[0] for image_size in batch_images_size], [image_size[1] for image_size in batch_images_size]
447
+
448
+ # add a dimension if not a batch but only one image
449
+ if not isinstance(batch_images_ids, list):
450
+ batch_images_ids = [batch_images_ids]
451
+ batch_images = [batch_images]
452
+ batch_bboxes_line = [batch_bboxes_line]
453
+ batch_texts = [batch_texts]
454
+ batch_width, batch_height = [batch_width], [batch_height]
455
+
456
+ # process all images of the batch
457
+ for num_batch, (image_id, boxes, texts, width, height) in enumerate(zip(batch_images_ids, batch_bboxes_line, batch_texts, batch_width, batch_height)):
458
+ tokens_list = []
459
+ bboxes_list = []
460
+
461
+ # add a dimension if only on image
462
+ if not isinstance(texts, list):
463
+ texts, boxes = [texts], [boxes]
464
+
465
+ # convert boxes to original
466
+ normalize_bboxes_line = [normalize_box(upperleft_to_lowerright(box), width, height) for box in boxes]
467
+
468
+ # sort boxes with texts
469
+ # we want sorted lists from top to bottom of the image
470
+ boxes, texts = sort_data_wo_labels(normalize_bboxes_line, texts)
471
+
472
+ count = 0
473
+ for box, text in zip(boxes, texts):
474
+ tokens = tokenizer.tokenize(text)
475
+ num_tokens = len(tokens) # get number of tokens
476
+ tokens_list.extend(tokens)
477
+
478
+ bboxes_list.extend([box] * num_tokens) # number of boxes must be the same as the number of tokens
479
+
480
+ # use of return_overflowing_tokens=True / stride=doc_stride
481
+ # to get parts of image with overlap
482
+ # source: https://huggingface.co/course/chapter6/3b?fw=tf#handling-long-contexts
483
+ encodings = tokenizer(" ".join(texts),
484
+ truncation=True,
485
+ padding="max_length",
486
+ max_length=max_length,
487
+ stride=doc_stride,
488
+ return_overflowing_tokens=True,
489
+ return_offsets_mapping=True
490
+ )
491
+
492
+ otsm = encodings.pop("overflow_to_sample_mapping")
493
+ offset_mapping = encodings.pop("offset_mapping")
494
+
495
+ # Let's label those examples and get their boxes
496
+ sequence_length_prev = 0
497
+ for i, offsets in enumerate(offset_mapping):
498
+ # truncate tokens, boxes and labels based on length of chunk - 2 (special tokens <s> and </s>)
499
+ sequence_length = len(encodings.input_ids[i]) - 2
500
+ if i == 0: start = 0
501
+ else: start += sequence_length_prev - doc_stride
502
+ end = start + sequence_length
503
+ sequence_length_prev = sequence_length
504
+
505
+ # get tokens, boxes and labels of this image chunk
506
+ bb = [cls_box] + bboxes_list[start:end] + [sep_box]
507
+
508
+ # as the last chunk can have a length < max_length
509
+ # we must to add [tokenizer.pad_token] (tokens), [sep_box] (boxes) and [-100] (labels)
510
+ if len(bb) < max_length:
511
+ bb = bb + [sep_box] * (max_length - len(bb))
512
+
513
+ # append results
514
+ input_ids_list.append(encodings["input_ids"][i])
515
+ attention_mask_list.append(encodings["attention_mask"][i])
516
+ bb_list.append(bb)
517
+ images_ids_list.append(image_id)
518
+ chunks_ids_list.append(i)
519
+
520
+ return {
521
+ "images_ids": images_ids_list,
522
+ "chunk_ids": chunks_ids_list,
523
+ "input_ids": input_ids_list,
524
+ "attention_mask": attention_mask_list,
525
+ "normalized_bboxes": bb_list,
526
+ }
527
+
528
+ from torch.utils.data import Dataset
529
+
530
+ class CustomDataset(Dataset):
531
+ def __init__(self, dataset, tokenizer):
532
+ self.dataset = dataset
533
+ self.tokenizer = tokenizer
534
+
535
+ def __len__(self):
536
+ return len(self.dataset)
537
+
538
+ def __getitem__(self, idx):
539
+ # get item
540
+ example = self.dataset[idx]
541
+ encoding = dict()
542
+ encoding["images_ids"] = example["images_ids"]
543
+ encoding["chunk_ids"] = example["chunk_ids"]
544
+ encoding["input_ids"] = example["input_ids"]
545
+ encoding["attention_mask"] = example["attention_mask"]
546
+ encoding["bbox"] = example["normalized_bboxes"]
547
+ # encoding["labels"] = example["labels"]
548
+
549
+ return encoding
550
+
551
+ import torch.nn.functional as F
552
+
553
+ # get predictions at token level
554
+ def predictions_token_level(images, custom_encoded_dataset):
555
+
556
+ num_imgs = len(images)
557
+ if num_imgs > 0:
558
+
559
+ chunk_ids, input_ids, bboxes, outputs, token_predictions = dict(), dict(), dict(), dict(), dict()
560
+ images_ids_list = list()
561
+
562
+ for i,encoding in enumerate(custom_encoded_dataset):
563
+
564
+ # get custom encoded data
565
+ image_id = encoding['images_ids']
566
+ chunk_id = encoding['chunk_ids']
567
+ input_id = torch.tensor(encoding['input_ids'])[None]
568
+ attention_mask = torch.tensor(encoding['attention_mask'])[None]
569
+ bbox = torch.tensor(encoding['bbox'])[None]
570
+
571
+ # save data in dictionnaries
572
+ if image_id not in images_ids_list: images_ids_list.append(image_id)
573
+
574
+ if image_id in chunk_ids: chunk_ids[image_id].append(chunk_id)
575
+ else: chunk_ids[image_id] = [chunk_id]
576
+
577
+ if image_id in input_ids: input_ids[image_id].append(input_id)
578
+ else: input_ids[image_id] = [input_id]
579
+
580
+ if image_id in bboxes: bboxes[image_id].append(bbox)
581
+ else: bboxes[image_id] = [bbox]
582
+
583
+ # get prediction with forward pass
584
+ with torch.no_grad():
585
+ output = model(
586
+ input_ids=input_id,
587
+ attention_mask=attention_mask,
588
+ bbox=bbox
589
+ )
590
+
591
+ # save probabilities of predictions in dictionnary
592
+ if image_id in outputs: outputs[image_id].append(F.softmax(output.logits.squeeze(), dim=-1))
593
+ else: outputs[image_id] = [F.softmax(output.logits.squeeze(), dim=-1)]
594
+
595
+ return outputs, images_ids_list, chunk_ids, input_ids, bboxes
596
+
597
+ else:
598
+ print("An error occurred while getting predictions!")
599
+
600
+ from functools import reduce
601
+
602
+ # Get predictions (line level)
603
+ def predictions_line_level(dataset, outputs, images_ids_list, chunk_ids, input_ids, bboxes):
604
+
605
+ ten_probs_dict, ten_input_ids_dict, ten_bboxes_dict = dict(), dict(), dict()
606
+ bboxes_list_dict, input_ids_dict_dict, probs_dict_dict, df = dict(), dict(), dict(), dict()
607
+
608
+ if len(images_ids_list) > 0:
609
+
610
+ for i, image_id in enumerate(images_ids_list):
611
+
612
+ # get image information
613
+ images_list = dataset.filter(lambda example: example["images_ids"] == image_id)["images"]
614
+ image = images_list[0]
615
+ width, height = image.size
616
+
617
+ # get data
618
+ chunk_ids_list = chunk_ids[image_id]
619
+ outputs_list = outputs[image_id]
620
+ input_ids_list = input_ids[image_id]
621
+ bboxes_list = bboxes[image_id]
622
+
623
+ # create zeros tensors
624
+ ten_probs = torch.zeros((outputs_list[0].shape[0] - 2)*len(outputs_list), outputs_list[0].shape[1])
625
+ ten_input_ids = torch.ones(size=(1, (outputs_list[0].shape[0] - 2)*len(outputs_list)), dtype =int)
626
+ ten_bboxes = torch.zeros(size=(1, (outputs_list[0].shape[0] - 2)*len(outputs_list), 4), dtype =int)
627
+
628
+ if len(outputs_list) > 1:
629
+
630
+ for num_output, (output, input_id, bbox) in enumerate(zip(outputs_list, input_ids_list, bboxes_list)):
631
+ start = num_output*(max_length - 2) - max(0,num_output)*doc_stride
632
+ end = start + (max_length - 2)
633
+
634
+ if num_output == 0:
635
+ ten_probs[start:end,:] += output[1:-1]
636
+ ten_input_ids[:,start:end] = input_id[:,1:-1]
637
+ ten_bboxes[:,start:end,:] = bbox[:,1:-1,:]
638
+ else:
639
+ ten_probs[start:start + doc_stride,:] += output[1:1 + doc_stride]
640
+ ten_probs[start:start + doc_stride,:] = ten_probs[start:start + doc_stride,:] * 0.5
641
+ ten_probs[start + doc_stride:end,:] += output[1 + doc_stride:-1]
642
+
643
+ ten_input_ids[:,start:start + doc_stride] = input_id[:,1:1 + doc_stride]
644
+ ten_input_ids[:,start + doc_stride:end] = input_id[:,1 + doc_stride:-1]
645
+
646
+ ten_bboxes[:,start:start + doc_stride,:] = bbox[:,1:1 + doc_stride,:]
647
+ ten_bboxes[:,start + doc_stride:end,:] = bbox[:,1 + doc_stride:-1,:]
648
+
649
+ else:
650
+ ten_probs += outputs_list[0][1:-1]
651
+ ten_input_ids = input_ids_list[0][:,1:-1]
652
+ ten_bboxes = bboxes_list[0][:,1:-1]
653
+
654
+ ten_probs_list, ten_input_ids_list, ten_bboxes_list = ten_probs.tolist(), ten_input_ids.tolist()[0], ten_bboxes.tolist()[0]
655
+ bboxes_list = list()
656
+ input_ids_dict, probs_dict = dict(), dict()
657
+ bbox_prev = [-100, -100, -100, -100]
658
+ for probs, input_id, bbox in zip(ten_probs_list, ten_input_ids_list, ten_bboxes_list):
659
+ bbox = denormalize_box(bbox, width, height)
660
+ if bbox != bbox_prev and bbox != cls_box:
661
+ bboxes_list.append(bbox)
662
+ input_ids_dict[str(bbox)] = [input_id]
663
+ probs_dict[str(bbox)] = [probs]
664
+ else:
665
+ if bbox != cls_box:
666
+ input_ids_dict[str(bbox)].append(input_id)
667
+ probs_dict[str(bbox)].append(probs)
668
+ bbox_prev = bbox
669
+
670
+ probs_bbox = dict()
671
+ for i,bbox in enumerate(bboxes_list):
672
+ probs = probs_dict[str(bbox)]
673
+ probs = np.array(probs).T.tolist()
674
+
675
+ probs_label = list()
676
+ for probs_list in probs:
677
+ prob_label = reduce(lambda x, y: x*y, probs_list)
678
+ probs_label.append(prob_label)
679
+ max_value = max(probs_label)
680
+ max_index = probs_label.index(max_value)
681
+ probs_bbox[str(bbox)] = max_index
682
+
683
+ bboxes_list_dict[image_id] = bboxes_list
684
+ input_ids_dict_dict[image_id] = input_ids_dict
685
+ probs_dict_dict[image_id] = probs_bbox
686
+
687
+ df[image_id] = pd.DataFrame()
688
+ df[image_id]["bboxes"] = bboxes_list
689
+ df[image_id]["texts"] = [tokenizer.decode(input_ids_dict[str(bbox)]) for bbox in bboxes_list]
690
+ df[image_id]["labels"] = [id2label[probs_bbox[str(bbox)]] for bbox in bboxes_list]
691
+
692
+ return probs_bbox, bboxes_list_dict, input_ids_dict_dict, probs_dict_dict, df
693
+
694
+ else:
695
+ print("An error occurred while getting predictions!")
696
+
697
+ # Get labeled images with lines bounding boxes
698
+ def get_labeled_images(dataset, images_ids_list, bboxes_list_dict, probs_dict_dict):
699
+
700
+ labeled_images = list()
701
+
702
+ for i, image_id in enumerate(images_ids_list):
703
+
704
+ # get image
705
+ images_list = dataset.filter(lambda example: example["images_ids"] == image_id)["images"]
706
+ image = images_list[0]
707
+ width, height = image.size
708
+
709
+ # get predicted boxes and labels
710
+ bboxes_list = bboxes_list_dict[image_id]
711
+ probs_bbox = probs_dict_dict[image_id]
712
+
713
+ draw = ImageDraw.Draw(image)
714
+ # https://stackoverflow.com/questions/66274858/choosing-a-pil-imagefont-by-font-name-rather-than-filename-and-cross-platform-f
715
+ font = font_manager.FontProperties(family='sans-serif', weight='bold')
716
+ font_file = font_manager.findfont(font)
717
+ font_size = 30
718
+ font = ImageFont.truetype(font_file, font_size)
719
+
720
+ for bbox in bboxes_list:
721
+ predicted_label = id2label[probs_bbox[str(bbox)]]
722
+ draw.rectangle(bbox, outline=label2color[predicted_label])
723
+ draw.text((bbox[0] + 10, bbox[1] - font_size), text=predicted_label, fill=label2color[predicted_label], font=font)
724
+
725
+ labeled_images.append(image)
726
+
727
+ return labeled_images
728
+
729
+ # get data of encoded chunk
730
+ def get_encoded_chunk_inference(index_chunk=None):
731
+
732
+ # get datasets
733
+ example = dataset
734
+ encoded_example = encoded_dataset
735
+
736
+ # get randomly a document in dataset
737
+ if index_chunk == None: index_chunk = random.randint(0, len(encoded_example)-1)
738
+ encoded_example = encoded_example[index_chunk]
739
+ encoded_image_ids = encoded_example["images_ids"]
740
+
741
+ # get the image
742
+ example = example.filter(lambda example: example["images_ids"] == encoded_image_ids)[0]
743
+ image = example["images"] # original image
744
+ width, height = image.size
745
+ page_no = example["page_no"]
746
+ num_pages = example["num_pages"]
747
+
748
+ # get boxes, texts, categories
749
+ bboxes, input_ids = encoded_example["normalized_bboxes"][1:-1], encoded_example["input_ids"][1:-1]
750
+ bboxes = [denormalize_box(bbox, width, height) for bbox in bboxes]
751
+ num_tokens = len(input_ids) + 2
752
+
753
+ # get unique bboxes and corresponding labels
754
+ bboxes_list, input_ids_list = list(), list()
755
+ input_ids_dict = dict()
756
+ bbox_prev = [-100, -100, -100, -100]
757
+ for i, (bbox, input_id) in enumerate(zip(bboxes, input_ids)):
758
+ if bbox != bbox_prev:
759
+ bboxes_list.append(bbox)
760
+ input_ids_dict[str(bbox)] = [input_id]
761
+ else:
762
+ input_ids_dict[str(bbox)].append(input_id)
763
+
764
+ # start_indexes_list.append(i)
765
+ bbox_prev = bbox
766
+
767
+ # do not keep "</s><pad><pad>..."
768
+ if input_ids_dict[str(bboxes_list[-1])][0] == (tokenizer.convert_tokens_to_ids('</s>')):
769
+ del input_ids_dict[str(bboxes_list[-1])]
770
+ bboxes_list = bboxes_list[:-1]
771
+
772
+ # get texts by line
773
+ input_ids_list = input_ids_dict.values()
774
+ texts_list = [tokenizer.decode(input_ids) for input_ids in input_ids_list]
775
+
776
+ # display DataFrame
777
+ df = pd.DataFrame({"texts": texts_list, "input_ids": input_ids_list, "bboxes": bboxes_list})
778
+
779
+ return image, df, num_tokens, page_no, num_pages
780
+
781
+ # display chunk of PDF image and its data
782
+ def display_chunk_lines_inference(index_chunk=None):
783
+
784
+ # get image and image data
785
+ image, df, num_tokens, page_no, num_pages = get_encoded_chunk_inference(index_chunk=index_chunk)
786
+
787
+ # get data from dataframe
788
+ input_ids = df["input_ids"]
789
+ texts = df["texts"]
790
+ bboxes = df["bboxes"]
791
+
792
+ print(f'Chunk ({num_tokens} tokens) of the PDF (page: {page_no+1} / {num_pages})\n')
793
+
794
+ # display image with bounding boxes
795
+ print(">> PDF image with bounding boxes of lines\n")
796
+ draw = ImageDraw.Draw(image)
797
+
798
+ labels = list()
799
+ for box, text in zip(bboxes, texts):
800
+ color = "red"
801
+ draw.rectangle(box, outline=color)
802
+
803
+ # resize image to original
804
+ width, height = image.size
805
+ image = image.resize((int(0.5*width), int(0.5*height)))
806
+
807
+ # convert to cv and display
808
+ img = np.array(image, dtype='uint8') # PIL to cv2
809
+ cv2_imshow(img)
810
+ cv2.waitKey(0)
811
+
812
+ # display image dataframe
813
+ print("\n>> Dataframe of annotated lines\n")
814
+ cols = ["texts", "bboxes"]
815
+ df = df[cols]
816
+ display(df)