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