This project demonstrates how to extract text and structured data from a scanned PDF document using Optical Character Recognition (OCR). It uses Python with libraries like PyMuPDF, Tesseract, OpenCV, and Pillow.
- Loads a scanned PDF.
- Converts PDF pages to images.
- Pre-processes images to improve OCR accuracy (grayscale, contrast, sharpness, filtering, thresholding).
- Extracts text from the images using Tesseract OCR.
- Extracts structured data (like names, dates, amounts) using regular expressions.
- Draws bounding boxes around detected words and key fields on the document image.
- Python 3.x
- Tesseract OCR engine
- The following Python packages:
pymupdfpytesseractopencv-pythonpillownumpyjupyter
-
Clone the repository:
git clone https://github.com/jadhavkrushna/Analyze_a_Scanned_PDF-.git cd Analyze_a_Scanned_PDF- -
Install Tesseract:
- Windows: Download and install Tesseract from the official repository. Make sure to add the installation path to your system's
PATHenvironment variable or update the path in the notebook:pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'
- macOS:
brew install tesseract
- Linux (Ubuntu/Debian):
sudo apt install tesseract-ocr
- Windows: Download and install Tesseract from the official repository. Make sure to add the installation path to your system's
-
Install Python dependencies:
pip install -r requirements.txt
(Note: You may need to create a
requirements.txtfile or install packages directly as shown in the notebook) -
Launch Jupyter Notebook:
jupyter notebook
-
Open and run the notebook:
- Open the
Analyze_a_Scanned_PDF_project.ipynbfile in Jupyter. - Update the
pdf_document_pathvariable to the location of your scanned PDF file. - Run the cells sequentially.
- Open the
A scanned PDF file. The project is configured to work with a sample mortgage document, but it can be adapted for other scanned documents. You will need to provide the path to your PDF file in the notebook.
Example:
pdf_document_path = r"c:\Users\adminpc\Downloads\sample_mortgage_document.pdf"The project produces several outputs:
- Extracted Text: A raw string of text extracted from the document.
- Structured Data (JSON): Key information extracted from the document in JSON format.
{ "document_title": "Mortgage", "borrower_names": [ "Ro Leading", "Mercia Fill", ... ], "lender_name": null, "loan_amount": null, "property_address": null, "document_dates": [] } - Image with Bounding Boxes: An image of the document page with bounding boxes drawn around detected words and key fields.