PaddleOCR과 PaddleNLP 연동을 통한 신분증 데이터 구조화

시스템 구성 개요

PaddleOCR으로 문서 이미지의 텍스트 영역을 시각적 인식한 뒤, PaddleNLP의 사전학습 전역 추출 모델을 결합해 지정된 필드 데이터를 자동 매핑하는 파이프라인입니다. 주소나 발급 기관명처럼 규칙이 불분명한 비정형 텍스트에서도 정규표현식 대비 우수한 추출 안정성을 제공합니다.

환경 의존성 설치

라이브러리 버전 충돌을 방지하기 위해 별도 가상환경을 구성하고 GPU 가속 지원 패키지를 우선 설치하는 것을 권장합니다.

conda create -n doc_parse_env python=3.9
conda activate doc_parse_env
conda install paddlepaddle-gpu==2.6.1 -c https://mirrors.tuna.tsinghua.edu.cn/anaconda/cloud/Paddle/
pip install "paddleocr>=2.7.0" paddlenlp

이미지 텍스트 추출 모듈

OCR 엔진을 초기화하고 이미지 경로로부터 연속된 문자열을 회수하는 함수형 클래스를 정의합니다.

from paddleocr import PaddleOCR

class IdentityDocExtractor:
    def __init__(self):
        self.recognizer = PaddleOCR(lang='ch', use_gpu=False, show_log=False)

    def get_raw_content(self, img_filepath: str) -> str:
        detection_result = self.recognizer.ocr(img_filepath)
        if not detection_result or not detection_result[0]:
            return ""
        
        recognized_lines = [segment[1][0] for segment in detection_result[0] if segment]
        return " ".join(recognized_lines)

pipeline_runner = IdentityDocExtractor()
raw_text_block = pipeline_runner.get_raw_content("sample_id.jpg")

PaddleNLP 필드 매핑

회수된 원본 문장열에서 특정 속성값만 선별해야 할 경우 Taskflow API를 활용합니다. 스키마 목록을 전달하면 내부 서버가 각 토큰을 해당 라벨에 할당합니다.

from paddlenlp import Taskflow
import json

TARGET_ATTRIBUTES = [
    "이름", "성별", "민족", "생년월일",
    "거주지", "발급조직", "유효기한", "신분증번호"
]

schema_loader = Taskflow(task_name='information_extraction', schema=TARGET_ATTRIBUTES)
field_mappings = schema_loader(raw_text_block)[0]

parsed_structure = {}
for attr_key in TARGET_ATTRIBUTES:
    try:
        matched_text = field_mappings[attr_key][0]["text"]
        parsed_structure[attr_key] = matched_text
    except (IndexError, KeyError):
        parsed_structure[attr_key] = "미검출"

print(json.dumps(parsed_structure, ensure_ascii=False, indent=2))

컴파일 및 배포 설정

Python 스크립트를 Windows 환경의 독립 실행형 실행 파일로 변환하려면 PyInstaller를 사용합니다. 동적으로 로드되는 AI 프레임워크 모듈은 명시적으로 포함시켜야 합니다.

pip install pyinstaller
pyinstaller --onefile --hidden-import=paddleocr --hidden-import=paddlenlp --clean app_core.py

생성된 `.exe` 파일과 함께 모델 가중치 폴더(`det_mv3_db`, `rec_chinese` 등)를 동일 루트에 배치하거나, 런타임 환경변수를 통해 경로 지정을 완료해야 인식 엔진이 정상적으로 로딩됩니다.

태그: PaddleOCR PaddleNLP InformationExtraction OCR Pipeline

9월 25일 06:58에 게시됨