mirror of
https://github.com/serty2005/rmser.git
synced 2026-02-04 19:02:33 -06:00
.venv deleted
ocr ready to test
This commit is contained in:
38
ocr-service/ocr.py
Normal file
38
ocr-service/ocr.py
Normal file
@@ -0,0 +1,38 @@
|
||||
import logging
|
||||
import pytesseract
|
||||
from PIL import Image
|
||||
import numpy as np
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
# Если tesseract не в PATH, раскомментируй и укажи путь:
|
||||
# pytesseract.pytesseract.tesseract_cmd = r'/usr/bin/tesseract'
|
||||
|
||||
class OCREngine:
|
||||
def __init__(self):
|
||||
logger.info("Initializing Tesseract OCR wrapper...")
|
||||
# Tesseract не требует загрузки моделей в память,
|
||||
# проверка версии просто чтобы убедиться, что он установлен
|
||||
try:
|
||||
version = pytesseract.get_tesseract_version()
|
||||
logger.info(f"Tesseract version found: {version}")
|
||||
except Exception as e:
|
||||
logger.error("Tesseract not found! Make sure it is installed (apt install tesseract-ocr).")
|
||||
raise e
|
||||
|
||||
def recognize(self, image: np.ndarray) -> str:
|
||||
"""
|
||||
Принимает бинарное изображение (numpy array).
|
||||
"""
|
||||
# Tesseract работает лучше с PIL Image
|
||||
pil_img = Image.fromarray(image)
|
||||
|
||||
# Конфигурация:
|
||||
# -l rus+eng: русский и английский
|
||||
# --psm 6: Assume a single uniform block of text (хорошо для чеков)
|
||||
custom_config = r'--oem 3 --psm 6'
|
||||
|
||||
text = pytesseract.image_to_string(pil_img, lang='rus+eng', config=custom_config)
|
||||
return text
|
||||
|
||||
ocr_engine = OCREngine()
|
||||
Reference in New Issue
Block a user