Delete ocr_utils.py
Browse files- ocr_utils.py +0 -90
ocr_utils.py
DELETED
|
@@ -1,90 +0,0 @@
|
|
| 1 |
-
import logging
|
| 2 |
-
import boto3
|
| 3 |
-
from PIL import Image
|
| 4 |
-
import easyocr
|
| 5 |
-
from paddleocr import PaddleOCR
|
| 6 |
-
from doctr.io import DocumentFile
|
| 7 |
-
from doctr.models import ocr_predictor
|
| 8 |
-
import langid
|
| 9 |
-
import numpy as np
|
| 10 |
-
|
| 11 |
-
# Configure logging
|
| 12 |
-
logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(levelname)s - %(message)s')
|
| 13 |
-
|
| 14 |
-
# AWS Textract
|
| 15 |
-
def extract_text_aws(file_content):
|
| 16 |
-
try:
|
| 17 |
-
client = boto3.client('textract', region_name='us-west-2') # Specify your AWS region
|
| 18 |
-
response = client.detect_document_text(Document={'Bytes': file_content})
|
| 19 |
-
lines = []
|
| 20 |
-
for item in response["Blocks"]:
|
| 21 |
-
if item["BlockType"] == "LINE":
|
| 22 |
-
lines.append((item["Text"], item["Confidence"]))
|
| 23 |
-
return lines
|
| 24 |
-
except Exception as e:
|
| 25 |
-
logging.error(f"Error in AWS Textract: {str(e)}")
|
| 26 |
-
return []
|
| 27 |
-
|
| 28 |
-
# Doctr OCR
|
| 29 |
-
def extract_text_doctr(image, model):
|
| 30 |
-
try:
|
| 31 |
-
doc = DocumentFile.from_images([image])
|
| 32 |
-
result = model(doc)
|
| 33 |
-
lines = [(word.value, word.confidence) for block in result.pages[0].blocks for line in block.lines for word in line.words]
|
| 34 |
-
return lines
|
| 35 |
-
except Exception as e:
|
| 36 |
-
logging.error(f"Error in Doctr OCR: {str(e)}")
|
| 37 |
-
return []
|
| 38 |
-
|
| 39 |
-
# EasyOCR
|
| 40 |
-
def extract_text_easyocr(image, reader):
|
| 41 |
-
try:
|
| 42 |
-
result = reader.readtext(np.array(image))
|
| 43 |
-
lines = [(text, conf) for bbox, text, conf in result]
|
| 44 |
-
return lines
|
| 45 |
-
except Exception as e:
|
| 46 |
-
logging.error(f"Error in EasyOCR: {str(e)}")
|
| 47 |
-
return []
|
| 48 |
-
|
| 49 |
-
# PaddleOCR
|
| 50 |
-
def extract_text_paddleocr(image, reader):
|
| 51 |
-
try:
|
| 52 |
-
result = reader.ocr(image, cls=True)
|
| 53 |
-
lines = [(line[1][0], line[1][1]) for line in result[0]]
|
| 54 |
-
return lines
|
| 55 |
-
except Exception as e:
|
| 56 |
-
logging.error(f"Error in PaddleOCR: {str(e)}")
|
| 57 |
-
return []
|
| 58 |
-
|
| 59 |
-
# Combine OCR Results
|
| 60 |
-
def combine_ocr_results(results, weights):
|
| 61 |
-
combined_lines = {}
|
| 62 |
-
for source, lines in results.items():
|
| 63 |
-
weight = weights.get(source, 1) # Default weight to 1 if not specified
|
| 64 |
-
for line, confidence in lines:
|
| 65 |
-
if line in combined_lines:
|
| 66 |
-
combined_lines[line] += confidence * weight
|
| 67 |
-
else:
|
| 68 |
-
combined_lines[line] = confidence * weight
|
| 69 |
-
combined_text = ' '.join(sorted(combined_lines.keys(), key=lambda k: -combined_lines[k]))
|
| 70 |
-
return combined_text
|
| 71 |
-
|
| 72 |
-
# Detect Language
|
| 73 |
-
def detect_language(text):
|
| 74 |
-
try:
|
| 75 |
-
lang, _ = langid.classify(text)
|
| 76 |
-
return lang
|
| 77 |
-
except Exception as e:
|
| 78 |
-
logging.error(f"Error in language detection: {str(e)}")
|
| 79 |
-
return 'en' # Default to English
|
| 80 |
-
|
| 81 |
-
# Load Models
|
| 82 |
-
def load_models(language):
|
| 83 |
-
try:
|
| 84 |
-
doctr_model = ocr_predictor(pretrained=True)
|
| 85 |
-
easyocr_reader = easyocr.Reader([language])
|
| 86 |
-
paddleocr_reader = PaddleOCR(use_angle_cls=True, lang=language)
|
| 87 |
-
return doctr_model, easyocr_reader, paddleocr_reader
|
| 88 |
-
except Exception as e:
|
| 89 |
-
logging.error(f"Error loading models: {str(e)}")
|
| 90 |
-
return None, None, None
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|