sbt-idp/cope2n-ai-fi/api/OCRBase/text_recognition.py

47 lines
1.2 KiB
Python
Raw Permalink Normal View History

2023-11-30 11:22:16 +00:00
from common.utils.ocr_yolox import OcrEngineForYoloX_Invoice
from common.utils.word_formation import Word, words_to_lines
det_ckpt = "/models/sdsvtd/hub/wild_receipt_finetune_weights_c_lite.pth"
cls_ckpt = "satrn-lite-general-pretrain-20230106"
engine = OcrEngineForYoloX_Invoice(det_ckpt, cls_ckpt)
2024-07-05 13:14:47 +00:00
import logging
import logging.config
from utils.logging.logging import LOGGER_CONFIG
# Load the logging configuration
logging.config.dictConfig(LOGGER_CONFIG)
# Get the logger
logger = logging.getLogger(__name__)
2023-11-30 11:22:16 +00:00
def ocr_predict(img):
"""Predict text from image
Args:
image_path (str): _description_
Returns:
list: list of words
"""
try:
lbboxes, lwords = engine.run_image(img)
lWords = [Word(text=word, bndbox=bbox) for word, bbox in zip(lwords, lbboxes)]
list_lines, _ = words_to_lines(lWords)
return list_lines
# return lbboxes, lwords
except AssertionError as e:
2024-07-05 13:14:47 +00:00
logger.info(e)
2023-11-30 11:22:16 +00:00
list_lines = []
return list_lines
if __name__ == "__main__":
import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--image", type=str, required=True)
args = parser.parse_args()
list_lines = ocr_predict(args.image)