Charles Kabui
commited on
Commit
·
1f9e550
1
Parent(s):
5ca4eb6
output_document_image_1_hash
Browse files- analysis.ipynb +24 -12
- main.py +20 -17
- utils/get_features.py +2 -2
analysis.ipynb
CHANGED
|
@@ -301,14 +301,14 @@
|
|
| 301 |
},
|
| 302 |
{
|
| 303 |
"cell_type": "code",
|
| 304 |
-
"execution_count":
|
| 305 |
"metadata": {},
|
| 306 |
"outputs": [
|
| 307 |
{
|
| 308 |
"name": "stdout",
|
| 309 |
"output_type": "stream",
|
| 310 |
"text": [
|
| 311 |
-
"Running on local URL: http://127.0.0.1:
|
| 312 |
"\n",
|
| 313 |
"To create a public link, set `share=True` in `launch()`.\n"
|
| 314 |
]
|
|
@@ -316,7 +316,7 @@
|
|
| 316 |
{
|
| 317 |
"data": {
|
| 318 |
"text/html": [
|
| 319 |
-
"<div><iframe src=\"http://127.0.0.1:
|
| 320 |
],
|
| 321 |
"text/plain": [
|
| 322 |
"<IPython.core.display.HTML object>"
|
|
@@ -329,14 +329,6 @@
|
|
| 329 |
"name": "stdout",
|
| 330 |
"output_type": "stream",
|
| 331 |
"text": [
|
| 332 |
-
"{'predicted_bboxes': 'predicted_bboxes', 'predicted_scores': 'predicted_scores', 'predicted_labels': 'predicted_labels'}\n",
|
| 333 |
-
"document_image_1.info.get(annotation_key) == True, start: False\n",
|
| 334 |
-
"document_image_1.info.get(annotation_key) == True, middle: False\n",
|
| 335 |
-
"document_image_1.info.get(annotation_key) == True, end: True\n",
|
| 336 |
-
"{'predicted_bboxes': 'reduced_predicted_bboxes', 'predicted_scores': 'reduced_predicted_scores', 'predicted_labels': 'reduced_predicted_labels'}\n",
|
| 337 |
-
"document_image_1.info.get(annotation_key) == True, start: False\n",
|
| 338 |
-
"document_image_1.info.get(annotation_key) == True, middle: False\n",
|
| 339 |
-
"document_image_1.info.get(annotation_key) == True, end: True\n",
|
| 340 |
"Keyboard interruption in main thread... closing server.\n"
|
| 341 |
]
|
| 342 |
},
|
|
@@ -344,7 +336,7 @@
|
|
| 344 |
"data": {
|
| 345 |
"text/plain": []
|
| 346 |
},
|
| 347 |
-
"execution_count":
|
| 348 |
"metadata": {},
|
| 349 |
"output_type": "execute_result"
|
| 350 |
}
|
|
@@ -356,6 +348,26 @@
|
|
| 356 |
"config_path = '../detectron2-layout-parser/config.yaml'\n",
|
| 357 |
"app(model_path=model_path, config_path=config_path, debug=True)"
|
| 358 |
]
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 359 |
}
|
| 360 |
],
|
| 361 |
"metadata": {
|
|
|
|
| 301 |
},
|
| 302 |
{
|
| 303 |
"cell_type": "code",
|
| 304 |
+
"execution_count": 21,
|
| 305 |
"metadata": {},
|
| 306 |
"outputs": [
|
| 307 |
{
|
| 308 |
"name": "stdout",
|
| 309 |
"output_type": "stream",
|
| 310 |
"text": [
|
| 311 |
+
"Running on local URL: http://127.0.0.1:7861\n",
|
| 312 |
"\n",
|
| 313 |
"To create a public link, set `share=True` in `launch()`.\n"
|
| 314 |
]
|
|
|
|
| 316 |
{
|
| 317 |
"data": {
|
| 318 |
"text/html": [
|
| 319 |
+
"<div><iframe src=\"http://127.0.0.1:7861/\" width=\"100%\" height=\"500\" allow=\"autoplay; camera; microphone; clipboard-read; clipboard-write;\" frameborder=\"0\" allowfullscreen></iframe></div>"
|
| 320 |
],
|
| 321 |
"text/plain": [
|
| 322 |
"<IPython.core.display.HTML object>"
|
|
|
|
| 329 |
"name": "stdout",
|
| 330 |
"output_type": "stream",
|
| 331 |
"text": [
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 332 |
"Keyboard interruption in main thread... closing server.\n"
|
| 333 |
]
|
| 334 |
},
|
|
|
|
| 336 |
"data": {
|
| 337 |
"text/plain": []
|
| 338 |
},
|
| 339 |
+
"execution_count": 21,
|
| 340 |
"metadata": {},
|
| 341 |
"output_type": "execute_result"
|
| 342 |
}
|
|
|
|
| 348 |
"config_path = '../detectron2-layout-parser/config.yaml'\n",
|
| 349 |
"app(model_path=model_path, config_path=config_path, debug=True)"
|
| 350 |
]
|
| 351 |
+
},
|
| 352 |
+
{
|
| 353 |
+
"cell_type": "code",
|
| 354 |
+
"execution_count": 15,
|
| 355 |
+
"metadata": {},
|
| 356 |
+
"outputs": [
|
| 357 |
+
{
|
| 358 |
+
"data": {
|
| 359 |
+
"text/plain": [
|
| 360 |
+
"False"
|
| 361 |
+
]
|
| 362 |
+
},
|
| 363 |
+
"execution_count": 15,
|
| 364 |
+
"metadata": {},
|
| 365 |
+
"output_type": "execute_result"
|
| 366 |
+
}
|
| 367 |
+
],
|
| 368 |
+
"source": [
|
| 369 |
+
"'3d29390000ffff3b' == '3d29090100ffff39'"
|
| 370 |
+
]
|
| 371 |
}
|
| 372 |
],
|
| 373 |
"metadata": {
|
main.py
CHANGED
|
@@ -13,10 +13,12 @@ label_map = {0: 'Caption', 1: 'Footnote', 2: 'Formula', 3: 'List-item', 4: 'Page
|
|
| 13 |
label_names = list(label_map.values())
|
| 14 |
color_map = {'Caption': '#acc2d9', 'Footnote': '#56ae57', 'Formula': '#b2996e', 'List-item': '#a8ff04', 'Page-footer': '#69d84f', 'Page-header': '#894585', 'Picture': '#70b23f', 'Section-header': '#d4ffff', 'Table': '#65ab7c', 'Text': '#952e8f', 'Title': '#fcfc81'}
|
| 15 |
cache = {
|
| 16 |
-
'
|
| 17 |
-
'
|
| 18 |
'document_image_1_features': None,
|
| 19 |
'document_image_2_features': None,
|
|
|
|
|
|
|
| 20 |
}
|
| 21 |
pre_message_style = 'overflow:auto;border:2px solid pink;padding:4px;border-radius:4px;'
|
| 22 |
visualize_bboxes_on_image_kwargs = {
|
|
@@ -27,7 +29,7 @@ visualize_bboxes_on_image_kwargs = {
|
|
| 27 |
'label_rectangle_left_margin': 0,
|
| 28 |
'label_rectangle_top_margin': 0
|
| 29 |
}
|
| 30 |
-
vectors_types = ['vectors', 'weighted_vectors', 'reduced_vectors', '
|
| 31 |
|
| 32 |
annotation_key = 'is_annotated_document_image'
|
| 33 |
annotation_original_image_key = 'original_image'
|
|
@@ -55,25 +57,24 @@ def similarity_fn(model: lp.Detectron2LayoutModel, document_image_1: Image.Image
|
|
| 55 |
if document_image_1 is None or document_image_2 is None:
|
| 56 |
message = f'<pre style="{pre_message_style}">Please load both the documents to compare.<pre>'
|
| 57 |
else:
|
| 58 |
-
|
| 59 |
-
|
| 60 |
|
| 61 |
-
|
| 62 |
-
document_image_2_hash = str(average_hash(document_image_2))
|
| 63 |
-
|
| 64 |
-
if document_image_1_hash == cache['document_image_1_hash']:
|
| 65 |
document_image_1_features = cache['document_image_1_features']
|
|
|
|
| 66 |
else:
|
| 67 |
document_image_1_features = get_features(document_image_1, model, label_names)
|
| 68 |
-
cache['document_image_1_hash'] = document_image_1_hash
|
| 69 |
cache['document_image_1_features'] = document_image_1_features
|
|
|
|
| 70 |
|
| 71 |
-
if
|
| 72 |
document_image_2_features = cache['document_image_2_features']
|
|
|
|
| 73 |
else:
|
| 74 |
document_image_2_features = get_features(document_image_2, model, label_names)
|
| 75 |
-
cache['document_image_2_hash'] = document_image_2_hash
|
| 76 |
cache['document_image_2_features'] = document_image_2_features
|
|
|
|
| 77 |
|
| 78 |
[[similarity]] = cosine_similarity(
|
| 79 |
[
|
|
@@ -83,23 +84,25 @@ def similarity_fn(model: lp.Detectron2LayoutModel, document_image_1: Image.Image
|
|
| 83 |
cache['document_image_2_features'][vectors_type]
|
| 84 |
])
|
| 85 |
message = f'<pre style="{pre_message_style}">Similarity between the two documents is: {round(similarity, 4)}<pre>'
|
| 86 |
-
|
| 87 |
image = document_image_1,
|
| 88 |
bboxes = cache['document_image_1_features'][annotations['predicted_bboxes']],
|
| 89 |
titles = [f'{label}, score:{round(score, 2)}' for label, score in zip(
|
| 90 |
cache['document_image_1_features'][annotations['predicted_labels']],
|
| 91 |
cache['document_image_1_features'][annotations['predicted_scores']])],
|
| 92 |
**visualize_bboxes_on_image_kwargs)
|
| 93 |
-
|
| 94 |
image = document_image_2,
|
| 95 |
bboxes = cache['document_image_2_features'][annotations['predicted_bboxes']],
|
| 96 |
titles = [f'{label}, score:{score}' for label, score in zip(
|
| 97 |
cache['document_image_2_features'][annotations['predicted_labels']],
|
| 98 |
cache['document_image_2_features'][annotations['predicted_scores']])],
|
| 99 |
**visualize_bboxes_on_image_kwargs)
|
|
|
|
|
|
|
|
|
|
|
|
|
| 100 |
show_vectors_type = True
|
| 101 |
-
document_image_1 = annotate_document_image(annotated_document_image_1, document_image_1)
|
| 102 |
-
document_image_2 = annotate_document_image(annotated_document_image_2, document_image_2)
|
| 103 |
except Exception as e:
|
| 104 |
message = f'<pre style="{pre_message_style}">{traceback.format_exc()}<pre>'
|
| 105 |
return [
|
|
@@ -138,7 +141,7 @@ def document_view(document_number: int):
|
|
| 138 |
gr.HTML(value=f'<h4>Load the {"first" if document_number == 1 else "second"} PDF or Document Image<h4>', elem_classes=['center'])
|
| 139 |
with gr.Tabs() as document_tabs:
|
| 140 |
with gr.Tab("From Image", id=0):
|
| 141 |
-
document = gr.Image(type="pil", label=f"Document {document_number}", visible=False)
|
| 142 |
document_error_message = gr.HTML(label="Error Message", visible=False)
|
| 143 |
document_preview = gr.UploadButton(
|
| 144 |
"Upload PDF or Document Image",
|
|
|
|
| 13 |
label_names = list(label_map.values())
|
| 14 |
color_map = {'Caption': '#acc2d9', 'Footnote': '#56ae57', 'Formula': '#b2996e', 'List-item': '#a8ff04', 'Page-footer': '#69d84f', 'Page-header': '#894585', 'Picture': '#70b23f', 'Section-header': '#d4ffff', 'Table': '#65ab7c', 'Text': '#952e8f', 'Title': '#fcfc81'}
|
| 15 |
cache = {
|
| 16 |
+
'output_document_image_1_hash': None,
|
| 17 |
+
'output_document_image_2_hash': None,
|
| 18 |
'document_image_1_features': None,
|
| 19 |
'document_image_2_features': None,
|
| 20 |
+
'original_document_image_1': None,
|
| 21 |
+
'original_document_image_2': None
|
| 22 |
}
|
| 23 |
pre_message_style = 'overflow:auto;border:2px solid pink;padding:4px;border-radius:4px;'
|
| 24 |
visualize_bboxes_on_image_kwargs = {
|
|
|
|
| 29 |
'label_rectangle_left_margin': 0,
|
| 30 |
'label_rectangle_top_margin': 0
|
| 31 |
}
|
| 32 |
+
vectors_types = ['vectors', 'weighted_vectors', 'reduced_vectors', 'weighted_reduced_vectors']
|
| 33 |
|
| 34 |
annotation_key = 'is_annotated_document_image'
|
| 35 |
annotation_original_image_key = 'original_image'
|
|
|
|
| 57 |
if document_image_1 is None or document_image_2 is None:
|
| 58 |
message = f'<pre style="{pre_message_style}">Please load both the documents to compare.<pre>'
|
| 59 |
else:
|
| 60 |
+
input_document_image_1_hash = str(average_hash(document_image_1))
|
| 61 |
+
input_document_image_2_hash = str(average_hash(document_image_2))
|
| 62 |
|
| 63 |
+
if input_document_image_1_hash == cache['output_document_image_1_hash']:
|
|
|
|
|
|
|
|
|
|
| 64 |
document_image_1_features = cache['document_image_1_features']
|
| 65 |
+
document_image_1 = cache['original_document_image_1']
|
| 66 |
else:
|
| 67 |
document_image_1_features = get_features(document_image_1, model, label_names)
|
|
|
|
| 68 |
cache['document_image_1_features'] = document_image_1_features
|
| 69 |
+
cache['original_document_image_1'] = document_image_1
|
| 70 |
|
| 71 |
+
if input_document_image_2_hash == cache['output_document_image_2_hash']:
|
| 72 |
document_image_2_features = cache['document_image_2_features']
|
| 73 |
+
document_image_2 = cache['original_document_image_2']
|
| 74 |
else:
|
| 75 |
document_image_2_features = get_features(document_image_2, model, label_names)
|
|
|
|
| 76 |
cache['document_image_2_features'] = document_image_2_features
|
| 77 |
+
cache['original_document_image_2'] = document_image_2
|
| 78 |
|
| 79 |
[[similarity]] = cosine_similarity(
|
| 80 |
[
|
|
|
|
| 84 |
cache['document_image_2_features'][vectors_type]
|
| 85 |
])
|
| 86 |
message = f'<pre style="{pre_message_style}">Similarity between the two documents is: {round(similarity, 4)}<pre>'
|
| 87 |
+
document_image_1 = visualize_bboxes_on_image(
|
| 88 |
image = document_image_1,
|
| 89 |
bboxes = cache['document_image_1_features'][annotations['predicted_bboxes']],
|
| 90 |
titles = [f'{label}, score:{round(score, 2)}' for label, score in zip(
|
| 91 |
cache['document_image_1_features'][annotations['predicted_labels']],
|
| 92 |
cache['document_image_1_features'][annotations['predicted_scores']])],
|
| 93 |
**visualize_bboxes_on_image_kwargs)
|
| 94 |
+
document_image_2 = visualize_bboxes_on_image(
|
| 95 |
image = document_image_2,
|
| 96 |
bboxes = cache['document_image_2_features'][annotations['predicted_bboxes']],
|
| 97 |
titles = [f'{label}, score:{score}' for label, score in zip(
|
| 98 |
cache['document_image_2_features'][annotations['predicted_labels']],
|
| 99 |
cache['document_image_2_features'][annotations['predicted_scores']])],
|
| 100 |
**visualize_bboxes_on_image_kwargs)
|
| 101 |
+
|
| 102 |
+
cache['output_document_image_1_hash'] = str(average_hash(document_image_1))
|
| 103 |
+
cache['output_document_image_2_hash'] = str(average_hash(document_image_2))
|
| 104 |
+
|
| 105 |
show_vectors_type = True
|
|
|
|
|
|
|
| 106 |
except Exception as e:
|
| 107 |
message = f'<pre style="{pre_message_style}">{traceback.format_exc()}<pre>'
|
| 108 |
return [
|
|
|
|
| 141 |
gr.HTML(value=f'<h4>Load the {"first" if document_number == 1 else "second"} PDF or Document Image<h4>', elem_classes=['center'])
|
| 142 |
with gr.Tabs() as document_tabs:
|
| 143 |
with gr.Tab("From Image", id=0):
|
| 144 |
+
document = gr.Image(type="pil", label=f"Document {document_number}", visible=False, interactive=False, show_download_button=False)
|
| 145 |
document_error_message = gr.HTML(label="Error Message", visible=False)
|
| 146 |
document_preview = gr.UploadButton(
|
| 147 |
"Upload PDF or Document Image",
|
utils/get_features.py
CHANGED
|
@@ -102,7 +102,7 @@ def get_features(image: Image.Image, model: lp.Detectron2LayoutModel, label_name
|
|
| 102 |
weighted_jaccard_index = False,
|
| 103 |
**reduced_predictions)
|
| 104 |
|
| 105 |
-
|
| 106 |
sub_images_bboxes = sub_images_bboxes,
|
| 107 |
label_names = label_names,
|
| 108 |
weighted_jaccard_index = True,
|
|
@@ -119,5 +119,5 @@ def get_features(image: Image.Image, model: lp.Detectron2LayoutModel, label_name
|
|
| 119 |
'reduced_predicted_scores': reduced_predictions['predicted_scores'],
|
| 120 |
'reduced_predicted_labels': reduced_predictions['predicted_labels'],
|
| 121 |
'reduced_vectors': list(reduced_vectors),
|
| 122 |
-
'weighted_reduced_vectors': list(
|
| 123 |
}
|
|
|
|
| 102 |
weighted_jaccard_index = False,
|
| 103 |
**reduced_predictions)
|
| 104 |
|
| 105 |
+
weighted_reduced_vectors = get_vectors(
|
| 106 |
sub_images_bboxes = sub_images_bboxes,
|
| 107 |
label_names = label_names,
|
| 108 |
weighted_jaccard_index = True,
|
|
|
|
| 119 |
'reduced_predicted_scores': reduced_predictions['predicted_scores'],
|
| 120 |
'reduced_predicted_labels': reduced_predictions['predicted_labels'],
|
| 121 |
'reduced_vectors': list(reduced_vectors),
|
| 122 |
+
'weighted_reduced_vectors': list(weighted_reduced_vectors),
|
| 123 |
}
|