-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhybrid_processor.py
More file actions
61 lines (51 loc) · 2.23 KB
/
Copy pathhybrid_processor.py
File metadata and controls
61 lines (51 loc) · 2.23 KB
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
import os
try:
from .pdf_processor import PDFProcessor
from .local_ocr_processor import LocalOCRProcessor
except ImportError:
try:
from pdf_processor import PDFProcessor
except:
PDFProcessor = None
try:
from local_ocr_processor import LocalOCRProcessor
except:
LocalOCRProcessor = None
class HybridProcessor:
"""
Smart processor that automatically uses:
- PDF processor if .pdf files found
- OCR processor if only images found
Priority: PDF first (better quality), then images
"""
def __init__(self):
self.pdf_processor = PDFProcessor() if PDFProcessor else None
self.ocr_processor = LocalOCRProcessor() if LocalOCRProcessor else None
def process_book_folder(self, folder_path, delay=0, resume=True):
"""Automatically detect and process PDFs or images"""
# Check what files we have
files = os.listdir(folder_path)
pdf_files = [f for f in files if f.lower().endswith('.pdf')]
image_extensions = ('.jpg', '.jpeg', '.png', '.bmp', '.webp')
image_files = [f for f in files if f.lower().endswith(image_extensions)]
# Decide what to process
if pdf_files and self.pdf_processor:
print(f"📚 Found {len(pdf_files)} PDF file(s) - Using PDF processor")
print("✨ Direct text extraction (fast & accurate)\n")
return self.pdf_processor.process_book_folder(folder_path)
elif image_files and self.ocr_processor:
print(f"🖼️ Found {len(image_files)} image file(s) - Using OCR processor")
print("⚡ Local OCR processing\n")
return self.ocr_processor.process_book_folder(folder_path, delay, resume)
else:
print("❌ No PDF or image files found in books/ folder!")
print(" Please add either:")
print(" - PDF files (recommended)")
print(" - Image files (JPG, PNG, etc.)")
return []
# Alias for drop-in replacement
OCRProcessor = HybridProcessor
if __name__ == "__main__":
processor = HybridProcessor()
texts = processor.process_book_folder("./books")
print(f"\n✅ Total extracted: {len(texts)} pages")