welcomecenterbot/nlp/ocr.py
2024-09-29 13:24:49 +03:00

23 lines
628 B
Python

import easyocr
import logging
logger = logging.getLogger(" ocr ")
# Initialize the EasyOCR reader
reader = easyocr.Reader(['ru']) # Specify the languages you want to support
def ocr_recognize(file_path):
sum_text = ""
# Use EasyOCR to detect text in the photo
results = reader.readtext(file_path)
if isinstance(results, list):
result = results[-1]
[_coords, ocr_text, ocr_accuracy] = result
logger.debug("OCR Result: %s", ocr_text)
if ocr_accuracy.item() > 0.5:
sum_text += " " + ocr_text
logger.debug(f'Recognized Text: {sum_text}')
return sum_text