Clip interrogator captioning

From wenmin-wu/ds-skills · View interactive version

Clip interrogator captioning is an AI agent skill from the GitHub repository wenmin-wu/ds-skills. Generate descriptive text prompts from images by combining BLIP captioning with CLIP cosine similarity against curated label banks for medium, movement, and flavor attributes

How to use this skill

Skill file

CLIP Interrogator Captioning

Overview

When you need to reverse-engineer or describe an image as a text prompt (e.g., for image-to-prompt tasks), CLIP Interrogator combines two models: BLIP generates a base caption, then CLIP matches the image embedding against precomputed text embeddings from curated label banks (mediums, movements, flavors). The top-matching labels are appended to the caption, producing a rich prompt that captures style, medium, and content.

Quick Start

import torch
from clip_interrogator import Config, Interrogator

ci = Interrogator(Config(clip_model_name="ViT-L-14/openai"))
cos = torch.nn.CosineSimilarity(dim=1)

mediums_features = torch.stack([torch.from_numpy(t) for t in ci.mediums.embeds]).to(ci.device)
movements_features = torch.stack([torch.from_numpy(t) for t in ci.movements.embeds]).to(ci.device)
flavors_features = torch.stack([torch.from_numpy(t) for t in ci.flavors.embeds]).to(ci.device)

def interrogate(image):
    caption = ci.generate_caption(image)
    feat = ci.image_to_features(image)
    medium = ci.mediums.labels[cos(feat, mediums_features).topk(1).indices[0]]
    movement = ci.movements.labels[cos(feat, movements_features).topk(1).indices[0]]
    flavors = ", ".join([ci.flavors.labels[i] for i in cos(feat, flavors_features).topk(3).indices])
    return f"{caption}, {medium}, {movement}, {flavors}"

Workflow

  1. Load BLIP (captioning) and CLIP (embedding) models via clip_interrogator
  2. Precompute text embeddings for all label banks (mediums, movements, flavors)
  3. For each image: generate BLIP caption, extract CLIP image features
  4. Compute cosine similarity against each label bank, take top-k matches
  5. Concatenate caption + matched labels into a single prompt string
  6. Optionally truncate to fit the CLIP tokenizer's max length (77 tokens)

Key Decisions

References

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.