# Text detections

Find the text labels in an image before changing them with a replace_text edit.

Source: https://scifig.ai/docs/api/text-detections

## Request and response

`POST /images/text-detections` with `{"image": "<file_id>"}` and an `Idempotency-Key`. Required scope: `figures:generate`. Free. It answers synchronously with `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` are in reading order; `corners` are pixel coordinates of each label in the image.
- Replaying the same key returns the same detection without running it again; the same key with another image returns `409 IDEMPOTENCY_CONFLICT`.
- Detections share the website’s limit of 20 per 15 minutes per account; beyond it the answer is `429 RATE_LIMITED`.
- Pass the `id` with the same image to a `replace_text` edit. It only works for that image and your account.
- The image is a PNG, JPEG or WebP `file_id`. A temporary failure of the text recognizer returns `503 SERVICE_UNAVAILABLE` with `Retry-After`: retry with the same key. A detection that ran but produced no result returns `502 TASK_FAILED` (`TEXT_DETECTION_FAILED`): send it again with a new key.

## 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())
```
