File size: 10,207 Bytes
6d407f0 cd70165 6d407f0 cd70165 6d407f0 cd70165 6d407f0 cd70165 6d407f0 88e7d50 6d407f0 cd70165 6d407f0 cd70165 6d407f0 cd70165 6d407f0 cd70165 6d407f0 cd70165 6d407f0 cd70165 6d407f0 88e7d50 6d407f0 |
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 304 305 306 307 308 309 310 311 312 313 314 315 316 |
{
"cells": [
{
"cell_type": "markdown",
"metadata": {
"id": "nKW7KdnwBZA2"
},
"source": [
"### Details"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "lAFpDK5eBZA3"
},
"source": [
"`/raw/DocLayNet_core.zip` downloaded from [DocLayNet_core.zip dataset](https://codait-cos-dax.s3.us.cloud-object-storage.appdomain.cloud/dax-doclaynet/1.0.0/DocLayNet_core.zip)\n",
"\n",
"`/raw/RVL-CDIP-invoice.zip` downloaded from [chainyo/rvl-cdip-invoice](https://huggingface.co/datasets/chainyo/rvl-cdip-invoice). It can also be downloaded from [aharley/rvl_cdip](https://huggingface.co/datasets/aharley/rvl_cdip).\n",
"\n",
"`/processed/vectors/RVL-CDIP-invoice.json.zip` generated using `/raw/RVL-CDIP-invoice.zip`, and the model to create the following features."
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "6qDjoIz8BZA4"
},
"source": [
"### Preview Data Raw"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "fzP0OHeTi1kV"
},
"outputs": [],
"source": [
"import sys\n",
"sys.path.insert(0, './document-similarity-search-using-visual-layout-features')\n",
"\n",
"%load_ext autoreload\n",
"%autoreload 2\n",
"\n",
"DOC_LAY_NET_CORE_ZIP = './raw/DocLayNet_core.zip'\n",
"RVL_CDIP_invoice = './raw/RVL-CDIP-invoice.zip'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "FsBPB2hLiSYt"
},
"outputs": [],
"source": [
"%pip install treeviz\n",
"%pip install --quiet simplebbox"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "gltCBti1hkr7"
},
"outputs": [],
"source": [
"import zipfile\n",
"import re\n",
"from utils.read_zip_file import read_zip_file\n",
"import json\n",
"from treeviz.treeviz import Node\n",
"from utils.visualize_bboxes_on_image import visualize_bboxes_on_image\n",
"from PIL import Image\n",
"import os\n",
"from simplebbox.array import x0y0wh_to_x0y0x1y1\n",
"from utils.remove_duplicates import remove_duplicates\n",
"from utils.show_tile_images import show_tile_images\n",
"import random"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "8gZ9K_d_BZA4"
},
"source": [
"#### `/raw/DocLayNet_core.zip` contents"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "Ai7mxKoyfKNU"
},
"source": [
"##### Directory structure"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "j81xEDLEBZA4",
"outputId": "170f571a-a7d9-4823-d2db-4069744c3d10"
},
"outputs": [],
"source": [
"doc_lay_zipfile = zipfile.ZipFile(DOC_LAY_NET_CORE_ZIP, 'r')\n",
"\n",
"test_json = json.load(read_zip_file(DOC_LAY_NET_CORE_ZIP, 'COCO/test.json'))\n",
"train_json = json.load(read_zip_file(DOC_LAY_NET_CORE_ZIP, 'COCO/train.json'))\n",
"val_json = json.load(read_zip_file(DOC_LAY_NET_CORE_ZIP, 'COCO/val.json'))\n",
"\n",
"root = Node(\"./raw/DocLayNet_core.zip\")\n",
"child1 = Node(\"COCO/\")\n",
"child1.add_child(Node('.DS_Store'))\n",
"test_json_node = Node('test.json')\n",
"[test_json_node.add_child(Node(f'{key} - {len(values):,} {key}')) for key, values in test_json.items()]\n",
"train_json_node = Node('train.json')\n",
"[train_json_node.add_child(Node(f'{key} - {len(values):,} {key}')) for key, values in train_json.items()]\n",
"val_json_node = Node('val.json')\n",
"[val_json_node.add_child(Node(f'{key} - {len(values):,} {key}')) for key, values in val_json.items()]\n",
"child1.add_child(test_json_node)\n",
"child1.add_child(train_json_node)\n",
"child1.add_child(val_json_node)\n",
"child2 = Node(\"PNG/\")\n",
"images = [i.filename for i in doc_lay_zipfile.filelist if re.fullmatch('PNG/.+\\.png', i.filename)]\n",
"child2.add_child(Node(f'*.png - {len(images):,} images'))\n",
"root.add_child(child1)\n",
"root.add_child(child2)\n",
"root.visualize(line_space=1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "F4gd3cxhfSJy"
},
"source": [
"##### Contents"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"id": "TfCCkKd2yFRo"
},
"outputs": [],
"source": [
"def get_annotatated_image(coco_json: json, image_path: str):\n",
" categories = [i['name'] for i in coco_json['categories']]\n",
" image_obj= next(i for i in coco_json['images'] if i['file_name'] == image_path)\n",
" image_id = image_obj['id']\n",
" image_path = image_obj['file_name']\n",
" annotations = [i for i in coco_json['annotations'] if i['image_id'] == image_id]\n",
" bboxes = [x0y0wh_to_x0y0x1y1(i['bbox']) for i in annotations]\n",
" labels = [categories[i['category_id']-1] for i in annotations]\n",
" bboxes, labels = remove_duplicates(zip(bboxes, labels), key=lambda x: tuple(x[0]), unzip=True)\n",
" image = Image.open(read_zip_file(DOC_LAY_NET_CORE_ZIP, os.path.join(\"PNG\", image_path))).convert('RGB')\n",
" return visualize_bboxes_on_image(\n",
" image,\n",
" bboxes,\n",
" labels,\n",
" label_text_color='white',\n",
" label_rectangle_color='black',\n",
" label_text_size=14,\n",
" label_text_padding=5,\n",
" label_rectangle_left_padding=0,\n",
" label_rectangle_top_padding=0,)\n",
"\n",
"def format_name(file_name: str):\n",
" name_, ext_ = file_name.split('.')\n",
" return f'PNG/{name_[:4]}---{name_[-4:]}.{ext_}'"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "ZahHUiLtzM3H",
"outputId": "ad8ce1eb-db2e-47ea-b492-177aceb7a032"
},
"outputs": [],
"source": [
"images_sample = random.sample([i['file_name'] for i in test_json['images']], 4)\n",
"show_tile_images(\n",
" images = [get_annotatated_image(test_json, i.split('/')[-1]) for i in images_sample],\n",
" titles = [format_name(i) for i in images_sample],\n",
" width_parts = 2,\n",
" figsize = (10.5 * 2, 12 * (len(images_sample) / 2)),\n",
" space = 6,\n",
" pad = True,\n",
" figcolor = '#d3eddd',\n",
" title_color = 'black',\n",
" title_background_color = 'white',\n",
" title_font_size = 30)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "SEQVXtS5sTWl"
},
"source": [
"#### `/raw/RVL-CDIP-invoice.zip` contents"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "bSYJ4puJFqOO"
},
"source": [
"##### Directory structure"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "1T0tHpwRm8-R",
"outputId": "0518bff4-18ce-412f-baff-868a4ed22479"
},
"outputs": [],
"source": [
"invoices_zipfile = zipfile.ZipFile(RVL_CDIP_invoice, 'r')\n",
"\n",
"root_invoices = Node(RVL_CDIP_invoice)\n",
"\n",
"root_invoices_images = Node(\"RVL-CDIP-invoice/\")\n",
"invoice_images = [i.filename for i in invoices_zipfile.filelist if re.fullmatch('RVL-CDIP-invoice/.+\\.png', i.filename)]\n",
"root_invoices_images.add_child(Node(f'*.png - {len(invoice_images):,} images'))\n",
"root_invoices.add_child(root_invoices_images)\n",
"root_invoices.visualize(line_space=1)"
]
},
{
"cell_type": "markdown",
"metadata": {
"id": "T5-Y8im-Frbw"
},
"source": [
"##### Contents"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/",
"height": 1000
},
"id": "__EuMuVwFwWy",
"outputId": "3fb83e47-90d2-4e1d-c654-210b42973a9f"
},
"outputs": [],
"source": [
"invoice_images_sample = random.sample(invoice_images, 4)\n",
"show_tile_images(\n",
" images = [Image.open(read_zip_file(RVL_CDIP_invoice, i)).convert('RGB') for i in invoice_images_sample],\n",
" titles = [i for i in invoice_images_sample],\n",
" width_parts = 2,\n",
" figsize = (10.5 * 2, 12 * (len(invoice_images_sample) / 2)),\n",
" space = 5,\n",
" pad = True,\n",
" figcolor = '#d3eddd',\n",
" title_color = 'black',\n",
" title_background_color = 'white',\n",
" title_font_size = 30)"
]
}
],
"metadata": {
"colab": {
"provenance": [],
"toc_visible": true
},
"kernelspec": {
"display_name": "dss-env",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.1.0"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
|