Update ocr_utils.py
Browse files- ocr_utils.py +10 -36
ocr_utils.py
CHANGED
|
@@ -27,37 +27,35 @@ def load_models(language):
|
|
| 27 |
def extract_text_aws(file_content):
|
| 28 |
try:
|
| 29 |
response = textract_client.detect_document_text(Document={'Bytes': file_content})
|
| 30 |
-
return
|
| 31 |
-
for item in response['Blocks'] if item['BlockType'] == 'WORD']
|
| 32 |
except Exception as e:
|
| 33 |
logging.error(f"Error in AWS Textract: {str(e)}")
|
| 34 |
-
return
|
| 35 |
|
| 36 |
def extract_text_doctr(image, model):
|
| 37 |
try:
|
| 38 |
doc = DocumentFile.from_images([image])
|
| 39 |
result = model(doc)
|
| 40 |
-
return
|
| 41 |
-
for block in result.pages[0].blocks for line in block.lines for word in line.words]
|
| 42 |
except Exception as e:
|
| 43 |
logging.error(f"Error in Doctr OCR: {str(e)}")
|
| 44 |
-
return
|
| 45 |
|
| 46 |
def extract_text_easyocr(image, reader):
|
| 47 |
try:
|
| 48 |
result = reader.readtext(np.array(image))
|
| 49 |
-
return
|
| 50 |
except Exception as e:
|
| 51 |
logging.error(f"Error in EasyOCR: {str(e)}")
|
| 52 |
-
return
|
| 53 |
|
| 54 |
def extract_text_paddleocr(image, reader):
|
| 55 |
try:
|
| 56 |
result = reader.ocr(image, cls=True)
|
| 57 |
-
return
|
| 58 |
except Exception as e:
|
| 59 |
logging.error(f"Error in PaddleOCR: {str(e)}")
|
| 60 |
-
return
|
| 61 |
|
| 62 |
def parallel_ocr(image, file_content, doctr_model, easyocr_reader, paddleocr_reader):
|
| 63 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
@@ -73,7 +71,7 @@ def parallel_ocr(image, file_content, doctr_model, easyocr_reader, paddleocr_rea
|
|
| 73 |
"paddleocr": future_paddleocr.result()
|
| 74 |
}
|
| 75 |
|
| 76 |
-
return results
|
| 77 |
|
| 78 |
@lru_cache(maxsize=1000)
|
| 79 |
def detect_language(text):
|
|
@@ -82,28 +80,4 @@ def detect_language(text):
|
|
| 82 |
return lang
|
| 83 |
except Exception as e:
|
| 84 |
logging.error(f"Error in language detection: {str(e)}")
|
| 85 |
-
return 'en' # Default to English
|
| 86 |
-
|
| 87 |
-
def combine_ocr_results(results, weights):
|
| 88 |
-
combined_words = []
|
| 89 |
-
for method, words in results.items():
|
| 90 |
-
for word, bbox, confidence in words:
|
| 91 |
-
try:
|
| 92 |
-
polygon = bbox_to_polygon(bbox)
|
| 93 |
-
combined_words.append((word, polygon, float(confidence) * weights[method]))
|
| 94 |
-
except Exception as e:
|
| 95 |
-
logging.error(f"Error processing word '{word}' from {method}: {str(e)}")
|
| 96 |
-
|
| 97 |
-
final_words = []
|
| 98 |
-
while combined_words:
|
| 99 |
-
current_word = combined_words.pop(0)
|
| 100 |
-
overlapping = [w for w in combined_words if current_word[1].intersects(w[1])]
|
| 101 |
-
if overlapping:
|
| 102 |
-
best_word = max([current_word] + overlapping, key=lambda x: x[2])
|
| 103 |
-
final_words.append(best_word[0])
|
| 104 |
-
for word in overlapping:
|
| 105 |
-
combined_words.remove(word)
|
| 106 |
-
else:
|
| 107 |
-
final_words.append(current_word[0])
|
| 108 |
-
|
| 109 |
-
return ' '.join(final_words)
|
|
|
|
| 27 |
def extract_text_aws(file_content):
|
| 28 |
try:
|
| 29 |
response = textract_client.detect_document_text(Document={'Bytes': file_content})
|
| 30 |
+
return " ".join([item['Text'] for item in response['Blocks'] if item['BlockType'] == 'WORD'])
|
|
|
|
| 31 |
except Exception as e:
|
| 32 |
logging.error(f"Error in AWS Textract: {str(e)}")
|
| 33 |
+
return ""
|
| 34 |
|
| 35 |
def extract_text_doctr(image, model):
|
| 36 |
try:
|
| 37 |
doc = DocumentFile.from_images([image])
|
| 38 |
result = model(doc)
|
| 39 |
+
return " ".join([word.value for block in result.pages[0].blocks for line in block.lines for word in line.words])
|
|
|
|
| 40 |
except Exception as e:
|
| 41 |
logging.error(f"Error in Doctr OCR: {str(e)}")
|
| 42 |
+
return ""
|
| 43 |
|
| 44 |
def extract_text_easyocr(image, reader):
|
| 45 |
try:
|
| 46 |
result = reader.readtext(np.array(image))
|
| 47 |
+
return " ".join([detection[1] for detection in result])
|
| 48 |
except Exception as e:
|
| 49 |
logging.error(f"Error in EasyOCR: {str(e)}")
|
| 50 |
+
return ""
|
| 51 |
|
| 52 |
def extract_text_paddleocr(image, reader):
|
| 53 |
try:
|
| 54 |
result = reader.ocr(image, cls=True)
|
| 55 |
+
return " ".join([line[1][0] for line in result[0]])
|
| 56 |
except Exception as e:
|
| 57 |
logging.error(f"Error in PaddleOCR: {str(e)}")
|
| 58 |
+
return ""
|
| 59 |
|
| 60 |
def parallel_ocr(image, file_content, doctr_model, easyocr_reader, paddleocr_reader):
|
| 61 |
with concurrent.futures.ThreadPoolExecutor() as executor:
|
|
|
|
| 71 |
"paddleocr": future_paddleocr.result()
|
| 72 |
}
|
| 73 |
|
| 74 |
+
return " ".join(results.values())
|
| 75 |
|
| 76 |
@lru_cache(maxsize=1000)
|
| 77 |
def detect_language(text):
|
|
|
|
| 80 |
return lang
|
| 81 |
except Exception as e:
|
| 82 |
logging.error(f"Error in language detection: {str(e)}")
|
| 83 |
+
return 'en' # Default to English
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|