{ "cells": [ { "cell_type": "code", "source": [ "from PIL import Image\n", "import cv2\n", "import numpy as np\n", "import pandas as pd\n", "import tensorflow as tf" ], "metadata": { "id": "mhEjVUbiUytV" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "#!pip install paddlepaddle-gpu==2.3.2 cudatoolkit==10.2" ], "metadata": { "id": "JejuCeNXHckg" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "!pip install paddlepaddle-gpu==2.3.0.post110 -f https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xWhdOt9Jay6z", "outputId": "cda3179f-f1d6-41e3-bdd2-6d8fdeab5867" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Looking in links: https://www.paddlepaddle.org.cn/whl/linux/mkl/avx/stable.html\n", "Collecting paddlepaddle-gpu==2.3.0.post110\n", " Downloading https://paddle-wheel.bj.bcebos.com/2.3.0/linux/linux-gpu-cuda11.0-cudnn8-mkl-gcc8.2-avx/paddlepaddle_gpu-2.3.0.post110-cp37-cp37m-linux_x86_64.whl (532.9 MB)\n", "\u001b[K |████████████████████████████████| 532.9 MB 27 kB/s \n", "\u001b[?25hRequirement already satisfied: decorator in /usr/local/lib/python3.7/dist-packages (from paddlepaddle-gpu==2.3.0.post110) (4.4.2)\n", "Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from paddlepaddle-gpu==2.3.0.post110) (7.1.2)\n", "Requirement already satisfied: requests>=2.20.0 in /usr/local/lib/python3.7/dist-packages (from paddlepaddle-gpu==2.3.0.post110) (2.23.0)\n", "Collecting paddle-bfloat==0.1.2\n", " Downloading paddle_bfloat-0.1.2-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (373 kB)\n", "\u001b[K |████████████████████████████████| 373 kB 5.7 MB/s \n", "\u001b[?25hRequirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from paddlepaddle-gpu==2.3.0.post110) (1.15.0)\n", "Requirement already satisfied: protobuf>=3.1.0 in /usr/local/lib/python3.7/dist-packages (from paddlepaddle-gpu==2.3.0.post110) (3.19.6)\n", "Requirement already satisfied: astor in /usr/local/lib/python3.7/dist-packages (from paddlepaddle-gpu==2.3.0.post110) (0.8.1)\n", "Requirement already satisfied: numpy>=1.13 in /usr/local/lib/python3.7/dist-packages (from paddlepaddle-gpu==2.3.0.post110) (1.21.6)\n", "Requirement already satisfied: opt-einsum==3.3.0 in /usr/local/lib/python3.7/dist-packages (from paddlepaddle-gpu==2.3.0.post110) (3.3.0)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20.0->paddlepaddle-gpu==2.3.0.post110) (2022.9.24)\n", "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20.0->paddlepaddle-gpu==2.3.0.post110) (2.10)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20.0->paddlepaddle-gpu==2.3.0.post110) (1.24.3)\n", "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests>=2.20.0->paddlepaddle-gpu==2.3.0.post110) (3.0.4)\n", "Installing collected packages: paddle-bfloat, paddlepaddle-gpu\n", "Successfully installed paddle-bfloat-0.1.2 paddlepaddle-gpu-2.3.0.post110\n" ] } ] }, { "cell_type": "markdown", "metadata": { "id": "dvKiMyj2feUe" }, "source": [ "# pdf2image" ] }, { "cell_type": "markdown", "source": [ "## Installation" ], "metadata": { "id": "aIz56VvlWkf-" } }, { "cell_type": "code", "source": [ "!pip install pdf2image\n", "!apt-get update\n", "!apt-get install poppler-utils" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "biSgyAkWnUD3", "outputId": "06487f1d-40ee-4de6-f072-6372561d16cd" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting pdf2image\n", " Downloading pdf2image-1.16.0-py3-none-any.whl (10 kB)\n", "Requirement already satisfied: pillow in /usr/local/lib/python3.7/dist-packages (from pdf2image) (7.1.2)\n", "Installing collected packages: pdf2image\n", "Successfully installed pdf2image-1.16.0\n", "Get:1 https://cloud.r-project.org/bin/linux/ubuntu bionic-cran40/ InRelease [3,626 B]\n", "Ign:2 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 InRelease\n", "Hit:3 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1804/x86_64 InRelease\n", "Hit:4 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1804/x86_64 Release\n", "Hit:5 http://archive.ubuntu.com/ubuntu bionic InRelease\n", "Get:6 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic InRelease [15.9 kB]\n", "Get:7 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]\n", "Get:8 http://security.ubuntu.com/ubuntu bionic-security InRelease [88.7 kB]\n", "Hit:10 http://ppa.launchpad.net/cran/libgit2/ubuntu bionic InRelease\n", "Get:11 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [83.3 kB]\n", "Hit:12 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu bionic InRelease\n", "Hit:13 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease\n", "Get:14 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [3,494 kB]\n", "Get:15 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main Sources [2,225 kB]\n", "Get:16 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [3,068 kB]\n", "Get:17 http://archive.ubuntu.com/ubuntu bionic-updates/multiverse amd64 Packages [29.8 kB]\n", "Get:18 http://archive.ubuntu.com/ubuntu bionic-updates/restricted amd64 Packages [1,303 kB]\n", "Get:19 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [2,336 kB]\n", "Get:20 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [1,561 kB]\n", "Get:21 http://ppa.launchpad.net/c2d4u.team/c2d4u4.0+/ubuntu bionic/main amd64 Packages [1,138 kB]\n", "Fetched 15.4 MB in 4s (3,448 kB/s)\n", "Reading package lists... Done\n", "Reading package lists... Done\n", "Building dependency tree \n", "Reading state information... Done\n", "The following package was automatically installed and is no longer required:\n", " libnvidia-common-460\n", "Use 'apt autoremove' to remove it.\n", "The following NEW packages will be installed:\n", " poppler-utils\n", "0 upgraded, 1 newly installed, 0 to remove and 7 not upgraded.\n", "Need to get 154 kB of archives.\n", "After this operation, 613 kB of additional disk space will be used.\n", "Get:1 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 poppler-utils amd64 0.62.0-2ubuntu2.14 [154 kB]\n", "Fetched 154 kB in 1s (198 kB/s)\n", "Selecting previously unselected package poppler-utils.\n", "(Reading database ... 123991 files and directories currently installed.)\n", "Preparing to unpack .../poppler-utils_0.62.0-2ubuntu2.14_amd64.deb ...\n", "Unpacking poppler-utils (0.62.0-2ubuntu2.14) ...\n", "Setting up poppler-utils (0.62.0-2ubuntu2.14) ...\n", "Processing triggers for man-db (2.8.3-2ubuntu0.1) ...\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Conversion" ], "metadata": { "id": "pLaguCz1WmF9" } }, { "cell_type": "code", "source": [ "from pdf2image import convert_from_path" ], "metadata": { "id": "HI1dzCGmkeWI" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "images = convert_from_path('/content/bahdanau attention.pdf')" ], "metadata": { "id": "xyhjFgqjkgG7" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "!mkdir pages" ], "metadata": { "id": "M5hBWxpskgJW" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "for i in range(len(images)):\n", " images[i].save('pages/page'+str(i)+'.jpg', 'JPEG')" ], "metadata": { "id": "7mlH9ltkkgMB" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Layout" ], "metadata": { "id": "Urwb7Q_OZC8t" } }, { "cell_type": "markdown", "source": [ "## Installation" ], "metadata": { "id": "vTkwQbF4FZPN" } }, { "cell_type": "code", "source": [ "#!python3 -m pip install paddlepaddle-gpu\n", "!pip install \"paddleocr>=2.0.1\"\n", "!pip install protobuf==3.20.0\n", "!git clone https://github.com/PaddlePaddle/PaddleOCR.git" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/", "height": 1000 }, "id": "R0lRpQLkIpbZ", "outputId": "24283e8e-5de0-4b41-bb43-8d0638f927c1" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting paddleocr>=2.0.1\n", " Downloading paddleocr-2.6.1.0-py3-none-any.whl (409 kB)\n", "\u001b[K |████████████████████████████████| 409 kB 6.1 MB/s \n", "\u001b[?25hRequirement already satisfied: lmdb in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (0.99)\n", "Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (4.64.1)\n", "Collecting python-docx\n", " Downloading python-docx-0.8.11.tar.gz (5.6 MB)\n", "\u001b[K |████████████████████████████████| 5.6 MB 56.9 MB/s \n", "\u001b[?25hCollecting premailer\n", " Downloading premailer-3.10.0-py2.py3-none-any.whl (19 kB)\n", "Collecting PyMuPDF==1.19.0\n", " Downloading PyMuPDF-1.19.0-cp37-cp37m-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (8.7 MB)\n", "\u001b[K |████████████████████████████████| 8.7 MB 19.5 MB/s \n", "\u001b[?25hCollecting lanms-neo==1.0.2\n", " Downloading lanms_neo-1.0.2.tar.gz (39 kB)\n", " Installing build dependencies ... \u001b[?25l\u001b[?25hdone\n", " Getting requirements to build wheel ... \u001b[?25l\u001b[?25hdone\n", " Preparing wheel metadata ... \u001b[?25l\u001b[?25hdone\n", "Requirement already satisfied: lxml in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (4.9.1)\n", "Collecting pyclipper\n", " Downloading pyclipper-1.3.0.post4-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (604 kB)\n", "\u001b[K |████████████████████████████████| 604 kB 62.9 MB/s \n", "\u001b[?25hRequirement already satisfied: scikit-image in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (0.18.3)\n", "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (1.21.6)\n", "Requirement already satisfied: cython in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (0.29.32)\n", "Collecting fire>=0.3.0\n", " Downloading fire-0.4.0.tar.gz (87 kB)\n", "\u001b[K |████████████████████████████████| 87 kB 7.9 MB/s \n", "\u001b[?25hRequirement already satisfied: opencv-contrib-python in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (4.6.0.66)\n", "Requirement already satisfied: opencv-python in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (4.6.0.66)\n", "Requirement already satisfied: openpyxl in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (3.0.10)\n", "Collecting rapidfuzz\n", " Downloading rapidfuzz-2.13.2-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (2.2 MB)\n", "\u001b[K |████████████████████████████████| 2.2 MB 52.5 MB/s \n", "\u001b[?25hCollecting pdf2docx\n", " Downloading pdf2docx-0.5.6-py3-none-any.whl (148 kB)\n", "\u001b[K |████████████████████████████████| 148 kB 68.2 MB/s \n", "\u001b[?25hRequirement already satisfied: beautifulsoup4 in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (4.6.3)\n", "Collecting attrdict\n", " Downloading attrdict-2.0.1-py2.py3-none-any.whl (9.9 kB)\n", "Collecting visualdl\n", " Downloading visualdl-2.4.1-py3-none-any.whl (4.9 MB)\n", "\u001b[K |████████████████████████████████| 4.9 MB 46.1 MB/s \n", "\u001b[?25hCollecting fonttools>=4.24.0\n", " Downloading fonttools-4.38.0-py3-none-any.whl (965 kB)\n", "\u001b[K |████████████████████████████████| 965 kB 57.7 MB/s \n", "\u001b[?25hCollecting Polygon3\n", " Downloading Polygon3-3.0.9.1.tar.gz (39 kB)\n", "Requirement already satisfied: shapely in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (1.8.5.post1)\n", "Requirement already satisfied: imgaug in /usr/local/lib/python3.7/dist-packages (from paddleocr>=2.0.1) (0.4.0)\n", "Requirement already satisfied: six in /usr/local/lib/python3.7/dist-packages (from fire>=0.3.0->paddleocr>=2.0.1) (1.15.0)\n", "Requirement already satisfied: termcolor in /usr/local/lib/python3.7/dist-packages (from fire>=0.3.0->paddleocr>=2.0.1) (2.1.0)\n", "Requirement already satisfied: scipy in /usr/local/lib/python3.7/dist-packages (from imgaug->paddleocr>=2.0.1) (1.7.3)\n", "Requirement already satisfied: matplotlib in /usr/local/lib/python3.7/dist-packages (from imgaug->paddleocr>=2.0.1) (3.2.2)\n", "Requirement already satisfied: imageio in /usr/local/lib/python3.7/dist-packages (from imgaug->paddleocr>=2.0.1) (2.9.0)\n", "Requirement already satisfied: Pillow in /usr/local/lib/python3.7/dist-packages (from imgaug->paddleocr>=2.0.1) (7.1.2)\n", "Requirement already satisfied: tifffile>=2019.7.26 in /usr/local/lib/python3.7/dist-packages (from scikit-image->paddleocr>=2.0.1) (2021.11.2)\n", "Requirement already satisfied: PyWavelets>=1.1.1 in /usr/local/lib/python3.7/dist-packages (from scikit-image->paddleocr>=2.0.1) (1.3.0)\n", "Requirement already satisfied: networkx>=2.0 in /usr/local/lib/python3.7/dist-packages (from scikit-image->paddleocr>=2.0.1) (2.6.3)\n", "Requirement already satisfied: python-dateutil>=2.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->imgaug->paddleocr>=2.0.1) (2.8.2)\n", "Requirement already satisfied: cycler>=0.10 in /usr/local/lib/python3.7/dist-packages (from matplotlib->imgaug->paddleocr>=2.0.1) (0.11.0)\n", "Requirement already satisfied: kiwisolver>=1.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->imgaug->paddleocr>=2.0.1) (1.4.4)\n", "Requirement already satisfied: pyparsing!=2.0.4,!=2.1.2,!=2.1.6,>=2.0.1 in /usr/local/lib/python3.7/dist-packages (from matplotlib->imgaug->paddleocr>=2.0.1) (3.0.9)\n", "Requirement already satisfied: typing-extensions in /usr/local/lib/python3.7/dist-packages (from kiwisolver>=1.0.1->matplotlib->imgaug->paddleocr>=2.0.1) (4.1.1)\n", "Requirement already satisfied: et-xmlfile in /usr/local/lib/python3.7/dist-packages (from openpyxl->paddleocr>=2.0.1) (1.1.0)\n", "Collecting cssutils\n", " Downloading cssutils-2.6.0-py3-none-any.whl (399 kB)\n", "\u001b[K |████████████████████████████████| 399 kB 56.6 MB/s \n", "\u001b[?25hRequirement already satisfied: cachetools in /usr/local/lib/python3.7/dist-packages (from premailer->paddleocr>=2.0.1) (5.2.0)\n", "Collecting cssselect\n", " Downloading cssselect-1.2.0-py2.py3-none-any.whl (18 kB)\n", "Requirement already satisfied: requests in /usr/local/lib/python3.7/dist-packages (from premailer->paddleocr>=2.0.1) (2.23.0)\n", "Requirement already satisfied: importlib-metadata in /usr/local/lib/python3.7/dist-packages (from cssutils->premailer->paddleocr>=2.0.1) (4.13.0)\n", "Requirement already satisfied: zipp>=0.5 in /usr/local/lib/python3.7/dist-packages (from importlib-metadata->cssutils->premailer->paddleocr>=2.0.1) (3.10.0)\n", "Requirement already satisfied: urllib3!=1.25.0,!=1.25.1,<1.26,>=1.21.1 in /usr/local/lib/python3.7/dist-packages (from requests->premailer->paddleocr>=2.0.1) (1.24.3)\n", "Requirement already satisfied: certifi>=2017.4.17 in /usr/local/lib/python3.7/dist-packages (from requests->premailer->paddleocr>=2.0.1) (2022.9.24)\n", "Requirement already satisfied: idna<3,>=2.5 in /usr/local/lib/python3.7/dist-packages (from requests->premailer->paddleocr>=2.0.1) (2.10)\n", "Requirement already satisfied: chardet<4,>=3.0.2 in /usr/local/lib/python3.7/dist-packages (from requests->premailer->paddleocr>=2.0.1) (3.0.4)\n", "Requirement already satisfied: packaging in /usr/local/lib/python3.7/dist-packages (from visualdl->paddleocr>=2.0.1) (21.3)\n", "Collecting Flask-Babel>=1.0.0\n", " Downloading Flask_Babel-2.0.0-py3-none-any.whl (9.3 kB)\n", "Collecting multiprocess\n", " Downloading multiprocess-0.70.14-py37-none-any.whl (115 kB)\n", "\u001b[K |████████████████████████████████| 115 kB 70.8 MB/s \n", "\u001b[?25hRequirement already satisfied: protobuf>=3.11.0 in /usr/local/lib/python3.7/dist-packages (from visualdl->paddleocr>=2.0.1) (3.19.6)\n", "Collecting bce-python-sdk\n", " Downloading bce_python_sdk-0.8.74-py3-none-any.whl (204 kB)\n", "\u001b[K |████████████████████████████████| 204 kB 74.4 MB/s \n", "\u001b[?25hRequirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from visualdl->paddleocr>=2.0.1) (1.3.5)\n", "Requirement already satisfied: flask>=1.1.1 in /usr/local/lib/python3.7/dist-packages (from visualdl->paddleocr>=2.0.1) (1.1.4)\n", "Requirement already satisfied: Jinja2<3.0,>=2.10.1 in /usr/local/lib/python3.7/dist-packages (from flask>=1.1.1->visualdl->paddleocr>=2.0.1) (2.11.3)\n", "Requirement already satisfied: itsdangerous<2.0,>=0.24 in /usr/local/lib/python3.7/dist-packages (from flask>=1.1.1->visualdl->paddleocr>=2.0.1) (1.1.0)\n", "Requirement already satisfied: click<8.0,>=5.1 in /usr/local/lib/python3.7/dist-packages (from flask>=1.1.1->visualdl->paddleocr>=2.0.1) (7.1.2)\n", "Requirement already satisfied: Werkzeug<2.0,>=0.15 in /usr/local/lib/python3.7/dist-packages (from flask>=1.1.1->visualdl->paddleocr>=2.0.1) (1.0.1)\n", "Requirement already satisfied: Babel>=2.3 in /usr/local/lib/python3.7/dist-packages (from Flask-Babel>=1.0.0->visualdl->paddleocr>=2.0.1) (2.11.0)\n", "Requirement already satisfied: pytz in /usr/local/lib/python3.7/dist-packages (from Flask-Babel>=1.0.0->visualdl->paddleocr>=2.0.1) (2022.6)\n", "Requirement already satisfied: MarkupSafe>=0.23 in /usr/local/lib/python3.7/dist-packages (from Jinja2<3.0,>=2.10.1->flask>=1.1.1->visualdl->paddleocr>=2.0.1) (2.0.1)\n", "Requirement already satisfied: future>=0.6.0 in /usr/local/lib/python3.7/dist-packages (from bce-python-sdk->visualdl->paddleocr>=2.0.1) (0.16.0)\n", "Collecting pycryptodome>=3.8.0\n", " Downloading pycryptodome-3.15.0-cp35-abi3-manylinux2010_x86_64.whl (2.3 MB)\n", "\u001b[K |████████████████████████████████| 2.3 MB 52.8 MB/s \n", "\u001b[?25hRequirement already satisfied: dill>=0.3.6 in /usr/local/lib/python3.7/dist-packages (from multiprocess->visualdl->paddleocr>=2.0.1) (0.3.6)\n", "Building wheels for collected packages: lanms-neo, fire, python-docx, Polygon3\n", " Building wheel for lanms-neo (PEP 517) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for lanms-neo: filename=lanms_neo-1.0.2-cp37-cp37m-linux_x86_64.whl size=110709 sha256=db3be5b526b39f2cdb111fae9919810e45d809fbf6483beb4aef28c3577351e1\n", " Stored in directory: /root/.cache/pip/wheels/09/7b/5e/1e4d24a8f94c1116afa284ce2968ef2f72b986a5457164b340\n", " Building wheel for fire (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for fire: filename=fire-0.4.0-py2.py3-none-any.whl size=115940 sha256=6a35e11384be4e128e9cc6c57ba0a7d5a6dd93ab81674fe4d179f29373f6016e\n", " Stored in directory: /root/.cache/pip/wheels/8a/67/fb/2e8a12fa16661b9d5af1f654bd199366799740a85c64981226\n", " Building wheel for python-docx (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for python-docx: filename=python_docx-0.8.11-py3-none-any.whl size=184508 sha256=39fd6da47445e3f73ed3453c6f584faecf60afde06c3276603ebda99353988d8\n", " Stored in directory: /root/.cache/pip/wheels/f6/6f/b9/d798122a8b55b74ad30b5f52b01482169b445fbb84a11797a6\n", " Building wheel for Polygon3 (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for Polygon3: filename=Polygon3-3.0.9.1-cp37-cp37m-linux_x86_64.whl size=102659 sha256=41f609300d5ade5b91d4f4b3bd34ac510e7b2ccfbf1ebd338f17d917ab63cd02\n", " Stored in directory: /root/.cache/pip/wheels/0d/f3/a1/d9909fbad83c438786f3fbde79b0636c9e843107bad74baba7\n", "Successfully built lanms-neo fire python-docx Polygon3\n", "Installing collected packages: pycryptodome, python-docx, PyMuPDF, multiprocess, fonttools, Flask-Babel, fire, cssutils, cssselect, bce-python-sdk, visualdl, rapidfuzz, pyclipper, premailer, Polygon3, pdf2docx, lanms-neo, attrdict, paddleocr\n", "Successfully installed Flask-Babel-2.0.0 Polygon3-3.0.9.1 PyMuPDF-1.19.0 attrdict-2.0.1 bce-python-sdk-0.8.74 cssselect-1.2.0 cssutils-2.6.0 fire-0.4.0 fonttools-4.38.0 lanms-neo-1.0.2 multiprocess-0.70.14 paddleocr-2.6.1.0 pdf2docx-0.5.6 premailer-3.10.0 pyclipper-1.3.0.post4 pycryptodome-3.15.0 python-docx-0.8.11 rapidfuzz-2.13.2 visualdl-2.4.1\n", "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Collecting protobuf==3.20.0\n", " Downloading protobuf-3.20.0-cp37-cp37m-manylinux_2_5_x86_64.manylinux1_x86_64.whl (1.0 MB)\n", "\u001b[K |████████████████████████████████| 1.0 MB 8.3 MB/s \n", "\u001b[?25hInstalling collected packages: protobuf\n", " Attempting uninstall: protobuf\n", " Found existing installation: protobuf 3.19.6\n", " Uninstalling protobuf-3.19.6:\n", " Successfully uninstalled protobuf-3.19.6\n", "\u001b[31mERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.\n", "tensorflow 2.9.2 requires protobuf<3.20,>=3.9.2, but you have protobuf 3.20.0 which is incompatible.\n", "tensorboard 2.9.1 requires protobuf<3.20,>=3.9.2, but you have protobuf 3.20.0 which is incompatible.\n", "google-cloud-translate 3.8.4 requires protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.19.5, but you have protobuf 3.20.0 which is incompatible.\n", "google-cloud-language 2.6.1 requires protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.19.5, but you have protobuf 3.20.0 which is incompatible.\n", "google-cloud-firestore 2.7.2 requires protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.19.5, but you have protobuf 3.20.0 which is incompatible.\n", "google-cloud-datastore 2.9.0 requires protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.19.5, but you have protobuf 3.20.0 which is incompatible.\n", "google-cloud-bigquery 3.3.6 requires protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.19.5, but you have protobuf 3.20.0 which is incompatible.\n", "google-cloud-bigquery-storage 2.16.2 requires protobuf!=3.20.0,!=3.20.1,!=4.21.0,!=4.21.1,!=4.21.2,!=4.21.3,!=4.21.4,!=4.21.5,<5.0.0dev,>=3.19.5, but you have protobuf 3.20.0 which is incompatible.\u001b[0m\n", "Successfully installed protobuf-3.20.0\n" ] }, { "output_type": "display_data", "data": { "application/vnd.colab-display-data+json": { "pip_warning": { "packages": [ "google" ] } } }, "metadata": {} }, { "output_type": "stream", "name": "stdout", "text": [ "Cloning into 'PaddleOCR'...\n", "remote: Enumerating objects: 44967, done.\u001b[K\n", "remote: Counting objects: 100% (98/98), done.\u001b[K\n", "remote: Compressing objects: 100% (75/75), done.\u001b[K\n", "remote: Total 44967 (delta 43), reused 45 (delta 23), pack-reused 44869\u001b[K\n", "Receiving objects: 100% (44967/44967), 338.24 MiB | 33.36 MiB/s, done.\n", "Resolving deltas: 100% (31741/31741), done.\n" ] } ] }, { "cell_type": "code", "source": [ "a=5" ], "metadata": { "id": "IXEKTAa2_10K" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "!wget https://paddleocr.bj.bcebos.com/whl/layoutparser-0.0.0-py3-none-any.whl\n", "!pip install -U layoutparser-0.0.0-py3-none-any.whl" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "8gWFgGl5CXu6", "outputId": "5d435097-2f57-4806-f454-eeeeb1a02efc" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "--2022-11-18 12:58:08-- https://paddleocr.bj.bcebos.com/whl/layoutparser-0.0.0-py3-none-any.whl\n", "Resolving paddleocr.bj.bcebos.com (paddleocr.bj.bcebos.com)... 103.235.46.61, 2409:8c04:1001:1002:0:ff:b001:368a\n", "Connecting to paddleocr.bj.bcebos.com (paddleocr.bj.bcebos.com)|103.235.46.61|:443... connected.\n", "HTTP request sent, awaiting response... 200 OK\n", "Length: 19145360 (18M) [application/octet-stream]\n", "Saving to: ‘layoutparser-0.0.0-py3-none-any.whl’\n", "\n", "layoutparser-0.0.0- 100%[===================>] 18.26M 3.69MB/s in 12s \n", "\n", "2022-11-18 12:58:23 (1.48 MB/s) - ‘layoutparser-0.0.0-py3-none-any.whl’ saved [19145360/19145360]\n", "\n", "Looking in indexes: https://pypi.org/simple, https://us-python.pkg.dev/colab-wheels/public/simple/\n", "Processing ./layoutparser-0.0.0-py3-none-any.whl\n", "Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from layoutparser==0.0.0) (1.21.6)\n", "Requirement already satisfied: pandas in /usr/local/lib/python3.7/dist-packages (from layoutparser==0.0.0) (1.3.5)\n", "Requirement already satisfied: tqdm in /usr/local/lib/python3.7/dist-packages (from layoutparser==0.0.0) (4.64.1)\n", "Collecting iopath\n", " Downloading iopath-0.1.10.tar.gz (42 kB)\n", "\u001b[K |████████████████████████████████| 42 kB 38 kB/s \n", "\u001b[?25hRequirement already satisfied: pillow in /usr/local/lib/python3.7/dist-packages (from layoutparser==0.0.0) (7.1.2)\n", "Requirement already satisfied: pyyaml>=5.1 in /usr/local/lib/python3.7/dist-packages (from layoutparser==0.0.0) (6.0)\n", "Requirement already satisfied: opencv-python in /usr/local/lib/python3.7/dist-packages (from layoutparser==0.0.0) (4.6.0.66)\n", "Requirement already satisfied: typing_extensions in /usr/local/lib/python3.7/dist-packages (from iopath->layoutparser==0.0.0) (4.1.1)\n", "Collecting portalocker\n", " Downloading portalocker-2.6.0-py2.py3-none-any.whl (15 kB)\n", "Requirement already satisfied: pytz>=2017.3 in /usr/local/lib/python3.7/dist-packages (from pandas->layoutparser==0.0.0) (2022.6)\n", "Requirement already satisfied: python-dateutil>=2.7.3 in /usr/local/lib/python3.7/dist-packages (from pandas->layoutparser==0.0.0) (2.8.2)\n", "Requirement already satisfied: six>=1.5 in /usr/local/lib/python3.7/dist-packages (from python-dateutil>=2.7.3->pandas->layoutparser==0.0.0) (1.15.0)\n", "Building wheels for collected packages: iopath\n", " Building wheel for iopath (setup.py) ... \u001b[?25l\u001b[?25hdone\n", " Created wheel for iopath: filename=iopath-0.1.10-py3-none-any.whl size=31548 sha256=8f3da2a0ce4dd2c6b432ef4d5851d960b4c889e015e7066a5a77b45bfa05cb60\n", " Stored in directory: /root/.cache/pip/wheels/aa/cc/ed/ca4e88beef656b01c84b9185196513ef2faf74a5a379b043a7\n", "Successfully built iopath\n", "Installing collected packages: portalocker, iopath, layoutparser\n", "Successfully installed iopath-0.1.10 layoutparser-0.0.0 portalocker-2.6.0\n" ] } ] }, { "cell_type": "markdown", "source": [ "## Table Extraction" ], "metadata": { "id": "w5MA08E0F8aU" } }, { "cell_type": "code", "source": [ "import cv2\n", "import layoutparser as lp\n", "image = cv2.imread(\"/content/pages/page13.jpg\")\n", "\n", "image = image[..., ::-1]\n", "\n", "# load model\n", "model = lp.PaddleDetectionLayoutModel(config_path=\"lp://PubLayNet/ppyolov2_r50vd_dcn_365e_publaynet/config\",\n", " threshold=0.5,\n", " label_map={0: \"Text\", 1: \"Title\", 2: \"List\", 3:\"Table\", 4:\"Figure\"},\n", " enforce_cpu=False,\n", " enable_mkldnn=True)#math kernel library\n", "# detect\n", "layout = model.detect(image)" ], "metadata": { "id": "bw9SFYnMCX0E", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "89218fbd-3b01-453e-a191-0d15c8883d03" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "download https://paddle-model-ecology.bj.bcebos.com/model/layout-parser/ppyolov2_r50vd_dcn_365e_publaynet.tar to /root/.paddledet/inference_model/ppyolov2_r50vd_dcn_365e_publaynet/ppyolov2_r50vd_dcn_365e_publaynet_infer/ppyolov2_r50vd_dcn_365e_publaynet.tar\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "100%|██████████| 221M/221M [00:37<00:00, 5.92MiB/s]\n" ] } ] }, { "cell_type": "code", "source": [ "layout" ], "metadata": { "id": "G76oO2WXCX6v", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "2b0f9ed6-ff8c-4b46-b69c-5e3961d017d4" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "Layout(_blocks=[TextBlock(block=Rectangle(x_1=300.35406494140625, y_1=1716.4278564453125, x_2=1401.105712890625, y_2=1876.4561767578125), text=None, id=None, type=Text, parent=None, next=None, score=0.947794497013092), TextBlock(block=Rectangle(x_1=296.242431640625, y_1=1969.3675537109375, x_2=1405.8653564453125, y_2=2034.4422607421875), text=None, id=None, type=Text, parent=None, next=None, score=0.934190034866333), TextBlock(block=Rectangle(x_1=296.2332763671875, y_1=1460.10986328125, x_2=1401.1175537109375, y_2=1553.036865234375), text=None, id=None, type=Text, parent=None, next=None, score=0.924132227897644), TextBlock(block=Rectangle(x_1=294.98626708984375, y_1=444.1419677734375, x_2=1408.130126953125, y_2=567.1005249023438), text=None, id=None, type=Text, parent=None, next=None, score=0.9196642637252808), TextBlock(block=Rectangle(x_1=300.4600830078125, y_1=1669.076171875, x_2=711.9566040039062, y_2=1697.28076171875), text=None, id=None, type=Title, parent=None, next=None, score=0.8556817770004272), TextBlock(block=Rectangle(x_1=297.86297607421875, y_1=220.03121948242188, x_2=1409.0029296875, y_2=418.0099182128906), text=None, id=None, type=Table, parent=None, next=None, score=0.7598645091056824), TextBlock(block=Rectangle(x_1=298.9613342285156, y_1=1601.4661865234375, x_2=687.694580078125, y_2=1633.7120361328125), text=None, id=None, type=Title, parent=None, next=None, score=0.7429183125495911), TextBlock(block=Rectangle(x_1=295.4989318847656, y_1=1405.8271484375, x_2=551.2619018554688, y_2=1438.658203125), text=None, id=None, type=Title, parent=None, next=None, score=0.6753251552581787), TextBlock(block=Rectangle(x_1=282.51495361328125, y_1=647.5087890625, x_2=1415.5732421875, y_2=1368.4254150390625), text=None, id=None, type=List, parent=None, next=None, score=0.6743354201316833), TextBlock(block=Rectangle(x_1=298.611328125, y_1=1063.1019287109375, x_2=366.89483642578125, y_2=1092.1378173828125), text=None, id=None, type=Text, parent=None, next=None, score=0.6345258951187134), TextBlock(block=Rectangle(x_1=298.71820068359375, y_1=1171.6640625, x_2=998.8455200195312, y_2=1205.39306640625), text=None, id=None, type=Text, parent=None, next=None, score=0.6338083148002625), TextBlock(block=Rectangle(x_1=294.16180419921875, y_1=931.6088256835938, x_2=1402.220458984375, y_2=996.5545043945312), text=None, id=None, type=Text, parent=None, next=None, score=0.5081444382667542)], page_data={})" ] }, "metadata": {}, "execution_count": 13 } ] }, { "cell_type": "code", "source": [ "x_1=0\n", "y_1=0\n", "x_2=0\n", "y_2=0\n", "\n", "for l in layout:\n", " #print(l)\n", " if l.type == 'Table':\n", " x_1 = int(l.block.x_1)\n", " print(l.block.x_1)\n", " y_1 = int(l.block.y_1)\n", " x_2 = int(l.block.x_2)\n", " y_2 = int(l.block.y_2)\n", "\n", " break" ], "metadata": { "id": "y3h0kCz-CX_U", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "5b44d211-694b-409f-df7c-69189d4b448f" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "297.86298\n" ] } ] }, { "cell_type": "code", "source": [ "print(x_1,y_1,x_2,y_2)" ], "metadata": { "id": "cdiYPeCJCYIg", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "4160ed2b-e1d1-4174-dd2e-55bee31167f3" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "297 220 1409 418\n" ] } ] }, { "cell_type": "code", "source": [ "im = cv2.imread('/content/pages/page13.jpg')" ], "metadata": { "id": "F39kJV3hCYLV" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "cv2.imwrite('ext_im.jpg', im[y_1:y_2,x_1:x_2])" ], "metadata": { "id": "EQDXSNijCYPs", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "9c0e6954-da96-491b-efcb-318dc5410561" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 17 } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "7O2P4aIMor_e" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "metadata": { "id": "EGwGhHnd8i_h" }, "source": [ "# Text Detection and Recognition" ] }, { "cell_type": "code", "source": [ "from paddleocr import PaddleOCR, draw_ocr" ], "metadata": { "id": "N6WQZXhLLDWk" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "ocr = PaddleOCR(lang='en')\n", "image_path = '/content/ext_im.jpg'\n", "image_cv = cv2.imread(image_path)\n", "image_height = image_cv.shape[0]\n", "image_width = image_cv.shape[1]\n", "output = ocr.ocr(image_path)[0]" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "A8bCZ9AULDZF", "outputId": "78de4243-7a38-4378-8dfd-063004300535" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "download https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_infer.tar to /root/.paddleocr/whl/det/en/en_PP-OCRv3_det_infer/en_PP-OCRv3_det_infer.tar\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "100%|██████████| 4.00M/4.00M [00:07<00:00, 505kiB/s] \n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "download https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_infer.tar to /root/.paddleocr/whl/rec/en/en_PP-OCRv3_rec_infer/en_PP-OCRv3_rec_infer.tar\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "100%|██████████| 9.96M/9.96M [00:14<00:00, 693kiB/s] \n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "download https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_infer.tar to /root/.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer/ch_ppocr_mobile_v2.0_cls_infer.tar\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "100%|██████████| 2.19M/2.19M [00:11<00:00, 190kiB/s]" ] }, { "output_type": "stream", "name": "stdout", "text": [ "[2022/11/18 13:00:00] ppocr DEBUG: Namespace(alpha=1.0, benchmark=False, beta=1.0, cls_batch_num=6, cls_image_shape='3, 48, 192', cls_model_dir='/root/.paddleocr/whl/cls/ch_ppocr_mobile_v2.0_cls_infer', cls_thresh=0.9, cpu_threads=10, crop_res_save_dir='./output', det=True, det_algorithm='DB', det_box_type='quad', det_db_box_thresh=0.6, det_db_score_mode='fast', det_db_thresh=0.3, det_db_unclip_ratio=1.5, det_east_cover_thresh=0.1, det_east_nms_thresh=0.2, det_east_score_thresh=0.8, det_limit_side_len=960, det_limit_type='max', det_model_dir='/root/.paddleocr/whl/det/en/en_PP-OCRv3_det_infer', det_pse_box_thresh=0.85, det_pse_min_area=16, det_pse_scale=1, det_pse_thresh=0, det_sast_nms_thresh=0.2, det_sast_score_thresh=0.5, draw_img_save_dir='./inference_results', drop_score=0.5, e2e_algorithm='PGNet', e2e_char_dict_path='./ppocr/utils/ic15_dict.txt', e2e_limit_side_len=768, e2e_limit_type='max', e2e_model_dir=None, e2e_pgnet_mode='fast', e2e_pgnet_score_thresh=0.5, e2e_pgnet_valid_set='totaltext', enable_mkldnn=False, fourier_degree=5, gpu_mem=500, help='==SUPPRESS==', image_dir=None, image_orientation=False, ir_optim=True, kie_algorithm='LayoutXLM', label_list=['0', '180'], lang='en', layout=True, layout_dict_path=None, layout_model_dir=None, layout_nms_threshold=0.5, layout_score_threshold=0.5, max_batch_size=10, max_text_length=25, merge_no_span_structure=True, min_subgraph_size=15, mode='structure', ocr=True, ocr_order_method=None, ocr_version='PP-OCRv3', output='./output', page_num=0, precision='fp32', process_id=0, re_model_dir=None, rec=True, rec_algorithm='SVTR_LCNet', rec_batch_num=6, rec_char_dict_path='/usr/local/lib/python3.7/dist-packages/paddleocr/ppocr/utils/en_dict.txt', rec_image_inverse=True, rec_image_shape='3, 48, 320', rec_model_dir='/root/.paddleocr/whl/rec/en/en_PP-OCRv3_rec_infer', recovery=False, save_crop_res=False, save_log_path='./log_output/', scales=[8, 16, 32], ser_dict_path='../train_data/XFUND/class_list_xfun.txt', ser_model_dir=None, show_log=True, sr_batch_num=1, sr_image_shape='3, 32, 128', sr_model_dir=None, structure_version='PP-Structurev2', table=True, table_algorithm='TableAttn', table_char_dict_path=None, table_max_len=488, table_model_dir=None, total_process_num=1, type='ocr', use_angle_cls=False, use_dilation=False, use_gpu=True, use_mp=False, use_npu=False, use_onnx=False, use_pdf2docx_api=False, use_pdserving=False, use_space_char=True, use_tensorrt=False, use_visual_backbone=True, use_xpu=False, vis_font_path='./doc/fonts/simfang.ttf', warmup=False)\n" ] }, { "output_type": "stream", "name": "stderr", "text": [ "\n" ] }, { "output_type": "stream", "name": "stdout", "text": [ "[2022/11/18 13:00:00] ppocr WARNING: Since the angle classifier is not initialized, the angle classifier will not be uesd during the forward process\n", "[2022/11/18 13:00:01] ppocr DEBUG: dt_boxes num : 42, elapse : 0.12288975715637207\n", "[2022/11/18 13:00:01] ppocr DEBUG: rec_res num : 42, elapse : 0.12553930282592773\n" ] } ] }, { "cell_type": "code", "source": [ "print(output)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "VBIDZA0XLDeA", "outputId": "b86018c1-5b24-4897-9174-a34299909bf7" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[[[[693.0, 4.0], [755.0, 4.0], [755.0, 36.0], [693.0, 36.0]], ('GPU', 0.9990124106407166)], [[[70.0, 5.0], [148.0, 5.0], [148.0, 36.0], [70.0, 36.0]], ('Model', 0.9997721910476685)], [[[230.0, 5.0], [394.0, 8.0], [394.0, 37.0], [229.0, 35.0]], ('Updates (105)', 0.9652137756347656)], [[[425.0, 5.0], [511.0, 5.0], [511.0, 36.0], [425.0, 36.0]], ('Epochs', 0.9998965263366699)], [[[537.0, 5.0], [612.0, 5.0], [612.0, 36.0], [537.0, 36.0]], ('Hours', 0.9620624780654907)], [[[833.0, 4.0], [958.0, 4.0], [958.0, 33.0], [833.0, 33.0]], ('Train NLL', 0.9994094371795654)], [[[988.0, 6.0], [1104.0, 6.0], [1104.0, 32.0], [988.0, 32.0]], ('Dev.NLL', 0.9872051477432251)], [[[42.0, 47.0], [177.0, 47.0], [177.0, 72.0], [42.0, 72.0]], ('RNNenc-30', 0.9995856881141663)], [[[285.0, 46.0], [341.0, 46.0], [341.0, 74.0], [285.0, 74.0]], ('8.46', 0.9999933242797852)], [[[446.0, 46.0], [489.0, 46.0], [489.0, 74.0], [446.0, 74.0]], ('6.4', 0.9964627623558044)], [[[553.0, 45.0], [599.0, 45.0], [599.0, 74.0], [553.0, 74.0]], ('109', 0.9999635815620422)], [[[641.0, 47.0], [805.0, 47.0], [805.0, 72.0], [641.0, 72.0]], ('TITAN BLACK', 0.9913126230239868)], [[[869.0, 43.0], [923.0, 43.0], [923.0, 75.0], [869.0, 75.0]], ('28.1', 0.999950110912323)], [[[1019.0, 46.0], [1075.0, 46.0], [1075.0, 74.0], [1019.0, 74.0]], ('53.0', 0.999934196472168)], [[[41.0, 75.0], [177.0, 75.0], [177.0, 101.0], [41.0, 101.0]], ('RNNenc-50', 0.9980602264404297)], [[[285.0, 75.0], [341.0, 75.0], [341.0, 104.0], [285.0, 104.0]], ('6.00', 0.9998406171798706)], [[[446.0, 74.0], [489.0, 74.0], [489.0, 104.0], [446.0, 104.0]], ('4.5', 0.9982914924621582)], [[[553.0, 74.0], [599.0, 74.0], [599.0, 104.0], [553.0, 104.0]], ('108', 0.9998137950897217)], [[[644.0, 78.0], [804.0, 78.0], [804.0, 103.0], [644.0, 103.0]], ('Quadro K-6000', 0.9990899562835693)], [[[868.0, 73.0], [927.0, 73.0], [927.0, 105.0], [868.0, 105.0]], ('44.0', 0.999671459197998)], [[[1017.0, 73.0], [1076.0, 73.0], [1076.0, 105.0], [1017.0, 105.0]], ('43.6', 0.9998911619186401)], [[[27.0, 109.0], [193.0, 109.0], [193.0, 134.0], [27.0, 134.0]], ('RNNsearch-30', 0.9990729689598083)], [[[284.0, 108.0], [338.0, 108.0], [338.0, 136.0], [284.0, 136.0]], ('4.71', 0.9994158744812012)], [[[445.0, 108.0], [489.0, 108.0], [489.0, 136.0], [445.0, 136.0]], ('3.6', 0.9944102168083191)], [[[553.0, 108.0], [599.0, 108.0], [599.0, 136.0], [553.0, 136.0]], ('113', 0.9994351267814636)], [[[643.0, 111.0], [804.0, 111.0], [804.0, 132.0], [643.0, 132.0]], ('TITAN BLACK', 0.9635356664657593)], [[[870.0, 108.0], [923.0, 108.0], [923.0, 136.0], [870.0, 136.0]], ('26.7', 0.9992948770523071)], [[[1018.0, 108.0], [1074.0, 108.0], [1074.0, 136.0], [1018.0, 136.0]], ('47.2', 0.9993252158164978)], [[[25.0, 139.0], [193.0, 139.0], [193.0, 163.0], [25.0, 163.0]], ('RNNsearch-50', 0.9973625540733337)], [[[285.0, 137.0], [339.0, 137.0], [339.0, 166.0], [285.0, 166.0]], ('2.88', 0.9999485015869141)], [[[445.0, 134.0], [489.0, 134.0], [489.0, 168.0], [445.0, 168.0]], ('2.2', 0.9979038238525391)], [[[551.0, 137.0], [598.0, 137.0], [598.0, 166.0], [551.0, 166.0]], ('111', 0.9987512230873108)], [[[643.0, 140.0], [804.0, 140.0], [804.0, 165.0], [643.0, 165.0]], ('Quadro K-6000', 0.9971157908439636)], [[[870.0, 137.0], [924.0, 137.0], [924.0, 166.0], [870.0, 166.0]], ('40.7', 0.9991925954818726)], [[[1018.0, 135.0], [1075.0, 135.0], [1075.0, 167.0], [1018.0, 167.0]], ('38.1', 0.9999594688415527)], [[[21.0, 172.0], [196.0, 172.0], [196.0, 193.0], [21.0, 193.0]], ('RNNsearch-50*', 0.9954861402511597)], [[[285.0, 170.0], [338.0, 170.0], [338.0, 197.0], [285.0, 197.0]], ('6.67', 0.9998753666877747)], [[[446.0, 170.0], [489.0, 170.0], [489.0, 197.0], [446.0, 197.0]], ('5.0', 0.9963653683662415)], [[[551.0, 170.0], [600.0, 170.0], [600.0, 197.0], [551.0, 197.0]], ('252', 0.9991322159767151)], [[[644.0, 172.0], [804.0, 172.0], [804.0, 197.0], [644.0, 197.0]], ('Quadro K-6000', 0.9994592666625977)], [[[870.0, 170.0], [923.0, 170.0], [923.0, 197.0], [870.0, 197.0]], ('36.7', 0.9990472793579102)], [[[1019.0, 170.0], [1074.0, 170.0], [1074.0, 197.0], [1019.0, 197.0]], ('35.2', 0.9986913204193115)]]\n" ] } ] }, { "cell_type": "code", "source": [ "boxes = [line[0] for line in output]\n", "texts = [line[1][0] for line in output]\n", "probabilities = [line[1][1] for line in output]" ], "metadata": { "id": "nNMBAQ78LDgG" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "image_boxes = image_cv.copy()\n" ], "metadata": { "id": "uukcg4SWV_dg" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "for box,text in zip(boxes,texts):\n", " cv2.rectangle(image_boxes, (int(box[0][0]),int(box[0][1])), (int(box[2][0]),int(box[2][1])),(0,0,255),1)\n", " cv2.putText(image_boxes, text,(int(box[0][0]),int(box[0][1])),cv2.FONT_HERSHEY_SIMPLEX,1,(222,0,0),1)" ], "metadata": { "id": "l_HzbiA7V_fw" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "cv2.imwrite('detections.jpg', image_boxes)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "PfUG9mcgV_iJ", "outputId": "b97aaa44-61b9-4a62-dd01-c8c26b66e9c7" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 24 } ] }, { "cell_type": "markdown", "metadata": { "id": "kYWt0lzDHZNp" }, "source": [ "# Reconstruction" ] }, { "cell_type": "markdown", "source": [ "## Get Horizontal and Vertical Lines" ], "metadata": { "id": "ruzifYJz4H6y" } }, { "cell_type": "code", "source": [ "im = image_cv.copy()" ], "metadata": { "id": "YLIoKedcqby_" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "horiz_boxes = []\n", "vert_boxes = []\n", "\n", "for box in boxes:\n", " x_h, x_v = 0,int(box[0][0])\n", " y_h, y_v = int(box[0][1]),0\n", " width_h,width_v = image_width, int(box[2][0]-box[0][0])\n", " height_h,height_v = int(box[2][1]-box[0][1]),image_height\n", "\n", " horiz_boxes.append([x_h,y_h,x_h+width_h,y_h+height_h])\n", " vert_boxes.append([x_v,y_v,x_v+width_v,y_v+height_v])\n", "\n", " cv2.rectangle(im,(x_h,y_h), (x_h+width_h,y_h+height_h),(0,0,255),1)\n", " cv2.rectangle(im,(x_v,y_v), (x_v+width_v,y_v+height_v),(0,255,0),1)\n", "" ], "metadata": { "id": "GwcAAe-wccnF" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "cv2.imwrite('horiz_vert.jpg',im)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "7UxFGhMkccph", "outputId": "344b62aa-8bbf-46e0-ccea-bd1c5eb60e3a" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 27 } ] }, { "cell_type": "markdown", "source": [ "## Non-Max Suppression" ], "metadata": { "id": "ekVFvJrM4ROL" } }, { "cell_type": "code", "source": [ "horiz_out = tf.image.non_max_suppression(\n", " horiz_boxes,\n", " probabilities,\n", " max_output_size = 1000,\n", " iou_threshold=0.1,\n", " score_threshold=float('-inf'),\n", " name=None\n", ")" ], "metadata": { "id": "4LVSSB2fcoe7" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "horiz_lines = np.sort(np.array(horiz_out))\n", "print(horiz_lines)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "pOboYpGnccr2", "outputId": "02933914-32bb-4b3d-88bb-72745b6dda8b" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[ 3 8 20 24 34 36]\n" ] } ] }, { "cell_type": "code", "source": [ "im_nms = image_cv.copy()" ], "metadata": { "id": "pfxHrn3iccyK" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "for val in horiz_lines:\n", " cv2.rectangle(im_nms, (int(horiz_boxes[val][0]),int(horiz_boxes[val][1])), (int(horiz_boxes[val][2]),int(horiz_boxes[val][3])),(0,0,255),1)\n", "" ], "metadata": { "id": "68PCHfmZcc0L" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "cv2.imwrite('im_nms.jpg',im_nms)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "Z8r9qpiAcc2X", "outputId": "9510d3cf-3870-4c77-b9ff-dd903f9a7902" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 32 } ] }, { "cell_type": "code", "source": [ "vert_out = tf.image.non_max_suppression(\n", " vert_boxes,\n", " probabilities,\n", " max_output_size = 1000,\n", " iou_threshold=0.1,\n", " score_threshold=float('-inf'),\n", " name=None\n", ")" ], "metadata": { "id": "mKgPuh7rcc4s" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "print(vert_out)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "GBpKsImVcc6p", "outputId": "f7bfbf04-9318-4230-f9df-98101fc7cadd" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "tf.Tensor([ 8 10 34 12 3 1 39], shape=(7,), dtype=int32)\n" ] } ] }, { "cell_type": "code", "source": [ "vert_lines = np.sort(np.array(vert_out))\n", "print(vert_lines)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "K0lBh-yz5YLp", "outputId": "31a4ea8a-27ce-4b53-9bdb-9857c65ebdaa" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[ 1 3 8 10 12 34 39]\n" ] } ] }, { "cell_type": "code", "source": [ "for val in vert_lines:\n", " cv2.rectangle(im_nms, (int(vert_boxes[val][0]),int(vert_boxes[val][1])), (int(vert_boxes[val][2]),int(vert_boxes[val][3])),(255,0,0),1)\n", "" ], "metadata": { "id": "WqsLm0L_cc84" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "cv2.imwrite('im_nms.jpg',im_nms)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "xZOEa7lpcdGM", "outputId": "a92cbce5-7529-4b3b-f4b5-2c016904ab21" }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "True" ] }, "metadata": {}, "execution_count": 37 } ] }, { "cell_type": "markdown", "source": [ "## Convert to CSV" ], "metadata": { "id": "116eBUrO93-i" } }, { "cell_type": "code", "source": [ "\n", "\n", "out_array = [[\"\" for i in range(len(vert_lines))] for j in range(len(horiz_lines))]\n", "print(np.array(out_array).shape)\n", "print(out_array)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "HRQzwVUTcdIq", "outputId": "93b6914b-c713-48e0-ecaf-87a764a48d13" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "(6, 7)\n", "[['', '', '', '', '', '', ''], ['', '', '', '', '', '', ''], ['', '', '', '', '', '', ''], ['', '', '', '', '', '', ''], ['', '', '', '', '', '', ''], ['', '', '', '', '', '', '']]\n" ] } ] }, { "cell_type": "code", "source": [ "\n", "unordered_boxes = []\n", "\n", "for i in vert_lines:\n", " print(vert_boxes[i])\n", " unordered_boxes.append(vert_boxes[i][0])" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "sSrupaRZIAk_", "outputId": "61055c32-2de2-4a31-c36f-e3857a6cc939" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[70, 0, 148, 198]\n", "[425, 0, 511, 198]\n", "[285, 0, 341, 198]\n", "[553, 0, 599, 198]\n", "[869, 0, 923, 198]\n", "[1018, 0, 1075, 198]\n", "[644, 0, 804, 198]\n" ] } ] }, { "cell_type": "code", "source": [ "ordered_boxes = np.argsort(unordered_boxes)\n", "print(ordered_boxes)" ], "metadata": { "colab": { "base_uri": "https://localhost:8080/" }, "id": "lRMlVNh_HuJV", "outputId": "de3751a5-525d-4659-acbc-00d61fcbeba1" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[0 2 1 3 6 4 5]\n" ] } ] }, { "cell_type": "code", "source": [ "def intersection(box_1, box_2):\n", " return [box_2[0], box_1[1],box_2[2], box_1[3]]" ], "metadata": { "id": "AHHaxKUuC6jQ" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "def iou(box_1, box_2):\n", "\n", " x_1 = max(box_1[0], box_2[0])\n", " y_1 = max(box_1[1], box_2[1])\n", " x_2 = min(box_1[2], box_2[2])\n", " y_2 = min(box_1[3], box_2[3])\n", "\n", " inter = abs(max((x_2 - x_1, 0)) * max((y_2 - y_1), 0))\n", " if inter == 0:\n", " return 0\n", "\n", " box_1_area = abs((box_1[2] - box_1[0]) * (box_1[3] - box_1[1]))\n", " box_2_area = abs((box_2[2] - box_2[0]) * (box_2[3] - box_2[1]))\n", "\n", " return inter / float(box_1_area + box_2_area - inter)" ], "metadata": { "id": "fDVb0DkxJSIf" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "for i in range(len(horiz_lines)):\n", " for j in range(len(vert_lines)):\n", " resultant = intersection(horiz_boxes[horiz_lines[i]], vert_boxes[vert_lines[ordered_boxes[j]]] )\n", "\n", " for b in range(len(boxes)):\n", " the_box = [boxes[b][0][0],boxes[b][0][1],boxes[b][2][0],boxes[b][2][1]]\n", " if(iou(resultant,the_box)>0.1):\n", " out_array[i][j] = texts[b]" ], "metadata": { "id": "LWGhCwg6BIoL" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "out_array=np.array(out_array)" ], "metadata": { "id": "c4tEY9LGNIM9" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [ "out_array" ], "metadata": { "id": "_ekto4-Ymxv2", "outputId": "f7a8e379-2879-4ed0-e8c0-4e127aacc50a", "colab": { "base_uri": "https://localhost:8080/" } }, "execution_count": null, "outputs": [ { "output_type": "execute_result", "data": { "text/plain": [ "array([['Model', 'Updates (105)', 'Epochs', 'Hours', 'GPU', 'Train NLL',\n", " 'Dev.NLL'],\n", " ['RNNenc-30', '8.46', '6.4', '109', 'TITAN BLACK', '28.1', '53.0'],\n", " ['RNNenc-50', '6.00', '4.5', '108', 'Quadro K-6000', '44.0',\n", " '43.6'],\n", " ['RNNsearch-30', '4.71', '3.6', '113', 'TITAN BLACK', '26.7',\n", " '47.2'],\n", " ['RNNsearch-50', '2.88', '2.2', '111', 'Quadro K-6000', '40.7',\n", " '38.1'],\n", " ['RNNsearch-50*', '6.67', '5.0', '252', 'Quadro K-6000', '36.7',\n", " '35.2']], dtype='',current_bank)\n", "cleaned_array=np.array(cleaned_array)\n", "print(cleaned_array)" ], "metadata": { "id": "W_q9e2EkPepQ", "colab": { "base_uri": "https://localhost:8080/" }, "outputId": "fba25f38-1e56-45c3-88fe-2211d38d4dca" }, "execution_count": null, "outputs": [ { "output_type": "stream", "name": "stdout", "text": [ "[['Model' 'Updates (105)' 'Epochs' 'Hours' 'GPU' 'Train NLL' 'Dev.NLL']\n", " ['RNNenc-30' '8.46' '6.4' '109' 'TITAN BLACK' '28.1' '53.0']\n", " ['RNNenc-50' '6.00' '4.5' '108' 'Quadro K-6000' '44.0' '43.6']\n", " ['RNNsearch-30' '4.71' '3.6' '113' 'TITAN BLACK' '26.7' '47.2']\n", " ['RNNsearch-50' '2.88' '2.2' '111' 'Quadro K-6000' '40.7' '38.1']\n", " ['RNNsearch-50*' '6.67' '5.0' '252' 'Quadro K-6000' '36.7' '35.2']]\n" ] } ] }, { "cell_type": "code", "source": [ "pd.DataFrame(cleaned_array).to_csv('cleaned.csv')" ], "metadata": { "id": "RZltyMmD_E68" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Convert to OWL Format" ], "metadata": { "id": "M9rH5mX9Mx7g" } }, { "cell_type": "markdown", "source": [ "# **CSV to Text**" ], "metadata": { "id": "SqFdbM634H5w" } }, { "cell_type": "code", "source": [ "import jpype\n", "import asposecells\n", "\n", "\n", "jpype.startJVM()\n", "from asposecells.api import Workbook\n", "\n", "workbook = Workbook(\"/content/sample.csv\")\n", "workbook.save(\"Output.docx\")\n", "jpype.shutdownJVM()" ], "metadata": { "id": "949GB9T1_Go7", "colab": { "base_uri": "https://localhost:8080/", "height": 373 }, "outputId": "d3bf9a55-8248-459c-8eb6-c2c67e5a84b3" }, "execution_count": null, "outputs": [ { "output_type": "error", "ename": "ModuleNotFoundError", "evalue": "ignored", "traceback": [ "\u001b[0;31m---------------------------------------------------------------------------\u001b[0m", "\u001b[0;31mModuleNotFoundError\u001b[0m Traceback (most recent call last)", "\u001b[0;32m\u001b[0m in \u001b[0;36m\u001b[0;34m\u001b[0m\n\u001b[0;32m----> 1\u001b[0;31m \u001b[0;32mimport\u001b[0m \u001b[0mjpype\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[0m\u001b[1;32m 2\u001b[0m \u001b[0;32mimport\u001b[0m \u001b[0masposecells\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 3\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 4\u001b[0m \u001b[0;34m\u001b[0m\u001b[0m\n\u001b[1;32m 5\u001b[0m \u001b[0mjpype\u001b[0m\u001b[0;34m.\u001b[0m\u001b[0mstartJVM\u001b[0m\u001b[0;34m(\u001b[0m\u001b[0;34m)\u001b[0m\u001b[0;34m\u001b[0m\u001b[0;34m\u001b[0m\u001b[0m\n", "\u001b[0;31mModuleNotFoundError\u001b[0m: No module named 'jpype'", "", "\u001b[0;31m---------------------------------------------------------------------------\u001b[0;32m\nNOTE: If your import is failing due to a missing package, you can\nmanually install dependencies using either !pip or !apt.\n\nTo view examples of installing some common dependencies, click the\n\"Open Examples\" button below.\n\u001b[0;31m---------------------------------------------------------------------------\u001b[0m\n" ], "errorDetails": { "actions": [ { "action": "open_url", "actionText": "Open Examples", "url": "/notebooks/snippets/importing_libraries.ipynb" } ] } } ] }, { "cell_type": "code", "source": [], "metadata": { "id": "eIGJDS7u4DLS" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "ziHBaaCn4DN2" }, "execution_count": null, "outputs": [] }, { "cell_type": "markdown", "source": [ "# Brighten Image (For Anyone dealing with PDFs created from scanned images)" ], "metadata": { "id": "FIzj02CHqce7" } }, { "cell_type": "code", "source": [ "from PIL import Image, ImageEnhance\n", "\n", "#read the image\n", "im = Image.open(\"ext_im.jpg\")\n", "\n", "#image brightness enhancer\n", "enhancer = ImageEnhance.Brightness(im)\n", "\n", "factor = 1 #gives original image\n", "im_output = enhancer.enhance(factor)\n", "im_output.save('ext_im-1.jpg')\n", "\n", "factor = 1.5## brightens the image\n", "im_output = enhancer.enhance(factor)\n", "im_output.save('ext_im-2.jpg')\n" ], "metadata": { "id": "vnzqj5q34DQk" }, "execution_count": null, "outputs": [] }, { "cell_type": "code", "source": [], "metadata": { "id": "arQSfE2br7YT" }, "execution_count": null, "outputs": [] } ], "metadata": { "accelerator": "GPU", "colab": { "collapsed_sections": [ "E693Ela3qhLx", "SqFdbM634H5w", "FIzj02CHqce7" ], "provenance": [] }, "kernelspec": { "display_name": "Python 3", "name": "python3" }, "language_info": { "name": "python" } }, "nbformat": 4, "nbformat_minor": 0 }