ImageProcessor_MainServer/test/test_ocr_fastdeploy.py

68 lines
2.1 KiB
Python

import logging
import os
import cv2
from ocr_module import OCRModule
class SimpleLogger:
"""간단한 콘솔 로거 (OCRModule과 동일한 인터페이스)"""
def log(self, msg, level=logging.INFO, exc_info=False):
if level == logging.ERROR:
print("[ERROR]", msg)
elif level == logging.WARNING:
print("[WARN]", msg)
else:
print("[INFO]", msg)
if exc_info:
import traceback
traceback.print_exc()
def main():
# 샘플 이미지 경로 (img/1.jpg)
image_path = os.path.join("img", "1.jpg")
if not os.path.isfile(image_path):
raise FileNotFoundError(f"샘플 이미지가 존재하지 않습니다: {image_path}")
logger = SimpleLogger()
# base_dir를 현재 프로젝트 루트로 설정
base_dir = os.getcwd()
# OCRModule 초기화 (FastDeploy GPU)
ocr_module = OCRModule(logger=logger, base_dir=base_dir)
# 텍스트 감지 수행
ocr_results = ocr_module.detect_text(image_path, method="polygon")
# 중국어 텍스트만 필터링
chinese_results = ocr_module.filter_chinese_text(ocr_results)
# 결과 출력
print("\n=== 인식된 중국어 텍스트 ===")
for idx, res in enumerate(chinese_results, 1):
print(f"{idx:02d}. text = {res['text']}, conf = {res['confidence']:.3f}")
# 시각화 예시 (선택사항)
try:
import numpy as np
import cv2
img = cv2.imread(image_path)
for res in chinese_results:
poly = np.array(res['polygon'], dtype=np.int32)
cv2.polylines(img, [poly], isClosed=True, color=(0, 255, 0), thickness=2)
x, y, w, h = res['bbox']
cv2.putText(img, res['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()