> 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/doctr.md).

# DocTR

[DocTR](https://github.com/mindee/doctr) は、当社のサービス経由でデプロイ可能なドキュメントOCRモデルです [サーバーレスホスト型 API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api).

## コード例

HTTPエンドポイントを介して DocTR を直接実行するには、 `curl`、または以下を使って: [`inference-sdk`](https://docs.roboflow.com/reference/inference/inference-sdk) ラッパー。

{% tabs %}
{% tab title="HTTP（curl）" icon="webhook" %}
{% stepper %}
{% step %}

### API キーを取得する

Roboflow アカウントを作成し、以下でキーを見つけます： [Roboflow API 設定ページ](https://app.roboflow.com/settings/api) その後、シェルで利用できるようにします：

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

{% endstep %}

{% step %}

### モデルを実行する

以下を呼び出します: `/doctr/ocr` エンドポイントを `curl`:

```bash
curl --location 'https://serverless.roboflow.com/doctr/ocr' \
  --header 'Content-Type: application/json' \
  --data '{
    "api_key": "'"$ROBOFLOW_API_KEY"'",
    "image": {"type": "url", "value": "https://media.roboflow.com/inference/license_plate_1.jpg"}
  }'
```

{% endstep %}
{% endstepper %}
{% endtab %}

{% tab title="SDK（Python）" icon="python" %}
{% stepper %}
{% step %}

### API キーを取得する

Roboflow アカウントを作成し、以下でキーを見つけます： [Roboflow API 設定ページ](https://app.roboflow.com/settings/api) その後、シェルで利用できるようにします：

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

{% endstep %}

{% step %}

### 依存関係をインストールする

このパッケージは次のモデルを呼び出します：

```bash
pip install -U inference-sdk supervision
```

{% endstep %}

{% step %}

### モデルを実行する

テキストを含む画像上で DocTR を実行するには：

```python
import os
import supervision as sv
from inference_sdk import InferenceHTTPClient

# テキストを含むサンプル画像
image = sv.load_image_from_url("https://media.roboflow.com/inference/license_plate_1.jpg")

client = InferenceHTTPClient(
    api_url="https://serverless.roboflow.com",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)

result = client.ocr_image(inference_input=image, model="doctr")

print(result["result"]) # 抽出されたテキスト
```

上のコードは推論結果をターミナルに出力します：

```
Mr
AUTPMATIC
280SE
34 T6511
```

{% endstep %}
{% endstepper %}
{% endtab %}
{% endtabs %}

## 推論速度

レイテンシの測定条件： [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>doctr</code></td><td>83.5</td></tr></tbody></table>

全文書画像で測定（テキスト検出＋認識）。

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

* `https://serverless.roboflow.com` Serverless Hosted API 用。
* `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（セルフホスト型）で使用する

DocTR は中核モデルです [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted)、そのため自分でホストするサーバーでも実行できます。ローカルサーバーを起動し、同じ `ocr_image` をそのサーバーに向けます：

```bash
pip install inference-cli
inference server start  # serves http://localhost:9001
```

```python
import os
from inference_sdk import InferenceHTTPClient

client = InferenceHTTPClient(
    api_url="http://localhost:9001",
    api_key=os.environ["ROBOFLOW_API_KEY"],
)

result = client.ocr_image(inference_input="./container.jpg")
print(result)
```

応答には、認識されたテキストと推論時間が含まれます：

```
{'result': 'MSKU 0439215', 'time': 3.87}
```

## さらに読む

* [OCRで画像内のテキストを検出する方法](https://blog.roboflow.com/ocr-api/)
