""" Local test script to verify OCR works on the sample CamScanner PDF. Tests each page individually and prints the extracted text. """ import sys import os sys.path.insert(0, os.path.dirname(os.path.abspath(__file__))) import cv2 import numpy as np from PIL import Image import pdfplumber import pytesseract # Check if Tesseract is available try: version = pytesseract.get_tesseract_version() print(f"Tesseract version: {version}") except Exception as e: print(f"Tesseract not found: {e}") print("Trying to find tesseract...") # Try common Windows paths for path in [ r"C:\Program Files\Tesseract-OCR\tesseract.exe", r"C:\Program Files (x86)\Tesseract-OCR\tesseract.exe", r"C:\Users\kmthu\AppData\Local\Programs\Tesseract-OCR\tesseract.exe", ]: if os.path.exists(path): pytesseract.pytesseract.tesseract_cmd = path print(f"Found tesseract at: {path}") break else: print("ERROR: Tesseract not found anywhere. Cannot test locally.") sys.exit(1) pdf_path = r"c:\Users\kmthu\projects\puffnparse\sample file\CamScanner 25-03-2026 17.48 (1).pdf" print(f"\n{'='*80}") print(f"Testing PDF: {pdf_path}") print(f"{'='*80}") with pdfplumber.open(pdf_path) as pdf: print(f"Total pages: {len(pdf.pages)}") for page in pdf.pages: page_num = page.page_number print(f"\n{'='*80}") print(f"PAGE {page_num}") print(f"{'='*80}") # Convert page to image at 300 DPI (same as production code) page_image = page.to_image(resolution=300) pil_img = page_image.original # Convert PIL to OpenCV BGR open_cv_image = np.array(pil_img) if len(open_cv_image.shape) == 3 and open_cv_image.shape[2] == 3: img_bgr = cv2.cvtColor(open_cv_image, cv2.COLOR_RGB2BGR) elif len(open_cv_image.shape) == 3 and open_cv_image.shape[2] == 4: img_bgr = cv2.cvtColor(open_cv_image, cv2.COLOR_RGBA2BGR) else: img_bgr = cv2.cvtColor(open_cv_image, cv2.COLOR_GRAY2BGR) h, w = img_bgr.shape[:2] print(f"Image dimensions: {w}x{h} (landscape={w > h})") # Save original page image for inspection out_dir = r"c:\Users\kmthu\projects\puffnparse\sample file\test_output" os.makedirs(out_dir, exist_ok=True) cv2.imwrite(os.path.join(out_dir, f"page_{page_num}_original.png"), img_bgr) # Test fix_orientation from services.image_preprocessor import fix_orientation oriented = fix_orientation(img_bgr) cv2.imwrite(os.path.join(out_dir, f"page_{page_num}_oriented.png"), oriented) oh, ow = oriented.shape[:2] print(f"After orientation fix: {ow}x{oh}") # Run OCR on oriented image (simple grayscale, no heavy preprocessing) gray = cv2.cvtColor(oriented, cv2.COLOR_BGR2GRAY) text = pytesseract.image_to_string(gray, config='--psm 3').strip() # Show first 500 chars of output preview = text[:500] if len(text) > 500 else text print(f"\nExtracted text ({len(text)} chars):") print(f"---") print(preview) print(f"---") # Check if it looks like gibberish if text: words = text.split() long_words = [w for w in words if len(w) > 3] alpha_chars = sum(1 for c in text if c.isalpha()) total_chars = len(text.replace(' ', '').replace('\n', '')) alpha_ratio = alpha_chars / total_chars if total_chars > 0 else 0 print(f"\nQuality metrics:") print(f" Total words: {len(words)}") print(f" Words > 3 chars: {len(long_words)}") print(f" Alpha ratio: {alpha_ratio:.2%}") print(f" Likely gibberish: {alpha_ratio < 0.5 or len(long_words) < 5}") print(f"\n{'='*80}") print("Test complete! Check images in: " + out_dir) print(f"{'='*80}")