Files
service-finder/backend/app/services/image_processor.py
2026-03-22 18:59:27 +00:00

41 lines
1.5 KiB
Python
Executable File

# /opt/docker/dev/service_finder/backend/app/services/image_processor.py
import cv2
import numpy as np
import logging
from typing import Optional
logger = logging.getLogger(__name__)
class DocumentImageProcessor:
""" Saját képtisztító pipeline Robot 3 OCR számára. """
@staticmethod
def process_for_ocr(image_bytes: bytes) -> Optional[bytes]:
if not image_bytes: return None
try:
nparr = np.frombuffer(image_bytes, np.uint8)
img = cv2.imdecode(nparr, cv2.IMREAD_COLOR)
if img is None: return None
# 1. Előkészítés (Szürkeárnyalat + Felskálázás)
gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
if gray.shape[1] < 1200:
gray = cv2.resize(gray, None, fx=2.0, fy=2.0, interpolation=cv2.INTER_CUBIC)
# 2. Kontraszt dúsítás (CLAHE)
clahe = cv2.createCLAHE(clipLimit=2.0, tileGridSize=(8, 8))
contrast = clahe.apply(gray)
# 3. Adaptív Binarizálás (Fekete-fehér szöveg kiemelés)
blur = cv2.GaussianBlur(contrast, (3, 3), 0)
thresh = cv2.adaptiveThreshold(
blur, 255, cv2.ADAPTIVE_THRESH_GAUSSIAN_C,
cv2.THRESH_BINARY, 11, 2
)
success, encoded_image = cv2.imencode('.png', thresh)
return encoded_image.tobytes() if success else None
except Exception as e:
logger.error(f"OpenCV Feldolgozási hiba: {e}")
return None