> 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/ja/supported-models/trocr.md).

# TrOCR

TrOCR は Microsoft のトランスフォーマーベースの OCR モデルです。行単位のテキスト認識用に学習されているため、最良の結果を得るには入力を 1 つのテキスト領域に切り出してください。

{% hint style="info" %}
TrOCR は Serverless Cloud API では利用できません。次の環境で実行してください。 [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) または [self-hosted Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).
{% endhint %}

## TrOCR API

{% 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",
    headers={"Authorization": f"Bearer {os.environ['ROBOFLOW_API_KEY']}"},
    json={
        "image": {"type": "base64", "value": image_base64},
    },
)
print(response.json()["result"])
```

{% endstep %}
{% endstepper %}

上のコードは、認識されたテキストをターミナルに出力します:

```
合計
```

## TrOCR の推論速度

次で測定したレイテンシー [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 1台の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 は切り出された 1 行のテキストを認識するため、これはページ全体ではなく 1 行分の切り出しに対するレイテンシです。

{% hint style="info" %}
設定する `URL` をデプロイ先に合わせます:

* `http://localhost:9001` ローカルの [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) サーバー用。
* あなたの [Dedicated Deployment](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments) プライベートエンドポイント用のURL。
  {% endhint %}

## セルフホスト型 Inference で TrOCR を実行する

TrOCR は次の環境で提供されます。 [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 自分のハードウェア上で実行することです。ローカルサーバーを起動し、次の `model` 引数を使って共有 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 は、切り出された 1 行の印刷テキストで最も良い性能を発揮します。送信する前に各テキスト領域を切り出してください。ほかの一部の OCR モデルとは異なり、TrOCR は切り出されていない画像や複数行の画像をうまく処理できません。
{% endhint %}
