> 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 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` Dedicated Deployment 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) NVIDIA L4 1基、バッチサイズ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) サーバー用。
* あなたの [専用デプロイメント](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  # serves 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 %}
