# Rilevamento del testo

Rileva le etichette di testo in un’immagine prima di modificarle con un’operazione `replace_text`.

Fonte: https://scifig.ai/it/docs/api/text-detections

## Richiesta e risposta

`POST /images/text-detections` con `{"image": "<file_id>"}` e un `Idempotency-Key`. Ambito richiesto: `figures:generate`. Gratuito. Risponde in modo sincrono con `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` segue l’ordine di lettura; `corners` contiene le coordinate in pixel di ogni etichetta nell’immagine.
- Ripetere la stessa chiave restituisce lo stesso rilevamento senza eseguirlo nuovamente; la stessa chiave con un’altra immagine restituisce `409 IDEMPOTENCY_CONFLICT`.
- I rilevamenti condividono con il sito il limite di 20 ogni 15 minuti per account; oltre tale limite la risposta è `429 RATE_LIMITED`.
- Passa l’`id` con la stessa immagine a una modifica `replace_text`. Funziona solo per quell’immagine e per il tuo account.
- L’immagine è un `file_id` di PNG, JPEG o WebP. Un errore temporaneo del riconoscimento del testo restituisce `503 SERVICE_UNAVAILABLE` con `Retry-After`: riprova con la stessa chiave. Un rilevamento eseguito senza risultati restituisce `502 TASK_FAILED` con `TEXT_DETECTION_FAILED`: invialo di nuovo con una nuova chiave.

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