Delete ocr_extractor.py
Browse files- ocr_extractor.py +0 -98
ocr_extractor.py
DELETED
|
@@ -1,98 +0,0 @@
|
|
| 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 process_file(file_path, weights_file):
|
| 85 |
-
weights = load_weights(weights_file)
|
| 86 |
-
if file_path.lower().endswith('.pdf'):
|
| 87 |
-
images = pdf2image.convert_from_path(file_path)
|
| 88 |
-
texts = []
|
| 89 |
-
for i, image in enumerate(images):
|
| 90 |
-
temp_image_path = f'temp_image_{i}.png'
|
| 91 |
-
image.save(temp_image_path, 'PNG')
|
| 92 |
-
results = {method: extract_text(temp_image_path, method) for method in weights.keys()}
|
| 93 |
-
texts.extend(weighted_vote(results, weights))
|
| 94 |
-
os.remove(temp_image_path)
|
| 95 |
-
return ' '.join(texts)
|
| 96 |
-
else:
|
| 97 |
-
results = {method: extract_text(file_path, method) for method in weights.keys()}
|
| 98 |
-
return ' '.join(weighted_vote(results, weights))
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|