import numpy as np
from pathlib import Path
from typing import Union, Tuple, List
from sdsvkvu.externals.basic_ocr.src.ocr import OcrEngine


def load_ocr_engine(opt) -> OcrEngine:
    print("[INFO] Loading engine...")
    engine = OcrEngine(**opt)
    print("[INFO] Engine loaded")
    return engine


def process_img(img: Union[str, np.ndarray], engine: OcrEngine) -> List: # For OCR integrated deskew using paddle
    page = engine(img)
    bboxes = []
    texts = []
    for word_segment in page._word_segments:
        for word in word_segment.list_words:
            bboxes.append(word.bbox[:])
            texts.append(word.text)   
    
    image = page.deskewed_image if page.deskewed_image is not None else page.image
    return bboxes, texts, image
        