> 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) は、当社の [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api).

## DocTR API

DocTRをHTTPエンドポイント経由で直接実行するには `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' \\
  --header "Authorization: Bearer $ROBOFLOW_API_KEY" \\
  --data '{
    "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, InferenceConfiguration

# テキストを含むサンプル画像
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"],
).configure(InferenceConfiguration(api_key_transport="header"))

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

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

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

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

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

## DocTRの推論速度

次で測定したレイテンシー [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>doctr</code></td><td>83.5</td></tr></tbody></table>

完全なドキュメント画像で測定（テキスト検出と認識を含む）。

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

* `https://serverless.roboflow.com` サーバーレスクラウドAPI用。
* `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でDocTRを実行する

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

```bash
pip install inference-cli
inference server start  # 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/)
