Skip to content

비전 검사 서비스 Backend

개요

GitHub 저장소 링크

사용자가 이미지를 업로드 하고 업로드한 이미지에서 영역을 정해 OCR을 수행할 수 있는 서비스

기술 스택

  • 백엔드 서버 구성: FastAPI Gunicorn Uvicorn
  • 데이터베이스: MariaDB MongoDB
  • OCR 인식 엔진: Tesseract
  • 배포: Docker

서비스 설명

  • 절차적 프로그래밍을 기반으로 한 도메인 주도의 레이어드 아키텍처 구성을 통해 코드 가독성 확보
  • 객체 지향적 프로그래밍 기반의 헥사고날 아키텍처(port - adapter 패턴) 적용을 통해 설계 유연성 확보
  • 배열 데이터의 일괄 처리 시 가독성 향상을 위해 comprehension 문법을 사용한 함수형 프로그래밍 적용
  • bcrypt 알고리즘을 사용한 JWT 로그인 기능
  • 배포를 위한 docker 컨테이너화

아키텍처

시스템 아키텍처

ocr_backend_architecture

  • 서버 가용성 확보를 위한 비동기 처리 적용
  • 커넥션 풀(connection pool) 기반의 ORM 사용
    • SQL injection 방지
    • 데이터베이스 부하 방지
  • 이미지 BLOB 데이터베이스 및 인식 결과 문서 저장을 위한 No-SQL DB 활용
  • 테서렉트 프로세스 관리를 위해 싱글턴 패턴 활용

    테서렉트 프로세스 관리 class
    class TesseractInferencer(OcrInferencer):
        _instance = None
    
        def __new__(cls, path: Path, *args, **kwargs):
            if not cls._instance:
                cls._instance = super(TesseractInferencer, cls).__new__(
                    cls, *args, **kwargs
                )
                pytesseract.pytesseract.tesseract_cmd = path
            return cls._instance
    
        async def extract_text(
            self,
            *,
            image: Image.Image,
            roi: ocr_model.Roi,
            lang: str | None = None,
            config: str = "",
            nice: int = 0,
            timeout: int = 0,
        ) -> list[str]:
            image = crop_image(image=image, roi=roi)
            extracted_text: str = await concurrency.run_in_threadpool(
                func=pytesseract.image_to_string,
                image=image,
                lang=lang,
                config=config,
                nice=nice,
                timeout=timeout,
            )
            res = extracted_text.replace("\n", "").split(" ")
            return res
    

DB 설계

OCR 서비스

erDiagram
    ROLE {
        bigint id   PK
        string name UK
    }

    STATE {
        bigint id PK
        string name UK
    }

    ROLE |o..o{ USER : ""
    USER {
        bigint      id                  PK
        string      name                UK  "null"
        string      password                "null"
        string      email               UK  "null"
        datetime    created_datetime
        bigint      role_id             FK  "null"
    }

    STATE ||..o{ USER_STATE : ""
    USER ||..o{ USER_STATE : ""
    USER_STATE {
        bigint      user_id             PK, FK
        bigint      state_id            PK, FK
        string      detail                      "null"
        datetime    created_datetime
    }

    USER ||..o{ LOGGED_IN : creates
    LOGGED_IN {
        bigint      id                  PK
        bigint      user_id             FK
        datetime    created_datetime
    }

    USER ||..o{ IMAGE : owns
    IMAGE {
        bigint      id                  PK
        string      name
        string      media_type              "image MIME type"
        bigint      image_id                "original image id at MongoDB"
        bigint      thumbnail_id            "thumbnail image id at MongoDB"
        datetime    created_datetime
        bigint      user_id             FK
    }

    USER ||..o{ OCR : creates
    IMAGE ||..o{ OCR : meta-data
    OCR {
        bigint      id                  PK
        int         version                 "default=1"
        datetime    created_datetime
        bigint      image_id            FK
        bigint      user_id             FK
        string      content_id              "ocr result id at MongoDB"
    }

    OCR ||..|| BLOB : meta-data
    IMAGE ||..|| BLOB : meta-data
  • 데이터의 생성 및 관리 단위에 따라 테이블 분리 및 정규화
  • N + 1 문제 방지를 위해 연관 관계(relationship mapping) 사용 지양
  • IMAGE 엔티티의 image_id, thumbnail_id 칼럼은 BLOB 데이터베이스에 저장된 이미지 데이터의 식별자
  • OCR 엔티티의 content_id 칼럼은 배열로 도출되는 인식 결과를 문서 형태로 저장한 NO-SQL DB에서의 식별자

주요 비즈니스 프로세스 및 설계

이미지 OCR 서비스

---
title: Image Upload Process
---
sequenceDiagram
    Client ->> FastAPI: API request
    activate FastAPI
    FastAPI -->> BLOB Database: upload thumbnail
    BLOB Database --) FastAPI: thumbnail id
    FastAPI -->> BLOB Database: upload image
    BLOB Database --) FastAPI: image id
    FastAPI -->> Database: insert image meta data
    FastAPI ->> Client: response
    deactivate FastAPI
---
title: Document OCR Process
---
sequenceDiagram
    Client ->> FastAPI: API request
    activate FastAPI
    FastAPI -->> Database: query
    Database --) FastAPI: image meta data
    FastAPI -->> BLOB Database: query
    BLOB Database --) FastAPI: image data
    FastAPI ->> Tesseract: ocr request
    Tesseract ->> FastAPI: result
    FastAPI --) Client: response
    FastAPI --) Database: Insert result meta data
    FastAPI --) Document DB: Insert OCR result data
    deactivate FastAPI