|
| 1 | +{ |
| 2 | + "cells": [ |
| 3 | + { |
| 4 | + "cell_type": "code", |
| 5 | + "execution_count": null, |
| 6 | + "outputs": [], |
| 7 | + "source": [ |
| 8 | + "import os\n", |
| 9 | + "import sys\n", |
| 10 | + "\n", |
| 11 | + "from unstructured_inference.constants import AnnotationResult\n", |
| 12 | + "from unstructured_inference.inference.layout import process_file_with_model\n", |
| 13 | + "from unstructured_inference.utils import annotate_layout_elements" |
| 14 | + ], |
| 15 | + "metadata": { |
| 16 | + "collapsed": false |
| 17 | + } |
| 18 | + }, |
| 19 | + { |
| 20 | + "cell_type": "code", |
| 21 | + "execution_count": null, |
| 22 | + "outputs": [], |
| 23 | + "source": [ |
| 24 | + "output_basedir_path = \"output\"" |
| 25 | + ], |
| 26 | + "metadata": { |
| 27 | + "collapsed": false |
| 28 | + } |
| 29 | + }, |
| 30 | + { |
| 31 | + "cell_type": "code", |
| 32 | + "execution_count": null, |
| 33 | + "outputs": [], |
| 34 | + "source": [ |
| 35 | + "def run(f_path, f_type):\n", |
| 36 | + " if f_type == \"pdf\":\n", |
| 37 | + " is_image = False\n", |
| 38 | + " elif f_type == \"image\":\n", |
| 39 | + " is_image = True\n", |
| 40 | + " else:\n", |
| 41 | + " print(\"Invalid file type.\")\n", |
| 42 | + " sys.exit(1)\n", |
| 43 | + "\n", |
| 44 | + " annotation_data_map = {\n", |
| 45 | + " \"final\": None,\n", |
| 46 | + " }\n", |
| 47 | + " actions = [False, True]\n", |
| 48 | + " for action in actions:\n", |
| 49 | + " _f_basename = os.path.splitext(os.path.basename(f_path))[0]\n", |
| 50 | + " output_dir_path = os.path.join(output_basedir_path, f\"{_f_basename}_{file_type}\")\n", |
| 51 | + " os.makedirs(output_dir_path, exist_ok=True)\n", |
| 52 | + "\n", |
| 53 | + " f_basename = f\"updated_{_f_basename}\" if action else f\"original_{_f_basename}\"\n", |
| 54 | + "\n", |
| 55 | + " label = \"Updated Results: \" if action else \"Original Results: \"\n", |
| 56 | + " print(label)\n", |
| 57 | + "\n", |
| 58 | + " doc = process_file_with_model(\n", |
| 59 | + " f_path,\n", |
| 60 | + " is_image=is_image,\n", |
| 61 | + " model_name=None,\n", |
| 62 | + " supplement_with_ocr_elements=action,\n", |
| 63 | + " analysis=True,\n", |
| 64 | + " )\n", |
| 65 | + "\n", |
| 66 | + " annotate_layout_elements(doc, annotation_data_map, output_dir_path, f_basename, AnnotationResult.PLOT)" |
| 67 | + ], |
| 68 | + "metadata": { |
| 69 | + "collapsed": false |
| 70 | + } |
| 71 | + }, |
| 72 | + { |
| 73 | + "cell_type": "code", |
| 74 | + "execution_count": null, |
| 75 | + "outputs": [], |
| 76 | + "source": [ |
| 77 | + "file_path = \"../../sample-docs/patent-1p.pdf\"\n", |
| 78 | + "file_type = \"pdf\"\n", |
| 79 | + "f_name = os.path.basename(file_path)\n", |
| 80 | + "print(f\"file_name: {f_name} - file_type: {file_type}\")\n", |
| 81 | + "\n", |
| 82 | + "run(file_path, file_type)" |
| 83 | + ], |
| 84 | + "metadata": { |
| 85 | + "collapsed": false |
| 86 | + } |
| 87 | + }, |
| 88 | + { |
| 89 | + "cell_type": "code", |
| 90 | + "execution_count": null, |
| 91 | + "outputs": [], |
| 92 | + "source": [ |
| 93 | + "file_path = \"../../sample-docs/layout-parser-paper-fast.jpg\"\n", |
| 94 | + "file_type = \"image\"\n", |
| 95 | + "f_name = os.path.basename(file_path)\n", |
| 96 | + "print(f\"file_name: {f_name} - file_type: {file_type}\")\n", |
| 97 | + "\n", |
| 98 | + "run(file_path, file_type)" |
| 99 | + ], |
| 100 | + "metadata": { |
| 101 | + "collapsed": false |
| 102 | + } |
| 103 | + } |
| 104 | + ], |
| 105 | + "metadata": { |
| 106 | + "kernelspec": { |
| 107 | + "display_name": "Python 3", |
| 108 | + "language": "python", |
| 109 | + "name": "python3" |
| 110 | + }, |
| 111 | + "language_info": { |
| 112 | + "codemirror_mode": { |
| 113 | + "name": "ipython", |
| 114 | + "version": 2 |
| 115 | + }, |
| 116 | + "file_extension": ".py", |
| 117 | + "mimetype": "text/x-python", |
| 118 | + "name": "python", |
| 119 | + "nbconvert_exporter": "python", |
| 120 | + "pygments_lexer": "ipython2", |
| 121 | + "version": "2.7.6" |
| 122 | + } |
| 123 | + }, |
| 124 | + "nbformat": 4, |
| 125 | + "nbformat_minor": 0 |
| 126 | +} |
0 commit comments