AI Classification
Automatically analyse uploaded images using Cloudflare Workers AI, to generate labels and a caption for each one. These are added to search, so users can find images by what's in them, without any manual tagging.
Setup
Enable AI classification by setting the following environment variable:
ENABLE_AI_CLASSIFICATION='true'That's it. Images are now analysed after upload, by the variant queue, so queues need to be on too (they are by default).
Optional: NSFW Detection
While not currently implemented out-of-the-box (Cloudflare Workers AI doesn't have a native NSFW model yet), you can enable the environment variable for when it becomes available:
ENABLE_AI_NSFW_DETECTION='true'For now, you'll need to integrate an external service like Sightengine or Replicate if you want NSFW detection. See the configuration guide in the source code for implementation details.
Advanced Configuration
To fine-tune it, edit AI_PROCESSING in packages/config/src/ai-classification.ts, then redeploy:
| Setting | Default | What it does |
|---|---|---|
DOWNSCALE_WIDTH | 384 | Width images are shrunk to before analysis |
MAX_LABELS | 10 | Most labels stored per image |
MIN_SCORE | 0.1 | Lowest confidence a label needs to be kept (0-1) |
TIMEOUT_MS | 30000 | How long to wait for the AI before giving up |
BATCH_SIZE | 5 | Images analysed per queue batch |
REANALYSIS_COOLDOWN_DAYS | 28 | How soon an image can be analysed again |
How It Works
When an image finishes uploading, the variant generation queue automatically triggers AI analysis:
sequenceDiagram
participant Upload as Upload Complete
participant Queue as Variant Queue
participant AI as Workers AI
participant D1 as D1 Database
Upload->>Queue: Image uploaded
Queue->>Queue: Fetch original from R2
Queue->>Queue: Downscale to 384px
Queue->>AI: Detect objects (DETR)
AI-->>Queue: Labels + scores
Queue->>AI: Generate caption (LLaVA)
AI-->>Queue: Text description
Queue->>D1: Store in image_ai table
Note over Queue,D1: Results available via APIModels Used
- Classification:
@cf/facebook/detr-resnet-50- Detects the objects in an image - Captioning:
@cf/llava-hf/llava-1.5-7b-hf- Writes a short description of the image - Embeddings:
@cf/baai/bge-base-en-v1.5- Creates 768-dimensional vectors for semantic search (if Vectorize is enabled)
Processing Pipeline
- Image is downscaled to 384px width to reduce processing costs
- The DETR model detects objects, which become labels (e.g., "cat", "bench", "tree")
- The LLaVA model writes a caption (e.g., "A ginger cat sitting on a wooden fence")
- Labels are filtered by confidence score (default: minimum 0.1) and limited to top 10
- Results stored in the
image_aitable with metadata:labels_json- Array of label stringslabel_scores_json- Corresponding confidence scorescaption- Generated descriptionmodel_version- Which models were usedprocessing_time_ms- Performance trackinganalyzed_at- Timestamp (used for re-analysis cooldown)
Search Integration
Labels and captions are added to the full-text search index, so searching for dog finds photos with a dog in them, even if nobody tagged them.
If Vectorize is on, captions are also stored as embeddings, ready for semantic search. Search doesn't use them yet, though.
Re-analysis Cooldown
To prevent excessive AI costs, images can only be re-analysed once every 28 days. You can change this with REANALYSIS_COOLDOWN_DAYS (see above).
Cost Optimisation
AI classification uses Cloudflare Workers AI, which is billed in Neurons. Every account gets 10,000 Neurons a day for free, and on the Workers Paid plan, anything over that costs $0.011 per 1,000. Each model uses a different number of Neurons per image, so check the Workers AI page in your Cloudflare dashboard to see what your instance actually uses. See Cloudflare's Workers AI pricing for the latest.
To reduce costs:
- Downscale images - Smaller input = faster processing. Default is 384px which provides good accuracy.
- Increase
MIN_SCORE- Fewer labels stored = smaller database, though fewer images will match searches. - Disable captioning - Edit
packages/api/src/services/ai-classification.tsand skip therunCaptioning()call if you don't need it. - Disable semantic search - Set
ENABLE_VECTORIZE='false'to skip embedding generation entirely.
Limitations
- Colour extraction is currently disabled (requires image decoder library not available in Workers)
- NSFW detection requires external integration (Cloudflare doesn't provide a native model yet)
- Local development doesn't support Vectorize embeddings (remote-only)
- Re-analysis has a 28-day cooldown to prevent abuse
Disabling
To disable AI classification entirely:
ENABLE_AI_CLASSIFICATION='false'Images will upload normally but won't be analysed. Existing AI data remains in the database.