Skip to main content

A command-line tool to index images using SigLIP2 models and generate embeddings

Project description

Image Indexer v3.0

Image Indexer is a powerful and flexible command-line tool designed to create a vector index from your local image directories. It uses state-of-the-art vision models from the Hugging Face library to generate embeddings for each image, storing them in an efficient format for use in similarity search, image retrieval, and other machine learning applications.

This tool is powered by Siglip 2, a powerful vision model from Google, to generate high-quality embeddings for your images.


Key Features ✨

  • High-Quality Embeddings: Leverages the google/siglip2-base-patch16-naflex model to generate rich image embeddings.
  • Efficient Re-indexing: Save time by only processing new or modified images since the last run. The tool automatically detects changes and updates the index accordingly.
  • Flexible Configuration: Use a simple YAML file or command-line arguments to customize everything from input directories and model selection to batch size and hardware acceleration.
  • Hardware Acceleration: Supports processing on CPU and CUDA (NVIDIA GPUs), with an auto mode to intelligently select the best available device.
  • Multiple Output Formats: Save your index as either a jsonl file for easy inspection and parsing or a pkl (pickle) file for efficient loading in Python applications.
  • Resilient and Recoverable: The indexing process is designed to be robust. If it's interrupted, it creates a .tmp file that allows it to resume from where it left off, preventing loss of progress.
  • Rich Metadata Extraction: In addition to the vector, the tool extracts and stores useful metadata for each image, including the file path, size, creation/modification times, image dimensions, and EXIF data.

Installation 💻

To get started, clone the repository and install the required dependencies.

# Clone the repository (not shown, but assumed)
# pip install -e .

The tool requires Python 3.9+ and the dependencies listed in pyproject.toml:

  • torch>=2.0.0
  • transformers>=4.30.0
  • Pillow>=10.0.0
  • PyYAML>=6.0
  • tqdm>=4.60.0
  • pillow-heif>=0.10.0 (for HEIC/HEIF support)

Configuration ⚙️

You can configure the Image Indexer using a YAML file. A config.example.yaml is provided to get you started.

# ----------------------------------------------------
# Example Configuration for the Image Indexer v3.0
# ----------------------------------------------------

# -- Input and Output Paths --
# List of directories to scan for images recursively.
input_dirs:
  - /path/to/your/images/collection1
  # - /path/to/your/images/collection2

# Path to the output index file.
output_file: "image_index.jsonl"

# -- Model and Processing Configuration --
# Hugging Face model name for vision embeddings.
model_name: "google/siglip2-base-patch16-naflex"

# Number of images to process in a single batch.
# Adjust based on your GPU memory.
batch_size: 32

# Hardware device to use.
# Options: "auto", "cpu", "cuda".
# "auto" will attempt to use GPU if available.
device: "auto"

# -- Output Format --
# Format for the output file.
# Options: "jsonl", "pkl"
output_format: "jsonl"

# -- Logging --
# Logging level.
# Options: "DEBUG", "INFO", "WARNING", "ERROR"
log_level: "INFO"

Important Note: Arguments provided via the command-line interface (CLI) will override any values specified in the YAML configuration file.


Usage 🚀

The tool is run from the command line. You can provide a configuration file or override settings using command-line arguments.

Basic Indexing

The most straightforward way to run the indexer is by pointing it to your image directories.

image-indexer -i /path/to/your/images --output-file my_index.jsonl

Using a Configuration File

For more complex setups, use a YAML configuration file.

image-indexer -c config.yaml

Output Formats and Structure

The tool supports two output formats: jsonl and pkl.

  • jsonl (JSON Lines): This format is human-readable and easy to parse with standard command-line tools. Each line in the file is a valid JSON object representing a single image.
  • pkl (Pickle): This is a binary format that serializes Python objects. It is highly efficient for reading and writing in Python, making it ideal for large datasets. The file consists of a sequence of serialized Python dictionary objects, one for each image. To read it, you will need to load the objects from the file in a loop until you reach the end.

Each record saved in the index file, whether in jsonl or pkl format, is a Python dictionary with the following structure.

{
  "vector": [0.0123, -0.0456, ..., 0.0789],  # List of floats representing the image embedding
  "metadata": {
    "path": "/path/to/your/image.jpg",      # Full path to the image file
    "filename": "image.jpg",               # Name of the image file
    "size_bytes": 123456,                   # Size of the file in bytes
    "creation_time": "2023-10-27T10:00:00", # ISO formatted creation timestamp of the file
    "modification_time": "2023-10-27T10:00:00", # ISO formatted modification timestamp of the file
    "width": 1920,                          # Width of the image in pixels
    "height": 1080,                         # Height of the image in pixels
    "exif": { ... }                         # Dictionary with EXIF data extracted from the image
  }
}

Re-indexing and Recovery

Re-indexing

To update an existing index with new or modified images, use the --reindex flag. The tool will compare the images on disk with the records in the source index and perform the following actions:

  • New files: Images found on disk but not in the index will be processed and added.
  • Modified files: Images whose modification timestamp on disk is different from the one stored in the index will be re-processed.
  • Deleted files: Records in the index that no longer have a corresponding file on disk will be removed.
image-indexer -c config.yaml --reindex

You can also specify a different source index for re-indexing using --source-index.

image-indexer --reindex --source-index /path/to/old_index.jsonl -i /path/to/images -o /path/to/new_index.jsonl

Recovery

If the indexing process is interrupted (e.g., due to a power outage or manual cancellation), the tool will leave a temporary file with a .tmp extension. When you run the tool again with the same configuration, it will detect this file and automatically resume the process from where it left off, ensuring that no progress is lost.


Command-Line Arguments

All options in the config file can be overridden via command-line arguments.

Argument Description Default (from config)
-c, --config-file Path to a YAML configuration file. None
-i, --input-dirs One or more directories to scan for images. None
-o, --output-file Path for the output index file. None
--model-name Hugging Face model name for vision embeddings. google/siglip2-base-patch16-naflex
--batch-size Number of images to process in a single batch. 32
--device Device to use for processing (auto, cpu, cuda). auto
--output-format Format for the output file (jsonl, pkl). jsonl
--log-level Set the logging level (DEBUG, INFO, WARNING, ERROR). INFO
--reindex Enable re-indexing mode. Only processes new or modified files. False
--source-index Path to an existing index to use as the source for re-indexing. output_file

License 📄

This project is licensed under the MIT License. See the pyproject.toml file for details.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

siglip2_image_indexer-1.0.0.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

siglip2_image_indexer-1.0.0-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file siglip2_image_indexer-1.0.0.tar.gz.

File metadata

  • Download URL: siglip2_image_indexer-1.0.0.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.12.7

File hashes

Hashes for siglip2_image_indexer-1.0.0.tar.gz
Algorithm Hash digest
SHA256 2299b1aa7cdc3abb3490dc2b31ac351ee37c10583f276fb4689a1d3f618d6fd0
MD5 419049d778be5577581203d138a865e3
BLAKE2b-256 556352cfeb535d663b123c04e327645b7ff693c2eb9801163542262f0a787300

See more details on using hashes here.

File details

Details for the file siglip2_image_indexer-1.0.0-py3-none-any.whl.

File metadata

File hashes

Hashes for siglip2_image_indexer-1.0.0-py3-none-any.whl
Algorithm Hash digest
SHA256 1888c0cf7c19c67b555d716e8443a36b167292d9e5eac708d127d9e9311e7adb
MD5 d465d0e43cd30fce797d941502089db7
BLAKE2b-256 77a9d0d2492166ad083e46e5b6a37b84cad4c1395d2b7fe092de2843ceba4185

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page