Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Text extractor #878

Merged
merged 2 commits into from
Nov 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
SEE
LIGHT
In THE
DARKNESS
277 changes: 277 additions & 0 deletions Computer Vision/Text_Extractor/EasyOCR/Easy_OCR_code.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,277 @@
{
"cells": [
{
"cell_type": "code",
"source": [
"!pip install easyocr"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"collapsed": true,
"id": "lsSJ-LCbo2w-",
"outputId": "8c6ff71f-2692-4e10-961d-497001938cc3"
},
"execution_count": 1,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Collecting easyocr\n",
" Downloading easyocr-1.7.2-py3-none-any.whl.metadata (10 kB)\n",
"Requirement already satisfied: torch in /usr/local/lib/python3.10/dist-packages (from easyocr) (2.5.0+cu121)\n",
"Requirement already satisfied: torchvision>=0.5 in /usr/local/lib/python3.10/dist-packages (from easyocr) (0.20.0+cu121)\n",
"Requirement already satisfied: opencv-python-headless in /usr/local/lib/python3.10/dist-packages (from easyocr) (4.10.0.84)\n",
"Requirement already satisfied: scipy in /usr/local/lib/python3.10/dist-packages (from easyocr) (1.13.1)\n",
"Requirement already satisfied: numpy in /usr/local/lib/python3.10/dist-packages (from easyocr) (1.26.4)\n",
"Requirement already satisfied: Pillow in /usr/local/lib/python3.10/dist-packages (from easyocr) (10.4.0)\n",
"Requirement already satisfied: scikit-image in /usr/local/lib/python3.10/dist-packages (from easyocr) (0.24.0)\n",
"Collecting python-bidi (from easyocr)\n",
" Downloading python_bidi-0.6.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl.metadata (4.9 kB)\n",
"Requirement already satisfied: PyYAML in /usr/local/lib/python3.10/dist-packages (from easyocr) (6.0.2)\n",
"Requirement already satisfied: Shapely in /usr/local/lib/python3.10/dist-packages (from easyocr) (2.0.6)\n",
"Collecting pyclipper (from easyocr)\n",
" Downloading pyclipper-1.3.0.post6-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl.metadata (9.0 kB)\n",
"Collecting ninja (from easyocr)\n",
" Downloading ninja-1.11.1.1-py2.py3-none-manylinux1_x86_64.manylinux_2_5_x86_64.whl.metadata (5.3 kB)\n",
"Requirement already satisfied: filelock in /usr/local/lib/python3.10/dist-packages (from torch->easyocr) (3.16.1)\n",
"Requirement already satisfied: typing-extensions>=4.8.0 in /usr/local/lib/python3.10/dist-packages (from torch->easyocr) (4.12.2)\n",
"Requirement already satisfied: networkx in /usr/local/lib/python3.10/dist-packages (from torch->easyocr) (3.4.2)\n",
"Requirement already satisfied: jinja2 in /usr/local/lib/python3.10/dist-packages (from torch->easyocr) (3.1.4)\n",
"Requirement already satisfied: fsspec in /usr/local/lib/python3.10/dist-packages (from torch->easyocr) (2024.10.0)\n",
"Requirement already satisfied: sympy==1.13.1 in /usr/local/lib/python3.10/dist-packages (from torch->easyocr) (1.13.1)\n",
"Requirement already satisfied: mpmath<1.4,>=1.1.0 in /usr/local/lib/python3.10/dist-packages (from sympy==1.13.1->torch->easyocr) (1.3.0)\n",
"Requirement already satisfied: imageio>=2.33 in /usr/local/lib/python3.10/dist-packages (from scikit-image->easyocr) (2.36.0)\n",
"Requirement already satisfied: tifffile>=2022.8.12 in /usr/local/lib/python3.10/dist-packages (from scikit-image->easyocr) (2024.9.20)\n",
"Requirement already satisfied: packaging>=21 in /usr/local/lib/python3.10/dist-packages (from scikit-image->easyocr) (24.1)\n",
"Requirement already satisfied: lazy-loader>=0.4 in /usr/local/lib/python3.10/dist-packages (from scikit-image->easyocr) (0.4)\n",
"Requirement already satisfied: MarkupSafe>=2.0 in /usr/local/lib/python3.10/dist-packages (from jinja2->torch->easyocr) (3.0.2)\n",
"Downloading easyocr-1.7.2-py3-none-any.whl (2.9 MB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m2.9/2.9 MB\u001b[0m \u001b[31m48.6 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading ninja-1.11.1.1-py2.py3-none-manylinux1_x86_64.manylinux_2_5_x86_64.whl (307 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m307.2/307.2 kB\u001b[0m \u001b[31m22.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading pyclipper-1.3.0.post6-cp310-cp310-manylinux_2_12_x86_64.manylinux2010_x86_64.whl (912 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m912.2/912.2 kB\u001b[0m \u001b[31m44.9 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hDownloading python_bidi-0.6.3-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (286 kB)\n",
"\u001b[2K \u001b[90m━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━\u001b[0m \u001b[32m286.8/286.8 kB\u001b[0m \u001b[31m19.2 MB/s\u001b[0m eta \u001b[36m0:00:00\u001b[0m\n",
"\u001b[?25hInstalling collected packages: python-bidi, pyclipper, ninja, easyocr\n",
"Successfully installed easyocr-1.7.2 ninja-1.11.1.1 pyclipper-1.3.0.post6 python-bidi-0.6.3\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"!pip install fpdf"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "RzslMvBT3m8B",
"outputId": "42040908-5521-4b4e-e453-f29357b1017b"
},
"execution_count": 10,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"Collecting fpdf\n",
" Downloading fpdf-1.7.2.tar.gz (39 kB)\n",
" Preparing metadata (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
"Building wheels for collected packages: fpdf\n",
" Building wheel for fpdf (setup.py) ... \u001b[?25l\u001b[?25hdone\n",
" Created wheel for fpdf: filename=fpdf-1.7.2-py2.py3-none-any.whl size=40704 sha256=ba17c004c11322432e0424a6f6b30cf73adc4f92d3b4827fb4634d993ea06273\n",
" Stored in directory: /root/.cache/pip/wheels/f9/95/ba/f418094659025eb9611f17cbcaf2334236bf39a0c3453ea455\n",
"Successfully built fpdf\n",
"Installing collected packages: fpdf\n",
"Successfully installed fpdf-1.7.2\n"
]
}
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {
"id": "vj6ee7edoVGP"
},
"outputs": [],
"source": [
"from easyocr import Reader\n",
"from PIL import Image\n",
"from io import BytesIO\n",
"import numpy as np"
]
},
{
"cell_type": "code",
"execution_count": 3,
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "uCLVTVS6oVGR",
"outputId": "8230340d-b821-40da-cc6e-cbeaca35f28b"
},
"outputs": [
{
"output_type": "stream",
"name": "stderr",
"text": [
"WARNING:easyocr.easyocr:Downloading detection model, please wait. This may take several minutes depending upon your network connection.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"Progress: |██████████████████████████████████████████████████| 100.0% Complete"
]
},
{
"output_type": "stream",
"name": "stderr",
"text": [
"WARNING:easyocr.easyocr:Downloading recognition model, please wait. This may take several minutes depending upon your network connection.\n"
]
},
{
"output_type": "stream",
"name": "stdout",
"text": [
"Progress: |██████████████████████████████████████████████████| 100.0% Complete"
]
}
],
"source": [
"# Loading model for the English language\n",
"language_reader = Reader([\"en\"])"
]
},
{
"cell_type": "code",
"source": [
"def extract_txt_from_img(file_path):\n",
"\n",
" image = Image.open(file_path)\n",
"\n",
" image_np = np.array(image)\n",
" text = language_reader.readtext(image_np)\n",
" extracted_text = \"\\n\".join([result[1] for result in text])\n",
"\n",
" return extracted_text"
],
"metadata": {
"id": "RypAbeHmsmfc"
},
"execution_count": 4,
"outputs": []
},
{
"cell_type": "code",
"source": [
"filepath = \"testimg6.jfif\"\n",
"img_to_text = extract_txt_from_img(filepath)\n",
"print(img_to_text)"
],
"metadata": {
"colab": {
"base_uri": "https://localhost:8080/"
},
"id": "TJGr3qmvtLmJ",
"outputId": "6ebf19ca-1c31-41e3-8bb0-c458d3993ad7"
},
"execution_count": 5,
"outputs": [
{
"output_type": "stream",
"name": "stdout",
"text": [
"SEE\n",
"LIGHT\n",
"In THE\n",
"DARKNESS\n"
]
}
]
},
{
"cell_type": "code",
"source": [
"# making text file\n",
"def save_text_as_file(text, text_file_path):\n",
" with open(text_file_path, 'w') as file:\n",
" file.write(text)"
],
"metadata": {
"id": "d7Cs1Hvw3Rfq"
},
"execution_count": 6,
"outputs": []
},
{
"cell_type": "code",
"source": [
"save_text_as_file(img_to_text, \"/content/EasyOCR_output_text.txt\")"
],
"metadata": {
"id": "k3VnYn-d3W67"
},
"execution_count": 9,
"outputs": []
},
{
"cell_type": "code",
"source": [
"# making pdf file\n",
"from fpdf import FPDF\n",
"def save_text_as_pdf(text, pdf_file_path):\n",
" pdf = FPDF()\n",
" pdf.set_auto_page_break(auto=True, margin=15)\n",
" pdf.add_page()\n",
"\n",
" pdf.set_font(\"Arial\", size=12)\n",
" pdf.multi_cell(0, 10, text)\n",
"\n",
" pdf.output(pdf_file_path)"
],
"metadata": {
"id": "ifn93tls3r7Z"
},
"execution_count": 11,
"outputs": []
},
{
"cell_type": "code",
"source": [
"save_text_as_pdf(img_to_text, \"/content/EasyOCR_output_pdf.pdf\")"
],
"metadata": {
"id": "Cg6zo_Ew3v6l"
},
"execution_count": 12,
"outputs": []
}
],
"metadata": {
"language_info": {
"name": "python"
},
"colab": {
"provenance": [],
"gpuType": "T4"
},
"accelerator": "GPU",
"kernelspec": {
"name": "python3",
"display_name": "Python 3"
}
},
"nbformat": 4,
"nbformat_minor": 0
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file not shown.
Binary file not shown.
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
The Quick Brown
Fox Jumps Over
The Lazy Dog

Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Loading