> 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 を当社の [サーバーレスホスト型 API](https://docs.roboflow.com/deployment/roboflow-cloud/serverless-api), [専用デプロイ](https://docs.roboflow.com/deployment/roboflow-cloud/dedicated-deployments)、および [セルフホスト型 Inference](https://docs.roboflow.com/deployment/self-hosted/self-hosted).

## コード例

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' \
  --data '{
    "api_key": "'"$ROBOFLOW_API_KEY"'",
    "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

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

## 推論速度

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

<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` 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（セルフホスト型）で使用する

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

{% hint style="warning" %}
セルフホストの GLM-OCR には GPU と、 `inference-models` のサポートが有効な Inference ビルドが必要です（`USE_INFERENCE_MODELS=true`).
{% endhint %}

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

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