Create ocr_extractor.py
Browse files- ocr_extractor.py +103 -0
ocr_extractor.py
ADDED
|
@@ -0,0 +1,103 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import os
|
| 2 |
+
import json
|
| 3 |
+
import tempfile
|
| 4 |
+
from PIL import Image
|
| 5 |
+
import pytesseract
|
| 6 |
+
import pdf2image
|
| 7 |
+
import boto3
|
| 8 |
+
import easyocr
|
| 9 |
+
import paddleocr
|
| 10 |
+
from doctr.models import ocr_predictor
|
| 11 |
+
from doctr.io import DocumentFile
|
| 12 |
+
from collections import defaultdict
|
| 13 |
+
|
| 14 |
+
def load_weights(json_file):
|
| 15 |
+
with open(json_file, 'r') as file:
|
| 16 |
+
return json.load(file)
|
| 17 |
+
|
| 18 |
+
def aws_textract(file_path):
|
| 19 |
+
client = boto3.client('textract',
|
| 20 |
+
region_name='eu-west-3',
|
| 21 |
+
aws_access_key_id=os.getenv("aws_access_key_id"),
|
| 22 |
+
aws_secret_access_key=os.getenv('aws_secret_access_key'))
|
| 23 |
+
|
| 24 |
+
with open(file_path, 'rb') as file:
|
| 25 |
+
response = client.detect_document_text(Document={'Bytes': file.read()})
|
| 26 |
+
return [(item['Text'], item['Geometry']['BoundingBox']) for item in response['Blocks'] if item['BlockType'] == 'WORD']
|
| 27 |
+
|
| 28 |
+
def doctr_ocr(file_path):
|
| 29 |
+
model = ocr_predictor(pretrained=True)
|
| 30 |
+
doc = DocumentFile.from_images(file_path)
|
| 31 |
+
result = model(doc)
|
| 32 |
+
return [(word.value, word.geometry) for block in result.pages for line in block.lines for word in line.words]
|
| 33 |
+
|
| 34 |
+
def easyocr_extract(file_path):
|
| 35 |
+
reader = easyocr.Reader(['en'])
|
| 36 |
+
result = reader.readtext(file_path)
|
| 37 |
+
return [(text, bbox) for bbox, text, _ in result]
|
| 38 |
+
|
| 39 |
+
def paddleocr_extract(file_path):
|
| 40 |
+
ocr = paddleocr.PaddleOCR(use_angle_cls=True, lang='en')
|
| 41 |
+
result = ocr.ocr(file_path)
|
| 42 |
+
return [(line[1][0], line[0]) for line in result]
|
| 43 |
+
|
| 44 |
+
def extract_text(file_path, method):
|
| 45 |
+
extractors = {
|
| 46 |
+
'aws': aws_textract,
|
| 47 |
+
'doctr': doctr_ocr,
|
| 48 |
+
'easyocr': easyocr_extract,
|
| 49 |
+
'paddleocr': paddleocr_extract
|
| 50 |
+
}
|
| 51 |
+
return extractors[method](file_path)
|
| 52 |
+
|
| 53 |
+
def calculate_overlap(box1, box2):
|
| 54 |
+
x1 = max(box1[0], box2[0])
|
| 55 |
+
y1 = max(box1[1], box2[1])
|
| 56 |
+
x2 = min(box1[0] + box1[2], box2[0] + box2[2])
|
| 57 |
+
y2 = min(box1[1] + box1[3], box2[1] + box2[3])
|
| 58 |
+
|
| 59 |
+
if x2 <= x1 or y2 <= y1:
|
| 60 |
+
return 0
|
| 61 |
+
|
| 62 |
+
overlap_area = (x2 - x1) * (y2 - y1)
|
| 63 |
+
box1_area = box1[2] * box1[3]
|
| 64 |
+
box2_area = box2[2] * box2[3]
|
| 65 |
+
|
| 66 |
+
return overlap_area / min(box1_area, box2_area)
|
| 67 |
+
|
| 68 |
+
def weighted_vote(results, weights):
|
| 69 |
+
word_votes = defaultdict(float)
|
| 70 |
+
for method, words in results.items():
|
| 71 |
+
for word, bbox in words:
|
| 72 |
+
found_match = False
|
| 73 |
+
for existing_word in word_votes:
|
| 74 |
+
if calculate_overlap(bbox, existing_word[1]) > 0.5: # 50% de chevauchement
|
| 75 |
+
word_votes[existing_word] += weights[method]
|
| 76 |
+
found_match = True
|
| 77 |
+
break
|
| 78 |
+
if not found_match:
|
| 79 |
+
word_votes[(word, bbox)] += weights[method]
|
| 80 |
+
|
| 81 |
+
sorted_words = sorted(word_votes.items(), key=lambda x: (x[1], x[0][1][0], x[0][1][1]), reverse=True)
|
| 82 |
+
return [word for (word, _), _ in sorted_words]
|
| 83 |
+
|
| 84 |
+
def extract_text_from_cv(file_path, weights_file):
|
| 85 |
+
weights = load_weights(weights_file)
|
| 86 |
+
results = {method: extract_text(file_path, method) for method in weights.keys()}
|
| 87 |
+
return ' '.join(weighted_vote(results, weights))
|
| 88 |
+
|
| 89 |
+
# Function to handle both PDF and image files
|
| 90 |
+
def process_file(file_path, weights_file):
|
| 91 |
+
if file_path.lower().endswith('.pdf'):
|
| 92 |
+
# Convert PDF to images
|
| 93 |
+
images = pdf2image.convert_from_path(file_path)
|
| 94 |
+
texts = []
|
| 95 |
+
for i, image in enumerate(images):
|
| 96 |
+
temp_image_path = f'temp_image_{i}.png'
|
| 97 |
+
image.save(temp_image_path, 'PNG')
|
| 98 |
+
texts.append(extract_text_from_cv(temp_image_path, weights_file))
|
| 99 |
+
os.remove(temp_image_path)
|
| 100 |
+
return ' '.join(texts)
|
| 101 |
+
else:
|
| 102 |
+
# Process image directly
|
| 103 |
+
return extract_text_from_cv(file_path, weights_file)
|