# 文字識別

先識別圖片中的文字標籤，再透過 `replace_text` 編輯修改文字。

來源：https://scifig.ai/zh-Hant/docs/api/text-detections

## 請求與響應

`POST /images/text-detections` 接受 `{"image": "<file_id>"}` 和 `Idempotency-Key`。所需權限範圍：`figures:generate`。免費，同步回傳 `200`：

```json
{
  "id": "c222cd46-d923-4abf-aa28-cdc72ad06623",
  "object": "text_detection",
  "width": 2048,
  "height": 1536,
  "texts": [
    { "index": 0, "text": "Nucleus", "corners": [{ "x": 75, "y": 260 }, { "x": 241, "y": 260 }, { "x": 241, "y": 295 }, { "x": 75, "y": 295 }] }
  ]
}
```

- `texts` 按閱讀順序排列；`corners` 是圖片中各標籤的畫素座標。
- 重放相同金鑰會回傳同一識別結果，不會再次執行；相同金鑰搭配其他圖片回傳 `409 IDEMPOTENCY_CONFLICT`。
- 文字識別與網站共享每個帳戶每 15 分鐘 20 次的限制；超出後回傳 `429 RATE_LIMITED`。
- 將 `id` 和同一張圖傳給 `replace_text` 編輯。該識別結果僅適用於對應圖片和你的帳戶。
- 輸入圖片為 PNG、JPEG 或 WebP 的 `file_id`。文字識別服務暫時失敗時，回傳 `503 SERVICE_UNAVAILABLE` 和 `Retry-After`：請用相同金鑰重試。識別已執行但未產生結果時，回傳 `502 TASK_FAILED`（`TEXT_DETECTION_FAILED`）：請用新金鑰重新傳送。

## cURL

```bash
curl -X POST https://scifig.ai/api/v1/images/text-detections \
  -H "Authorization: Bearer $SCIFIG_API_KEY" \
  -H "Idempotency-Key: $(uuidgen)" \
  -H "Prefer: wait=25" \
  -H "Content-Type: application/json" \
  -d '{
    "image": "<file_id>"
  }'
```

## JavaScript

```javascript
const response = await fetch('https://scifig.ai/api/v1/images/text-detections', {
  method: 'POST',
  headers: {
    Authorization: `Bearer ${process.env.SCIFIG_API_KEY}`,
    'Idempotency-Key': crypto.randomUUID(),
    Prefer: 'wait=25',
    'Content-Type': 'application/json',
  },
  body: JSON.stringify({
    "image": "<file_id>"
  }),
});
const result = await response.json();
console.log(response.status, result);
```

## Python

```python
import os
import uuid

import requests

headers = {
    "Authorization": f"Bearer {os.environ['SCIFIG_API_KEY']}",
    "Idempotency-Key": str(uuid.uuid4()),
    "Prefer": "wait=25",
}
response = requests.post(
    "https://scifig.ai/api/v1/images/text-detections",
    headers=headers,
    json={
        "image": "<file_id>"
    },
    timeout=60,
)
print(response.status_code, response.json())
```
