├── LICENSE ├── OCR_Tesseract_Python.ipynb └── README.md /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2018 Ritesh Maurya 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /OCR_Tesseract_Python.ipynb: -------------------------------------------------------------------------------- 1 | { 2 | "nbformat": 4, 3 | "nbformat_minor": 0, 4 | "metadata": { 5 | "colab": { 6 | "name": "OCR-Tesseract-Python.ipynb", 7 | "version": "0.3.2", 8 | "provenance": [], 9 | "collapsed_sections": [ 10 | "0GHxCcNS3EhN", 11 | "qzkn3o9H3QIc" 12 | ] 13 | }, 14 | "kernelspec": { 15 | "name": "python3", 16 | "display_name": "Python 3" 17 | }, 18 | "accelerator": "GPU" 19 | }, 20 | "cells": [ 21 | { 22 | "metadata": { 23 | "id": "Eu9Yihr34MUj", 24 | "colab_type": "text" 25 | }, 26 | "cell_type": "markdown", 27 | "source": [ 28 | "#Introduction\n", 29 | "**The aim of this NoteBook is to be able to recognise text from an image file using the Tesseract Library in the Python Programming Language.**\n", 30 | "\n", 31 | "**Tesseract is an Open Source library for Optical Character recognition(OCR). We will be using PyTesseract to print the recognized text given an input image of any of the following formats\n", 32 | ": jpeg, png, gif, bmp, tiff, and others. **\n" 33 | ] 34 | }, 35 | { 36 | "metadata": { 37 | "id": "4-yKpihT25p8", 38 | "colab_type": "text" 39 | }, 40 | "cell_type": "markdown", 41 | "source": [ 42 | "# Installation" 43 | ] 44 | }, 45 | { 46 | "metadata": { 47 | "id": "Iup9C5Lon-g0", 48 | "colab_type": "code", 49 | "outputId": "f1b46343-59be-4bc8-f762-2df08b207ac8", 50 | "colab": { 51 | "base_uri": "https://localhost:8080/", 52 | "height": 493 53 | } 54 | }, 55 | "cell_type": "code", 56 | "source": [ 57 | "!sudo add-apt-repository ppa:alex-p/tesseract-ocr" 58 | ], 59 | "execution_count": 0, 60 | "outputs": [ 61 | { 62 | "output_type": "stream", 63 | "text": [ 64 | " The Tesseract OCR engine was one of the top 3 engines in the 1995\n", 65 | " UNLV Accuracy test. Between 1995 and 2006 it had little work done on\n", 66 | " it, but since then it has been improved extensively by Google and is\n", 67 | " probably one of the most accurate open source OCR engines\n", 68 | " available. It can read a wide variety of image formats and convert\n", 69 | " them to text in over 40 languages. This package includes the command\n", 70 | " line tool.\n", 71 | " More info: https://launchpad.net/~alex-p/+archive/ubuntu/tesseract-ocr\n", 72 | "Press [ENTER] to continue or Ctrl-c to cancel adding it.\n", 73 | "\n", 74 | "Ign:1 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64 InRelease\n", 75 | "Get:2 http://security.ubuntu.com/ubuntu bionic-security InRelease [83.2 kB]\n", 76 | "Hit:3 http://archive.ubuntu.com/ubuntu bionic InRelease\n", 77 | "Get:4 http://ppa.launchpad.net/alex-p/tesseract-ocr/ubuntu bionic InRelease [15.4 kB]\n", 78 | "Ign:5 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64 InRelease\n", 79 | "Hit:6 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64 Release\n", 80 | "Hit:7 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64 Release\n", 81 | "Get:8 http://archive.ubuntu.com/ubuntu bionic-updates InRelease [88.7 kB]\n", 82 | "Get:9 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease [21.3 kB]\n", 83 | "Get:11 http://archive.ubuntu.com/ubuntu bionic-backports InRelease [74.6 kB]\n", 84 | "Get:13 http://ppa.launchpad.net/alex-p/tesseract-ocr/ubuntu bionic/main amd64 Packages [26.4 kB]\n", 85 | "Get:14 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages [300 kB]\n", 86 | "Get:15 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic/main amd64 Packages [27.2 kB]\n", 87 | "Get:16 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages [618 kB]\n", 88 | "Get:17 http://security.ubuntu.com/ubuntu bionic-security/universe amd64 Packages [135 kB]\n", 89 | "Get:18 http://archive.ubuntu.com/ubuntu bionic-updates/universe amd64 Packages [900 kB]\n", 90 | "Fetched 2,290 kB in 2s (1,341 kB/s)\n", 91 | "Reading package lists... Done\n" 92 | ], 93 | "name": "stdout" 94 | } 95 | ] 96 | }, 97 | { 98 | "metadata": { 99 | "id": "RxADPcEA39z-", 100 | "colab_type": "code", 101 | "outputId": "d9cc72fc-93f9-43aa-9411-ba5184fa5fd1", 102 | "colab": { 103 | "base_uri": "https://localhost:8080/", 104 | "height": 204 105 | } 106 | }, 107 | "cell_type": "code", 108 | "source": [ 109 | "!sudo apt-get update" 110 | ], 111 | "execution_count": 0, 112 | "outputs": [ 113 | { 114 | "output_type": "stream", 115 | "text": [ 116 | "\r0% [Working]\r \rIgn:1 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64 InRelease\n", 117 | "\r0% [Waiting for headers] [Waiting for headers] [Waiting for headers] [Waiting f\r \rIgn:2 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64 InRelease\n", 118 | "\r \r0% [Waiting for headers] [Waiting for headers] [Waiting for headers]\r \rHit:3 https://developer.download.nvidia.com/compute/cuda/repos/ubuntu1710/x86_64 Release\n", 119 | "\r0% [Waiting for headers] [Waiting for headers] [Waiting for headers]\r \rHit:4 http://archive.ubuntu.com/ubuntu bionic InRelease\n", 120 | "\r \rHit:5 http://security.ubuntu.com/ubuntu bionic-security InRelease\n", 121 | "\r \rHit:6 http://ppa.launchpad.net/alex-p/tesseract-ocr/ubuntu bionic InRelease\n", 122 | "\r0% [Waiting for headers] [Connecting to ppa.launchpad.net (91.189.95.83)]\r0% [Release.gpg gpgv 564 B] [Waiting for headers] [Connecting to ppa.launchpad.\r \rHit:7 https://developer.download.nvidia.com/compute/machine-learning/repos/ubuntu1604/x86_64 Release\n", 123 | "\r0% [Release.gpg gpgv 564 B] [Waiting for headers] [Connecting to ppa.launchpad.\r \r0% [Waiting for headers] [Waiting for headers]\r \rHit:9 http://archive.ubuntu.com/ubuntu bionic-updates InRelease\n", 124 | "\r0% [Waiting for headers] [Waiting for headers]\r0% [4 InRelease gpgv 242 kB] [Waiting for headers] [Waiting for headers]\r \rHit:10 http://ppa.launchpad.net/graphics-drivers/ppa/ubuntu bionic InRelease\n", 125 | "Hit:11 http://archive.ubuntu.com/ubuntu bionic-backports InRelease\n", 126 | "Reading package lists... Done\n" 127 | ], 128 | "name": "stdout" 129 | } 130 | ] 131 | }, 132 | { 133 | "metadata": { 134 | "id": "bOQf0ryh3_8d", 135 | "colab_type": "code", 136 | "outputId": "f35e64cf-dfa1-4c39-c7a4-1696e118b0bf", 137 | "colab": { 138 | "base_uri": "https://localhost:8080/", 139 | "height": 632 140 | } 141 | }, 142 | "cell_type": "code", 143 | "source": [ 144 | "!sudo apt install tesseract-ocr" 145 | ], 146 | "execution_count": 0, 147 | "outputs": [ 148 | { 149 | "output_type": "stream", 150 | "text": [ 151 | "Reading package lists... Done\n", 152 | "Building dependency tree \n", 153 | "Reading state information... Done\n", 154 | "The following additional packages will be installed:\n", 155 | " tesseract-ocr-eng tesseract-ocr-osd\n", 156 | "The following NEW packages will be installed:\n", 157 | " tesseract-ocr tesseract-ocr-eng tesseract-ocr-osd\n", 158 | "0 upgraded, 3 newly installed, 0 to remove and 14 not upgraded.\n", 159 | "Need to get 4,843 kB of archives.\n", 160 | "After this operation, 15.8 MB of additional disk space will be used.\n", 161 | "Get:1 http://ppa.launchpad.net/alex-p/tesseract-ocr/ubuntu bionic/main amd64 tesseract-ocr-eng all 1:4.00~git30-7274cfa-1ppa1~bionic1 [1,592 kB]\n", 162 | "Get:2 http://ppa.launchpad.net/alex-p/tesseract-ocr/ubuntu bionic/main amd64 tesseract-ocr-osd all 1:4.00~git30-7274cfa-1ppa1~bionic1 [2,991 kB]\n", 163 | "Get:3 http://ppa.launchpad.net/alex-p/tesseract-ocr/ubuntu bionic/main amd64 tesseract-ocr amd64 4.0.0+git3360-e3a39c35-1ppa1~bionic1 [259 kB]\n", 164 | "Fetched 4,843 kB in 8s (588 kB/s)\n", 165 | "debconf: unable to initialize frontend: Dialog\n", 166 | "debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76, <> line 3.)\n", 167 | "debconf: falling back to frontend: Readline\n", 168 | "debconf: unable to initialize frontend: Readline\n", 169 | "debconf: (This frontend requires a controlling tty.)\n", 170 | "debconf: falling back to frontend: Teletype\n", 171 | "dpkg-preconfigure: unable to re-open stdin: \n", 172 | "Selecting previously unselected package tesseract-ocr-eng.\n", 173 | "(Reading database ... 110842 files and directories currently installed.)\n", 174 | "Preparing to unpack .../tesseract-ocr-eng_1%3a4.00~git30-7274cfa-1ppa1~bionic1_all.deb ...\n", 175 | "Unpacking tesseract-ocr-eng (1:4.00~git30-7274cfa-1ppa1~bionic1) ...\n", 176 | "Selecting previously unselected package tesseract-ocr-osd.\n", 177 | "Preparing to unpack .../tesseract-ocr-osd_1%3a4.00~git30-7274cfa-1ppa1~bionic1_all.deb ...\n", 178 | "Unpacking tesseract-ocr-osd (1:4.00~git30-7274cfa-1ppa1~bionic1) ...\n", 179 | "Selecting previously unselected package tesseract-ocr.\n", 180 | "Preparing to unpack .../tesseract-ocr_4.0.0+git3360-e3a39c35-1ppa1~bionic1_amd64.deb ...\n", 181 | "Unpacking tesseract-ocr (4.0.0+git3360-e3a39c35-1ppa1~bionic1) ...\n", 182 | "Setting up tesseract-ocr-osd (1:4.00~git30-7274cfa-1ppa1~bionic1) ...\n", 183 | "Setting up tesseract-ocr-eng (1:4.00~git30-7274cfa-1ppa1~bionic1) ...\n", 184 | "Processing triggers for man-db (2.8.3-2ubuntu0.1) ...\n", 185 | "Setting up tesseract-ocr (4.0.0+git3360-e3a39c35-1ppa1~bionic1) ...\n" 186 | ], 187 | "name": "stdout" 188 | } 189 | ] 190 | }, 191 | { 192 | "metadata": { 193 | "id": "qUkfdeTq4Bta", 194 | "colab_type": "code", 195 | "outputId": "a81a632c-6b48-4112-fa58-21ec0bf1e1f1", 196 | "colab": { 197 | "base_uri": "https://localhost:8080/", 198 | "height": 734 199 | } 200 | }, 201 | "cell_type": "code", 202 | "source": [ 203 | "!sudo apt install libtesseract-dev" 204 | ], 205 | "execution_count": 0, 206 | "outputs": [ 207 | { 208 | "output_type": "stream", 209 | "text": [ 210 | "Reading package lists... Done\n", 211 | "Building dependency tree \n", 212 | "Reading state information... Done\n", 213 | "The following additional packages will be installed:\n", 214 | " liblept5 libleptonica-dev libtesseract4\n", 215 | "The following NEW packages will be installed:\n", 216 | " libleptonica-dev libtesseract-dev\n", 217 | "The following packages will be upgraded:\n", 218 | " liblept5 libtesseract4\n", 219 | "2 upgraded, 2 newly installed, 0 to remove and 12 not upgraded.\n", 220 | "Need to get 4,916 kB of archives.\n", 221 | "After this operation, 13.7 MB of additional disk space will be used.\n", 222 | "Get:1 http://ppa.launchpad.net/alex-p/tesseract-ocr/ubuntu bionic/main amd64 liblept5 amd64 1.76.0-1+nmu1ppa1~bionic1 [933 kB]\n", 223 | "Get:2 http://ppa.launchpad.net/alex-p/tesseract-ocr/ubuntu bionic/main amd64 libleptonica-dev amd64 1.76.0-1+nmu1ppa1~bionic1 [1,321 kB]\n", 224 | "Get:3 http://ppa.launchpad.net/alex-p/tesseract-ocr/ubuntu bionic/main amd64 libtesseract4 amd64 4.0.0+git3360-e3a39c35-1ppa1~bionic1 [1,209 kB]\n", 225 | "Get:4 http://ppa.launchpad.net/alex-p/tesseract-ocr/ubuntu bionic/main amd64 libtesseract-dev amd64 4.0.0+git3360-e3a39c35-1ppa1~bionic1 [1,453 kB]\n", 226 | "Fetched 4,916 kB in 8s (611 kB/s)\n", 227 | "debconf: unable to initialize frontend: Dialog\n", 228 | "debconf: (No usable dialog-like program is installed, so the dialog based frontend cannot be used. at /usr/share/perl5/Debconf/FrontEnd/Dialog.pm line 76, <> line 4.)\n", 229 | "debconf: falling back to frontend: Readline\n", 230 | "debconf: unable to initialize frontend: Readline\n", 231 | "debconf: (This frontend requires a controlling tty.)\n", 232 | "debconf: falling back to frontend: Teletype\n", 233 | "dpkg-preconfigure: unable to re-open stdin: \n", 234 | "(Reading database ... 110889 files and directories currently installed.)\n", 235 | "Preparing to unpack .../liblept5_1.76.0-1+nmu1ppa1~bionic1_amd64.deb ...\n", 236 | "Unpacking liblept5 (1.76.0-1+nmu1ppa1~bionic1) over (1.75.3-3) ...\n", 237 | "Selecting previously unselected package libleptonica-dev.\n", 238 | "Preparing to unpack .../libleptonica-dev_1.76.0-1+nmu1ppa1~bionic1_amd64.deb ...\n", 239 | "Unpacking libleptonica-dev (1.76.0-1+nmu1ppa1~bionic1) ...\n", 240 | "Preparing to unpack .../libtesseract4_4.0.0+git3360-e3a39c35-1ppa1~bionic1_amd64.deb ...\n", 241 | "Unpacking libtesseract4:amd64 (4.0.0+git3360-e3a39c35-1ppa1~bionic1) over (4.00~git2288-10f4998a-2) ...\n", 242 | "Selecting previously unselected package libtesseract-dev:amd64.\n", 243 | "Preparing to unpack .../libtesseract-dev_4.0.0+git3360-e3a39c35-1ppa1~bionic1_amd64.deb ...\n", 244 | "Unpacking libtesseract-dev:amd64 (4.0.0+git3360-e3a39c35-1ppa1~bionic1) ...\n", 245 | "Setting up liblept5 (1.76.0-1+nmu1ppa1~bionic1) ...\n", 246 | "Processing triggers for libc-bin (2.27-3ubuntu1) ...\n", 247 | "Setting up libleptonica-dev (1.76.0-1+nmu1ppa1~bionic1) ...\n", 248 | "Setting up libtesseract4:amd64 (4.0.0+git3360-e3a39c35-1ppa1~bionic1) ...\n", 249 | "Setting up libtesseract-dev:amd64 (4.0.0+git3360-e3a39c35-1ppa1~bionic1) ...\n", 250 | "Processing triggers for libc-bin (2.27-3ubuntu1) ...\n" 251 | ], 252 | "name": "stdout" 253 | } 254 | ] 255 | }, 256 | { 257 | "metadata": { 258 | "id": "8gSJiO8E4DT6", 259 | "colab_type": "code", 260 | "outputId": "ebab2cf1-528d-45ff-ef0f-e7e90d3bc28c", 261 | "colab": { 262 | "base_uri": "https://localhost:8080/", 263 | "height": 204 264 | } 265 | }, 266 | "cell_type": "code", 267 | "source": [ 268 | "!sudo pip install pytesseract" 269 | ], 270 | "execution_count": 0, 271 | "outputs": [ 272 | { 273 | "output_type": "stream", 274 | "text": [ 275 | "Collecting pytesseract\n", 276 | "\u001b[?25l Downloading https://files.pythonhosted.org/packages/71/5a/d7600cad26276d991feecb27f3627ae2d0ee89aa1e3065fa4f9f1f2defbc/pytesseract-0.2.6.tar.gz (169kB)\n", 277 | "\u001b[K 100% |████████████████████████████████| 174kB 8.1MB/s \n", 278 | "\u001b[?25hRequirement already satisfied: Pillow in /usr/local/lib/python3.6/dist-packages (from pytesseract) (4.0.0)\n", 279 | "Requirement already satisfied: olefile in /usr/local/lib/python3.6/dist-packages (from Pillow->pytesseract) (0.46)\n", 280 | "Building wheels for collected packages: pytesseract\n", 281 | " Running setup.py bdist_wheel for pytesseract ... \u001b[?25l-\b \bdone\n", 282 | "\u001b[?25h Stored in directory: /root/.cache/pip/wheels/d5/90/56/ab7b652592da86821293f7cadc1c554aa376a0d57ce414d0a0\n", 283 | "Successfully built pytesseract\n", 284 | "Installing collected packages: pytesseract\n", 285 | "Successfully installed pytesseract-0.2.6\n" 286 | ], 287 | "name": "stdout" 288 | } 289 | ] 290 | }, 291 | { 292 | "metadata": { 293 | "id": "4DmjENFFpd9w", 294 | "colab_type": "code", 295 | "outputId": "3222ecd7-6a6b-461a-ff74-27c37ecd22b4", 296 | "colab": { 297 | "base_uri": "https://localhost:8080/", 298 | "height": 119 299 | } 300 | }, 301 | "cell_type": "code", 302 | "source": [ 303 | "#Checking the installation.\n", 304 | "!tesseract --version" 305 | ], 306 | "execution_count": 0, 307 | "outputs": [ 308 | { 309 | "output_type": "stream", 310 | "text": [ 311 | "tesseract 4.0.0-115-ge3a3\n", 312 | " leptonica-1.76.0\n", 313 | " libgif 5.1.4 : libjpeg 8d (libjpeg-turbo 1.5.2) : libpng 1.6.34 : libtiff 4.0.9 : zlib 1.2.11 : libwebp 0.6.1 : libopenjp2 2.3.0\n", 314 | " Found AVX2\n", 315 | " Found AVX\n", 316 | " Found SSE\n" 317 | ], 318 | "name": "stdout" 319 | } 320 | ] 321 | }, 322 | { 323 | "metadata": { 324 | "id": "0GHxCcNS3EhN", 325 | "colab_type": "text" 326 | }, 327 | "cell_type": "markdown", 328 | "source": [ 329 | "# Image Downloader" 330 | ] 331 | }, 332 | { 333 | "metadata": { 334 | "id": "OUU2ixCpva_L", 335 | "colab_type": "code", 336 | "outputId": "cedc9c81-940e-44f9-a407-b906bc0ac677", 337 | "colab": { 338 | "base_uri": "https://localhost:8080/", 339 | "height": 51 340 | } 341 | }, 342 | "cell_type": "code", 343 | "source": [ 344 | "#Image Downloader(From previous video.)\n", 345 | "import requests\n", 346 | "\n", 347 | "print ('Starting to Download!')\n", 348 | "\n", 349 | "url = 'http://zone1-af2a.kxcdn.com/wp-content/uploads/life-quotes-that-will-change-you-forever-wisdom-quotes.jpg'\n", 350 | "r = requests.get(url)\n", 351 | "\n", 352 | "filename = '2.jpg'\n", 353 | "\n", 354 | "with open(filename, 'wb') as out_file:\n", 355 | " out_file.write(r.content)\n", 356 | "\n", 357 | "print(\"Download complete!\")" 358 | ], 359 | "execution_count": 0, 360 | "outputs": [ 361 | { 362 | "output_type": "stream", 363 | "text": [ 364 | "Starting to Download!\n", 365 | "Download complete!\n" 366 | ], 367 | "name": "stdout" 368 | } 369 | ] 370 | }, 371 | { 372 | "metadata": { 373 | "id": "dvyFYwR7zZ59", 374 | "colab_type": "text" 375 | }, 376 | "cell_type": "markdown", 377 | "source": [ 378 | "#TESTING 5 Images\n", 379 | "\n", 380 | "\n", 381 | "---\n", 382 | "\n", 383 | "\n", 384 | "#IMAGE 1\n", 385 | " \n", 386 | " ![alt text](https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcT6z5vTpAMfE1xjxV7rrZ726lWoMARenCftjVxH4_HPEykFfAiK)\n", 387 | "\n", 388 | "\n", 389 | "---\n", 390 | "\n", 391 | "\n", 392 | "#IMAGE 2\n", 393 | "\n", 394 | "![alt text](http://pun.me/pages/funny-quote-about-life.jpg)\n", 395 | "\n", 396 | "---\n", 397 | "\n", 398 | "#IMAGE 3\n", 399 | "\n", 400 | "![alt text](https://i.pinimg.com/originals/38/0b/87/380b87f345c48637cee8ff05d872fd60.png)\n", 401 | "\n", 402 | "---\n", 403 | "\n", 404 | "#IMAGE 4\n", 405 | "![alt text](https://i.pinimg.com/originals/5a/bc/0f/5abc0feb32c88c9626afb7f8aedaea2f.jpg)\n", 406 | "\n", 407 | "---\n", 408 | "\n", 409 | "\n", 410 | "#IMAGE 5\n", 411 | "![alt text](http://quotesideas.com/wp-content/uploads/2015/03/nice-motivational-inspirational-quotes-thoughts-achieves-possible-great-best.jpg)\n", 412 | "\n", 413 | "---\n" 414 | ] 415 | }, 416 | { 417 | "metadata": { 418 | "id": "qzkn3o9H3QIc", 419 | "colab_type": "text" 420 | }, 421 | "cell_type": "markdown", 422 | "source": [ 423 | "# Final Code" 424 | ] 425 | }, 426 | { 427 | "metadata": { 428 | "id": "jTrX8LtltFgX", 429 | "colab_type": "code", 430 | "outputId": "f3021430-383c-4e04-fd02-7a058359c91a", 431 | "colab": { 432 | "base_uri": "https://localhost:8080/", 433 | "height": 102 434 | } 435 | }, 436 | "cell_type": "code", 437 | "source": [ 438 | "import cv2\n", 439 | "import numpy as np\n", 440 | "import pytesseract\n", 441 | "from PIL import Image\n", 442 | "\n", 443 | "# Path of working folder on Disk\n", 444 | "\n", 445 | "def get_string(img_path):\n", 446 | " # Read image with opencv\n", 447 | " img = cv2.imread(img_path)\n", 448 | "\n", 449 | " # Convert to gray\n", 450 | " img = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)\n", 451 | "\n", 452 | " # Apply dilation and erosion to remove some noise\n", 453 | " kernel = np.ones((1, 1), np.uint8)\n", 454 | " img = cv2.dilate(img, kernel, iterations=1)\n", 455 | " img = cv2.erode(img, kernel, iterations=1)\n", 456 | "\n", 457 | " # Write image after removed noise\n", 458 | " cv2.imwrite(\"removed_noise.png\", img)\n", 459 | "\n", 460 | " # Apply threshold to get image with only black and white\n", 461 | " #img = cv2.adaptiveThreshold(img, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C, cv2.THRESH_BINARY, 31, 2)\n", 462 | "\n", 463 | " # Write the image after apply opencv to do some ...\n", 464 | " cv2.imwrite(img_path, img)\n", 465 | "\n", 466 | " # Recognize text with tesseract for python\n", 467 | " result = pytesseract.image_to_string(Image.open(img_path))\n", 468 | "\n", 469 | " # Remove template file\n", 470 | " #os.remove(temp)\n", 471 | "\n", 472 | " return result\n", 473 | "\n", 474 | "\n", 475 | "print ('--- Start recognize text from image ---')\n", 476 | "print (get_string(filename))\n", 477 | "\n", 478 | "print (\"------ Done -------\")" 479 | ], 480 | "execution_count": 0, 481 | "outputs": [ 482 | { 483 | "output_type": "stream", 484 | "text": [ 485 | " " 486 | ], 487 | "name": "stdout" 488 | } 489 | ] 490 | } 491 | ] 492 | } 493 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # OCR-Python 2 | This is the CODE by RItesh Kumar Maurya for [this video](https://youtu.be/fn7A50rBtD0) on Youtube. 3 | 4 | 5 | **Introduction:** 6 | The aim of this Repository is to be able to recognise text from an image file using the Tesseract Library in the Python Programming Language. 7 | 8 | Tesseract is an Open Source library for Optical Character recognition(OCR). We will be using PyTesseract to print the recognized text given an input image of any of the following formats : jpeg, png, gif, bmp, tiff, and others. 9 | 10 | 11 | **SETUP:** 12 | Every detailed Step by Step process is given in the Python NoteBook and explained in [this video](https://youtu.be/fn7A50rBtD0). 13 | 14 | HOPE this Repository helped you guys. Please do STAR and FORK the repository and don't forget to follow me on [GitHub](https://github.com/MauryaRitesh) as well as on [YouTube](https://www.youtube.com/RiteshKumarMaurya). 15 | --------------------------------------------------------------------------------