> 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モデルです [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' \
  --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"]) # 抽出されたテキスト
```

上記のコードは推論結果を端末に出力します:

```
ミスター
AUTPMATIC
280SE
34 T6511
```

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

## DocTRの推論速度

レイテンシの計測条件: [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>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) サーバー。
* プライベートエンドポイントの [専用デプロイメント](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/)
