forked from Anteros-Code-Mentoria/poc-mvc-ocr
ajustado
This commit is contained in:
parent
f0ea5b282b
commit
8a68a6b652
@ -14,16 +14,27 @@ UPLOAD_DIR = "app/static/uploads"
|
||||
TEXTS_DIR = "app/static/texts"
|
||||
|
||||
def process_document(file_path: str) -> Dict[str, str]:
|
||||
"""
|
||||
Processa um arquivo PDF ou imagem, aplica OCR com diferentes engines e salva os textos extraídos.
|
||||
Retorna os textos por engine e o texto corrigido.
|
||||
|
||||
Args:
|
||||
file_path (str): Caminho do arquivo a ser processado.
|
||||
|
||||
Returns:
|
||||
Dict[str, str]: Dicionário com os textos por engine e o texto corrigido.
|
||||
"""
|
||||
try:
|
||||
if not os.path.exists(file_path):
|
||||
raise FileNotFoundError(f"Arquivo não encontrado: {file_path}")
|
||||
|
||||
filename = os.path.basename(file_path)
|
||||
base_name = os.path.splitext(filename)[0]
|
||||
output_folder = os.path.join(TEXTS_DIR, base_name)
|
||||
os.makedirs(output_folder, exist_ok=True)
|
||||
|
||||
images = []
|
||||
if file_path.lower().endswith(".pdf"):
|
||||
images = convert_from_path(file_path)
|
||||
else:
|
||||
images = [Image.open(file_path)]
|
||||
# Converte PDF para imagens ou carrega imagem única
|
||||
images = convert_from_path(file_path) if file_path.lower().endswith(".pdf") else [Image.open(file_path)]
|
||||
|
||||
results = {
|
||||
"tesseract": "",
|
||||
@ -32,6 +43,7 @@ def process_document(file_path: str) -> Dict[str, str]:
|
||||
# "mmocr": ""
|
||||
}
|
||||
|
||||
# Processa cada página/imagem
|
||||
for i, image in enumerate(images):
|
||||
temp_img_path = os.path.join(output_folder, f"page_{i}.png")
|
||||
image.save(temp_img_path)
|
||||
@ -43,11 +55,18 @@ def process_document(file_path: str) -> Dict[str, str]:
|
||||
|
||||
os.remove(temp_img_path)
|
||||
|
||||
# Salva os textos extraídos
|
||||
for engine, text in results.items():
|
||||
save_text_file(text, os.path.join(output_folder, f"{engine}.txt"))
|
||||
|
||||
# Aplica correção no texto do tesseract
|
||||
corrected = correct_text(results["tesseract"])
|
||||
save_text_file(corrected, os.path.join(output_folder, "corrigido.txt"))
|
||||
results["corrigido"] = corrected
|
||||
|
||||
return results
|
||||
|
||||
except Exception as e:
|
||||
print(f"[ERRO] Falha ao processar documento: {e}")
|
||||
return {"error": str(e)}
|
||||
|
||||
|
||||
Loading…
Reference in New Issue
Block a user