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

# GLM-OCR

GLM-OCR は、GLM ビジョン・ランゲージモデルファミリーに基づく OCR モデルです。画像からテキストを書き起こし、レイアウトが混在する文書、標識、ラベルに適しています。GLM-OCR は当社の [Serverless Cloud API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api), [専用デプロイ](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments)、 [self-hosted Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).

## GLM-OCR API

GLM-OCR は共有 `/infer/lmm` エンドポイントで実行されます。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 %}

### モデルを実行する

次を呼び出します `/infer/lmm` エンドポイントを `curl`:

```bash
curl --location 'https://serverless.roboflow.com/infer/lmm' \\
  --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"},
    "model_id": "glm-ocr",
    "prompt": "OCR",
    "max_new_tokens": 128
  }'
```

{% 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 %}

### モデルを実行する

テキストを含む画像で GLM-OCR を実行:

```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.infer_lmm(
    image,
    model_id="glm-ocr",
    prompt="OCR",
    max_new_tokens=128,
)
print(result["response"] )
```

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

```
280 SE
AUTOMATIC
34 T 6511
```

<figure><img src="/files/d188302b04cb284db8d7835b79aa1a4d0687096e" alt=""><figcaption></figcaption></figure>
{% endstep %}
{% endstepper %}
{% endtab %}
{% endtabs %}

## GLM-OCR の推論速度

次で測定したレイテンシー [Roboflow Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 1 台の NVIDIA L4、バッチサイズ 1、固定プロンプトから greedy decoding でちょうど 128 トークンを生成した条件で測定。レイテンシは出力長に比例するため、他の長さの見積もりには tokens/sec を使用してください。

<table data-search="false"><thead><tr><th>別名</th><th>レイテンシ、128トークン（ms）</th><th>トークン/秒</th></tr></thead><tbody><tr><td><code>glm-ocr</code></td><td>1850</td><td>69</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 で GLM-OCR を実行

GLM-OCR は、 [Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted) 自分でホストするサーバー。

{% hint style="warning" %}
セルフホストの GLM-OCR には GPU と、 `inference-models` のサポートを有効にした（`USE_INFERENCE_MODELS=true`).
{% endhint %}

ローカルサーバーを起動します:

```bash
pip install inference-cli
inference server start  # http://localhost:9001 を提供
```

次に、認識プロンプトを使って共有マルチモーダルエンドポイントを呼び出します。GLM-OCR はカスタムプロンプトを受け付けるため、シリアル番号、ラベル、文書テキストに向けて誘導できます:

```python
import os
from inference_sdk import InferenceHTTPClient

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

result = client.infer_lmm(
    inference_input="./serial_number.png",
    prompt="Text Recognition:",
    model_id="glm-ocr",
)
print(result["response"] )
```

## 参考資料

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