{
    "body": "<section id=\"cv-analysis-visual-cv-based-document-parsing\">\n<h1>cv-analysis - Visual (CV-Based) Document Parsing<a class=\"headerlink\" href=\"#cv-analysis-visual-cv-based-document-parsing\" title=\"Link to this heading\">#</a></h1>\n<p>parse_pdf()\nThis repository implements computer vision based approaches for detecting and parsing visual features such as tables or\nprevious redactions in documents.</p>\n<section id=\"api\">\n<h2>API<a class=\"headerlink\" href=\"#api\" title=\"Link to this heading\">#</a></h2>\n<p>Input message:</p>\n<div class=\"highlight-json notranslate\"><div class=\"highlight\"><pre><span></span><span class=\"p\">{</span>\n<span class=\"w\">  </span><span class=\"nt\">&quot;targetFilePath&quot;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"p\">{</span>\n<span class=\"w\">    </span><span class=\"nt\">&quot;pdf&quot;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"s2\">&quot;absolute file path&quot;</span><span class=\"p\">,</span>\n<span class=\"w\">    </span><span class=\"nt\">&quot;vlp_output&quot;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"s2\">&quot;absolute file path&quot;</span>\n<span class=\"w\">  </span><span class=\"p\">},</span>\n<span class=\"w\">  </span><span class=\"nt\">&quot;responseFilePath&quot;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"s2\">&quot;absolute file path&quot;</span><span class=\"p\">,</span>\n<span class=\"w\">  </span><span class=\"nt\">&quot;operation&quot;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"s2\">&quot;table_image_inference&quot;</span>\n<span class=\"p\">}</span>\n</pre></div>\n</div>\n<p>Response is uploaded to the storage as specified in the <code class=\"docutils literal notranslate\"><span class=\"pre\">responseFilePath</span></code> field. The structure is as follows:</p>\n<div class=\"highlight-json notranslate\"><div class=\"highlight\"><pre><span></span><span class=\"p\">{</span>\n<span class=\"w\">  </span><span class=\"err\">...</span><span class=\"p\">,</span>\n<span class=\"w\">  </span><span class=\"nt\">&quot;data&quot;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"p\">[</span>\n<span class=\"w\">    </span><span class=\"p\">{</span>\n<span class=\"w\">      </span><span class=\"err\">&#39;pageNum&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">0</span><span class=\"p\">,</span>\n<span class=\"w\">      </span><span class=\"err\">&#39;bbox&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"p\">{</span>\n<span class=\"w\">        </span><span class=\"err\">&#39;x</span><span class=\"mi\">1</span><span class=\"err\">&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mf\">55.3407</span><span class=\"p\">,</span>\n<span class=\"w\">        </span><span class=\"err\">&#39;y</span><span class=\"mi\">1</span><span class=\"err\">&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mf\">247.0246</span><span class=\"p\">,</span>\n<span class=\"w\">        </span><span class=\"err\">&#39;x</span><span class=\"mi\">2</span><span class=\"err\">&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mf\">558.5602</span><span class=\"p\">,</span>\n<span class=\"w\">        </span><span class=\"err\">&#39;y</span><span class=\"mi\">2</span><span class=\"err\">&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mf\">598.0585</span>\n<span class=\"w\">      </span><span class=\"p\">},</span>\n<span class=\"w\">      </span><span class=\"err\">&#39;uuid&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"err\">&#39;</span><span class=\"mi\">2</span><span class=\"err\">b</span><span class=\"mi\">10</span><span class=\"err\">c</span><span class=\"mi\">1</span><span class=\"err\">a</span><span class=\"mi\">2-393</span><span class=\"err\">c</span><span class=\"mi\">-4</span><span class=\"kc\">f</span><span class=\"err\">ca</span><span class=\"mi\">-</span><span class=\"err\">b</span><span class=\"mf\">9e3-0</span><span class=\"err\">ad</span><span class=\"mi\">5</span><span class=\"err\">b</span><span class=\"mi\">774</span><span class=\"err\">ac</span><span class=\"mi\">84</span><span class=\"err\">&#39;</span><span class=\"p\">,</span>\n<span class=\"w\">      </span><span class=\"err\">&#39;label&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"err\">&#39;</span><span class=\"kc\">ta</span><span class=\"err\">ble&#39;</span><span class=\"p\">,</span>\n<span class=\"w\">      </span><span class=\"err\">&#39;</span><span class=\"kc\">ta</span><span class=\"err\">bleLi</span><span class=\"kc\">nes</span><span class=\"err\">&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"p\">[</span>\n<span class=\"w\">        </span><span class=\"p\">{</span>\n<span class=\"w\">          </span><span class=\"err\">&#39;x</span><span class=\"mi\">1</span><span class=\"err\">&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">0</span><span class=\"p\">,</span>\n<span class=\"w\">          </span><span class=\"err\">&#39;y</span><span class=\"mi\">1</span><span class=\"err\">&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">16</span><span class=\"p\">,</span>\n<span class=\"w\">          </span><span class=\"err\">&#39;x</span><span class=\"mi\">2</span><span class=\"err\">&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">1399</span><span class=\"p\">,</span>\n<span class=\"w\">          </span><span class=\"err\">&#39;y</span><span class=\"mi\">2</span><span class=\"err\">&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">16</span>\n<span class=\"w\">        </span><span class=\"p\">},</span>\n<span class=\"w\">        </span><span class=\"err\">...</span>\n<span class=\"w\">      </span><span class=\"p\">],</span>\n<span class=\"w\">      </span><span class=\"err\">&#39;imageI</span><span class=\"kc\">nf</span><span class=\"err\">o&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"p\">{</span>\n<span class=\"w\">        </span><span class=\"err\">&#39;heigh</span><span class=\"kc\">t</span><span class=\"err\">&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">693</span><span class=\"p\">,</span>\n<span class=\"w\">        </span><span class=\"err\">&#39;wid</span><span class=\"kc\">t</span><span class=\"err\">h&#39;</span><span class=\"p\">:</span><span class=\"w\"> </span><span class=\"mi\">1414</span>\n<span class=\"w\">      </span><span class=\"p\">}</span>\n<span class=\"w\">    </span><span class=\"p\">},</span>\n<span class=\"w\">    </span><span class=\"err\">...</span>\n<span class=\"w\">  </span><span class=\"p\">]</span>\n<span class=\"p\">}</span>\n</pre></div>\n</div>\n</section>\n<section id=\"installation\">\n<h2>Installation<a class=\"headerlink\" href=\"#installation\" title=\"Link to this heading\">#</a></h2>\n<div class=\"highlight-bash notranslate\"><div class=\"highlight\"><pre><span></span>git<span class=\"w\"> </span>clone<span class=\"w\"> </span>ssh://git@git.iqser.com:2222/rr/cv-analysis.git\n<span class=\"nb\">cd</span><span class=\"w\"> </span>cv-analysis\n\npython<span class=\"w\"> </span>-m<span class=\"w\"> </span>venv<span class=\"w\"> </span>env\n<span class=\"nb\">source</span><span class=\"w\"> </span>env/bin/activate\n\npip<span class=\"w\"> </span>install<span class=\"w\"> </span>-e<span class=\"w\"> </span>.\npip<span class=\"w\"> </span>install<span class=\"w\"> </span>-r<span class=\"w\"> </span>requirements.txt\n\ndvc<span class=\"w\"> </span>pull\n</pre></div>\n</div>\n</section>\n<section id=\"usage\">\n<h2>Usage<a class=\"headerlink\" href=\"#usage\" title=\"Link to this heading\">#</a></h2>\n<section id=\"as-an-api\">\n<h3>As an API<a class=\"headerlink\" href=\"#as-an-api\" title=\"Link to this heading\">#</a></h3>\n<p>The module provided functions for the individual tasks that all return some kind of collection of points, depending on\nthe specific task.</p>\n<section id=\"redaction-detection-api\">\n<h4>Redaction Detection (API)<a class=\"headerlink\" href=\"#redaction-detection-api\" title=\"Link to this heading\">#</a></h4>\n<p>The below snippet shows hot to find the outlines of previous redactions.</p>\n<div class=\"highlight-python notranslate\"><div class=\"highlight\"><pre><span></span><span class=\"kn\">from</span> <span class=\"nn\">cv_analysis.redaction_detection</span> <span class=\"kn\">import</span> <span class=\"n\">find_redactions</span>\n<span class=\"kn\">import</span> <span class=\"nn\">pdf2image</span>\n<span class=\"kn\">import</span> <span class=\"nn\">numpy</span> <span class=\"k\">as</span> <span class=\"nn\">np</span>\n\n<span class=\"n\">pdf_path</span> <span class=\"o\">=</span> <span class=\"o\">...</span>\n<span class=\"n\">page_index</span> <span class=\"o\">=</span> <span class=\"o\">...</span>\n\n<span class=\"n\">page</span> <span class=\"o\">=</span> <span class=\"n\">pdf2image</span><span class=\"o\">.</span><span class=\"n\">convert_from_path</span><span class=\"p\">(</span><span class=\"n\">pdf_path</span><span class=\"p\">,</span> <span class=\"n\">first_page</span><span class=\"o\">=</span><span class=\"n\">page_index</span><span class=\"p\">,</span> <span class=\"n\">last_page</span><span class=\"o\">=</span><span class=\"n\">page_index</span><span class=\"p\">)[</span><span class=\"mi\">0</span><span class=\"p\">]</span>\n<span class=\"n\">page</span> <span class=\"o\">=</span> <span class=\"n\">np</span><span class=\"o\">.</span><span class=\"n\">array</span><span class=\"p\">(</span><span class=\"n\">page</span><span class=\"p\">)</span>\n\n<span class=\"n\">redaction_contours</span> <span class=\"o\">=</span> <span class=\"n\">find_redactions</span><span class=\"p\">(</span><span class=\"n\">page</span><span class=\"p\">)</span>\n</pre></div>\n</div>\n</section>\n</section>\n</section>\n<section id=\"as-a-cli-tool\">\n<h2>As a CLI Tool<a class=\"headerlink\" href=\"#as-a-cli-tool\" title=\"Link to this heading\">#</a></h2>\n<p>Core API functionalities can be used through a CLI.</p>\n<section id=\"table-parsing\">\n<h3>Table Parsing<a class=\"headerlink\" href=\"#table-parsing\" title=\"Link to this heading\">#</a></h3>\n<p>The tables parsing utility detects and segments tables into individual cells.</p>\n<div class=\"highlight-bash notranslate\"><div class=\"highlight\"><pre><span></span>python<span class=\"w\"> </span>scripts/annotate.py<span class=\"w\"> </span>data/test_pdf.pdf<span class=\"w\"> </span><span class=\"m\">7</span><span class=\"w\"> </span>--type<span class=\"w\"> </span>table\n</pre></div>\n</div>\n<p>The below image shows a parsed table, where each table cell has been detected individually.</p>\n<p><img alt=\"Table Parsing Demonstration\" src=\"_images/table_parsing.png\" /></p>\n</section>\n<section id=\"redaction-detection-cli\">\n<h3>Redaction Detection (CLI)<a class=\"headerlink\" href=\"#redaction-detection-cli\" title=\"Link to this heading\">#</a></h3>\n<p>The redaction detection utility detects previous redactions in PDFs (filled black rectangles).</p>\n<div class=\"highlight-bash notranslate\"><div class=\"highlight\"><pre><span></span>python<span class=\"w\"> </span>scripts/annotate.py<span class=\"w\"> </span>data/test_pdf.pdf<span class=\"w\"> </span><span class=\"m\">2</span><span class=\"w\"> </span>--type<span class=\"w\"> </span>redaction\n</pre></div>\n</div>\n<p>The below image shows the detected redactions with green outlines.</p>\n<p><img alt=\"Redaction Detection Demonstration\" src=\"_images/redaction_detection.png\" /></p>\n</section>\n<section id=\"layout-parsing\">\n<h3>Layout Parsing<a class=\"headerlink\" href=\"#layout-parsing\" title=\"Link to this heading\">#</a></h3>\n<p>The layout parsing utility detects elements such as paragraphs, tables and figures.</p>\n<div class=\"highlight-bash notranslate\"><div class=\"highlight\"><pre><span></span>python<span class=\"w\"> </span>scripts/annotate.py<span class=\"w\"> </span>data/test_pdf.pdf<span class=\"w\"> </span><span class=\"m\">7</span><span class=\"w\"> </span>--type<span class=\"w\"> </span>layout\n</pre></div>\n</div>\n<p>The below image shows the detected layout elements on a page.</p>\n<p><img alt=\"Layout Parsing Demonstration\" src=\"_images/layout_parsing.png\" /></p>\n</section>\n<section id=\"figure-detection\">\n<h3>Figure Detection<a class=\"headerlink\" href=\"#figure-detection\" title=\"Link to this heading\">#</a></h3>\n<p>The figure detection utility detects figures specifically, which can be missed by the generic layout parsing utility.</p>\n<div class=\"highlight-bash notranslate\"><div class=\"highlight\"><pre><span></span>python<span class=\"w\"> </span>scripts/annotate.py<span class=\"w\"> </span>data/test_pdf.pdf<span class=\"w\"> </span><span class=\"m\">3</span><span class=\"w\"> </span>--type<span class=\"w\"> </span>figure\n</pre></div>\n</div>\n<p>The below image shows the detected figure on a page.</p>\n<p><img alt=\"Figure Detection Demonstration\" src=\"_images/figure_detection.png\" /></p>\n</section>\n</section>\n<section id=\"running-as-a-service\">\n<h2>Running as a service<a class=\"headerlink\" href=\"#running-as-a-service\" title=\"Link to this heading\">#</a></h2>\n<section id=\"building\">\n<h3>Building<a class=\"headerlink\" href=\"#building\" title=\"Link to this heading\">#</a></h3>\n<p>Build base image</p>\n<div class=\"highlight-bash notranslate\"><div class=\"highlight\"><pre><span></span>bash<span class=\"w\"> </span>setup/docker.sh\n</pre></div>\n</div>\n<p>Build head image</p>\n<div class=\"highlight-bash notranslate\"><div class=\"highlight\"><pre><span></span>docker<span class=\"w\"> </span>build<span class=\"w\"> </span>-f<span class=\"w\"> </span>Dockerfile<span class=\"w\"> </span>-t<span class=\"w\"> </span>cv-analysis<span class=\"w\"> </span>.<span class=\"w\"> </span>--build-arg<span class=\"w\"> </span><span class=\"nv\">BASE_ROOT</span><span class=\"o\">=</span><span class=\"s2\">&quot;&quot;</span>\n</pre></div>\n</div>\n</section>\n<section id=\"usage-service\">\n<h3>Usage (service)<a class=\"headerlink\" href=\"#usage-service\" title=\"Link to this heading\">#</a></h3>\n<p>Shell 1</p>\n<div class=\"highlight-bash notranslate\"><div class=\"highlight\"><pre><span></span>docker<span class=\"w\"> </span>run<span class=\"w\"> </span>--rm<span class=\"w\"> </span>--net<span class=\"o\">=</span>host<span class=\"w\"> </span>--rm<span class=\"w\"> </span>cv-analysis\n</pre></div>\n</div>\n<p>Shell 2</p>\n<div class=\"highlight-bash notranslate\"><div class=\"highlight\"><pre><span></span>python<span class=\"w\"> </span>scripts/client_mock.py<span class=\"w\"> </span>--pdf_path<span class=\"w\"> </span>/path/to/a/pdf\n</pre></div>\n</div>\n</section>\n</section>\n</section>\n",
    "title": "cv-analysis - Visual (CV-Based) Document Parsing",
    "sourcename": "README.md.txt",
    "current_page_name": "README",
    "toc": "<ul>\n<li><a class=\"reference internal\" href=\"#\">cv-analysis - Visual (CV-Based) Document Parsing</a><ul>\n<li><a class=\"reference internal\" href=\"#api\">API</a></li>\n<li><a class=\"reference internal\" href=\"#installation\">Installation</a></li>\n<li><a class=\"reference internal\" href=\"#usage\">Usage</a><ul>\n<li><a class=\"reference internal\" href=\"#as-an-api\">As an API</a><ul>\n<li><a class=\"reference internal\" href=\"#redaction-detection-api\">Redaction Detection (API)</a></li>\n</ul>\n</li>\n</ul>\n</li>\n<li><a class=\"reference internal\" href=\"#as-a-cli-tool\">As a CLI Tool</a><ul>\n<li><a class=\"reference internal\" href=\"#table-parsing\">Table Parsing</a></li>\n<li><a class=\"reference internal\" href=\"#redaction-detection-cli\">Redaction Detection (CLI)</a></li>\n<li><a class=\"reference internal\" href=\"#layout-parsing\">Layout Parsing</a></li>\n<li><a class=\"reference internal\" href=\"#figure-detection\">Figure Detection</a></li>\n</ul>\n</li>\n<li><a class=\"reference internal\" href=\"#running-as-a-service\">Running as a service</a><ul>\n<li><a class=\"reference internal\" href=\"#building\">Building</a></li>\n<li><a class=\"reference internal\" href=\"#usage-service\">Usage (service)</a></li>\n</ul>\n</li>\n</ul>\n</li>\n</ul>\n",
    "page_source_suffix": ".md"
}