> For the complete documentation index, see [llms.txt](https://docs.roboflow.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.roboflow.com/models/ko/supported-models/trocr.md).

# TrOCR

TrOCR은 Microsoft의 트랜스포머 기반 OCR 모델입니다. 이 모델은 줄 단위 텍스트 인식을 위해 학습되었으므로, 최상의 결과를 위해 입력을 하나의 텍스트 영역으로 잘라 주세요.

{% hint style="info" %}
TrOCR은 Serverless Hosted API에서 사용할 수 없습니다. 다음에서 실행하세요 [전용 배포](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) 또는 [자가 호스팅 Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).
{% endhint %}

## 코드 샘플

{% stepper %}
{% step %}

### API 키 받기

Roboflow 계정을 만들고, 다음에서 키를 찾으세요 [Roboflow API 설정 페이지](https://app.roboflow.com/settings/api) 그리고 셸에서 사용할 수 있도록 설정하세요:

```bash
export ROBOFLOW_API_KEY="your-key-here"
```

{% endstep %}

{% step %}

### 의존성 설치하기

이 패키지들은 이미지를 가져와 API를 호출합니다:

```bash
pip install -U requests opencv-python supervision
```

{% endstep %}

{% step %}

### 모델 실행하기

설정하세요 `URL` 전용 배포 URL 또는 로컬 추론 서버로.

```python
import base64
import os
import cv2
import requests
import supervision as sv

URL = "https://your-deployment.roboflow.cloud"
image = sv.load_image_from_url("https://media.roboflow.com/inference/license_plate_1.jpg")
_, buffer = cv2.imencode(".jpg", image)
image_base64 = base64.b64encode(buffer).decode("utf-8")

response = requests.post(
    f"{URL}/ocr/trocr",
    json={
        "api_key": os.environ["ROBOFLOW_API_KEY"],
        "image": {"type": "base64", "value": image_base64},
    },
)
print(response.json()["result"])
```

{% endstep %}
{% endstepper %}

위 코드는 인식된 텍스트를 터미널에 출력합니다:

```
총계
```

## 추론 속도

다음을 사용해 측정한 지연 시간 [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 1x NVIDIA L4, 배치 크기 1, 워밍업 후 평균.

<table data-search="false"><thead><tr><th>모델</th><th>지연 시간(ms)</th></tr></thead><tbody><tr><td><code>trocr</code></td><td>114.4</td></tr></tbody></table>

TrOCR은 잘라낸 단일 텍스트 줄 하나를 인식하므로, 이는 전체 페이지가 아니라 한 줄 자르기에 대한 지연 시간입니다.

{% hint style="info" %}
설정하세요 `URL` 배포 대상에 맞게 설정하세요:

* `http://localhost:9001` 로컬 [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 서버.
* 사용자 [전용 배포](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) 비공개 엔드포인트용 URL.
  {% endhint %}

## Inference(자가 호스팅)와 함께 사용

TrOCR은 다음에서 제공됩니다 [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 자체 하드웨어에서 실행하는 방식입니다. 로컬 서버를 시작한 다음, 다음을 사용하여 모델을 선택하세요 `모델` 공유 OCR 엔드포인트의 인수:

```bash
pip install inference-cli
inference server start  # http://localhost:9001을 제공합니다
```

```python
from inference_sdk import InferenceHTTPClient

client = InferenceHTTPClient(api_url="http://127.0.0.1:9001")

result = client.ocr_image(inference_input="./serial_number.png", model="trocr")
print(result)
```

{% hint style="warning" %}
TrOCR은 잘라낸 단일 줄의 인쇄된 텍스트에서 가장 잘 작동합니다. 각 텍스트 영역을 잘라서 전달하세요. 다른 일부 OCR 모델과 달리, TrOCR은 자르지 않은 이미지나 여러 줄 이미지에서는 잘 작동하지 않습니다.
{% endhint %}
