ImageProcessor_MainServer/test/test_fast_ocr_module.py

56 lines
1.7 KiB
Python

# -*- coding: utf-8 -*-
import os
import logging
import numpy as np
import cv2
from ocr_module import OCRModule
class SimpleLogger:
def log(self, msg, level=logging.INFO, exc_info=False):
prefix = {logging.ERROR: "[ERROR]",
logging.WARNING: "[WARN]",
logging.INFO: "[INFO]"} .get(level, "[LOG]")
print(prefix, msg)
if exc_info:
import traceback
traceback.print_exc()
def main():
image_path = os.path.join("img", "1.jpg")
if not os.path.isfile(image_path):
raise FileNotFoundError(f"샘플 이미지가 존재하지 않습니다: {image_path}")
logger = SimpleLogger()
base_dir = os.getcwd()
ocr_module = OCRModule(logger=logger, base_dir=base_dir, gpu_id=0)
ocr_results = ocr_module.detect_text(image_path, method="polygon")
chinese_results = ocr_module.filter_chinese_text(ocr_results)
print("\n=== 인식된 중국어 텍스트 ===")
for i, r in enumerate(chinese_results, 1):
print(f"{i:02d}. text = {r['text']}, conf = {r['confidence']:.3f}")
# 시각화
try:
img = cv2.imread(image_path)
for r in chinese_results:
poly = np.array(r['polygon'], dtype=np.int32)
cv2.polylines(img, [poly], True, (0, 255, 0), 2)
x, y, w, h = r['bbox']
cv2.putText(img, r['text'], (x, y - 5),
cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 1)
vis_path = os.path.join("img", "1_vis.jpg")
cv2.imwrite(vis_path, img)
print(f"시각화 이미지를 {vis_path} 에 저장했습니다.")
except Exception as e:
logger.log(f"시각화 실패: {e}", level=logging.WARNING)
if __name__ == "__main__":
main()