File size: 2,019 Bytes
e74eb58
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import pdfplumber
import easyocr
import fitz
import re
import numpy as np


reader = easyocr.Reader(['en'])


def clean_text(text):
    """
    Clean extracted text while preserving useful structure.
    """

    text = re.sub(r'[ \t]+', ' ', text)

    text = re.sub(r'\n+', '\n', text)

    return text.strip()


def extract_pdf_text(pdf_path):
    """
    Extract selectable text using pdfplumber.
    """

    text = ""

    with pdfplumber.open(pdf_path) as pdf:

        for page in pdf.pages:

            page_text = page.extract_text()

            if page_text:
                text += page_text + "\n"

    return clean_text(text)


def extract_scanned_pdf_text(pdf_path):
    """
    OCR fallback for scanned PDFs using
    PyMuPDF + EasyOCR.
    """
    print("Opening PDF...")

    doc = fitz.open(pdf_path)

    text = ""

    print(f"Pages found: {len(doc)}")

    for page_num in range(1, len(doc)):

        print(f"Processing page {page_num + 1}")

        page = doc.load_page(page_num)

        pix = page.get_pixmap(matrix=fitz.Matrix(1, 1))

        img = np.frombuffer(
            pix.samples,
            dtype=np.uint8
        )

        img = img.reshape(
            pix.height,
            pix.width,
            pix.n
        )

        results = reader.readtext(
            img,
            detail=0
        )

        text += " ".join(results)
        text += "\n"

    doc.close()

    return clean_text(text)


def extract_text_from_pdf(pdf_path):

    text = extract_pdf_text(pdf_path)

    if len(text.strip()) > 50:
        return text

    print("Scanned PDF detected. Running OCR...")

    return extract_scanned_pdf_text(pdf_path)


def extract_text_from_image(image_path):

    results = reader.readtext(
        image_path,
        detail=0
    )

    text = " ".join(results)

    return clean_text(text)


def extract_text(file_path):

    if file_path.lower().endswith(".pdf"):
        return extract_text_from_pdf(file_path)

    return extract_text_from_image(file_path)