sbt-idp/cope2n-ai-fi/common/ocr.py

45 lines
1.2 KiB
Python
Raw Normal View History

2023-11-30 11:22:16 +00:00
from common.utils.ocr_yolox import OcrEngineForYoloX_ID_Driving
from common.utils.word_formation import Word, words_to_lines
det_ckpt = "yolox-s-general-text-pretrain-20221226"
cls_ckpt = "satrn-lite-general-pretrain-20230106"
engine = OcrEngineForYoloX_ID_Driving(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(image):
"""Predict text from image
Args:
image_path (str): _description_
Returns:
list: list of words
"""
try:
lbboxes, lwords = engine.run_image(image)
lWords = [Word(text=word, bndbox=bbox) for word, bbox in zip(lwords, lbboxes)]
list_lines, _ = words_to_lines(lWords)
return list_lines
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)