Skip to main content

A lightweight Python library for generating image embeddings with semantic search

Project description

Imgemb

PyPI version Python 3.8+ License: MIT

A lightweight Python library for generating and comparing image embeddings, featuring semantic search capabilities powered by OpenCLIP.

Features

  • Multiple Embedding Methods:

    • Average Color: Simple RGB/HSV color averaging
    • Grid-based: Divide image into grid cells and compute color statistics
    • Edge-based: Extract edge features using Sobel operators
    • Semantic Search: Utilize OpenCLIP for advanced image understanding
  • Command Line Interface (CLI): Easy-to-use commands for:

    • Generating embeddings
    • Comparing images
    • Finding similar images
    • Batch processing directories
  • Python API: Flexible and intuitive API for integration into your projects

  • Efficient Processing: Optimized for both CPU and GPU environments

Installation

From PyPI

pip install imgemb

From Source

git clone https://github.com/aryanraj2713/image_embeddings.git
cd image_embeddings
pip install -e .

Quick Start

Command Line Usage

  1. Generate embeddings for an image:
imgemb generate input.jpg --method grid --grid-size 3 3
  1. Compare two images:
imgemb compare image1.jpg image2.jpg --method average
  1. Find similar images in a directory:
imgemb find-similar query.jpg images_dir/ --top-k 5

Python API

Basic Usage

from imgemb import ImageEmbedder, SemanticSearcher

# Generate embeddings using different methods
embedder = ImageEmbedder(method="grid", grid_size=(3, 3))
embedding = embedder.embed_image("path/to/image.jpg")

# Semantic search
searcher = SemanticSearcher()
searcher.index_directory("path/to/image/directory")
results = searcher.search("a photo of a dog", top_k=5)

Advanced Usage

# Custom grid size with normalization
embedder = ImageEmbedder(
    method="grid",
    grid_size=(4, 4),
    normalize=True,
    color_space="hsv"
)

# Batch processing with edge detection
embedder = ImageEmbedder(method="edge")
embeddings = embedder.embed_directory(
    "path/to/directory",
    extensions=[".jpg", ".png"]
)

# Semantic search with threshold
searcher = SemanticSearcher(device="cuda")  # Use GPU if available
searcher.index_directory("image_database")
results = searcher.search(
    "abstract art",
    top_k=10,
    threshold=0.7  # Only return results with similarity > 0.7
)

API Documentation

ImageEmbedder

Methods

  • __init__(method="average", grid_size=None, normalize=False, color_space="rgb")

    • method: Embedding method ("average", "grid", "edge")
    • grid_size: Tuple of (rows, cols) for grid method
    • normalize: Whether to normalize embeddings
    • color_space: Color space to use ("rgb" or "hsv")
  • embed_image(image_path: str) -> np.ndarray

    • Generates embedding for a single image
    • Returns numpy array of embedding values
  • embed_directory(directory: str, extensions: List[str] = None) -> Dict[str, np.ndarray]

    • Generates embeddings for all images in directory
    • Returns dictionary mapping file paths to embeddings

SemanticSearcher

Methods

  • __init__(device="cuda", model_name="ViT-B-32")

    • device: Computing device ("cuda" or "cpu")
    • model_name: OpenCLIP model variant
  • index_directory(directory: str, extensions: List[str] = None)

    • Indexes all images in specified directory
    • Optional file extension filtering
  • search(query: str, top_k: int = 5, threshold: float = 0.0) -> List[Tuple[str, float]]

    • Searches for images matching text query
    • Returns list of (image_path, similarity_score) tuples

Development

Setup Development Environment

# Install development dependencies
pip install -e ".[dev]"

# Run tests
pytest tests/

# Format code
black image_embeddings/ tests/

# Run linter
flake8 image_embeddings/ tests/

Running Tests

# Run all tests
pytest

# Run with coverage report
pytest --cov=image_embeddings

# Generate HTML coverage report
pytest --cov=image_embeddings --cov-report=html

Requirements

  • Python 3.8+
  • PyTorch 2.0+
  • OpenCV 4.5+
  • NumPy 1.19+
  • Pillow 9.0+
  • open-clip-torch 2.20+

Contributing

  1. Fork the repository
  2. Create your feature branch (git checkout -b feature/amazing-feature)
  3. Commit your changes (git commit -m 'Add amazing feature')
  4. Push to the branch (git push origin feature/amazing-feature)
  5. Open a Pull Request

License

This project is licensed under the MIT License - see the LICENSE file for details.

Citation

If you use this library in your research, please cite:

@software{imgemb,
  author = {Aryan Raj},
  title = {Imgemb: A Lightweight Python Library for Image Embeddings},
  year = {2024},
  publisher = {GitHub},
  url = {https://github.com/aryanraj2713/image_embeddings}
}

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

imgemb-0.2.2.tar.gz (40.4 kB view details)

Uploaded Source

Built Distribution

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

imgemb-0.2.2-py3-none-any.whl (12.6 kB view details)

Uploaded Python 3

File details

Details for the file imgemb-0.2.2.tar.gz.

File metadata

  • Download URL: imgemb-0.2.2.tar.gz
  • Upload date:
  • Size: 40.4 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for imgemb-0.2.2.tar.gz
Algorithm Hash digest
SHA256 ce4043b28e21e7e6fc1c73f058deaac722ab41408e0d3875683da0662b5a5cb9
MD5 1fb884f3bd4016e8c2afb7b95f213338
BLAKE2b-256 7b910a3f6deac75215199a33e53eab45946081de765fff94e70054cf38e19301

See more details on using hashes here.

File details

Details for the file imgemb-0.2.2-py3-none-any.whl.

File metadata

  • Download URL: imgemb-0.2.2-py3-none-any.whl
  • Upload date:
  • Size: 12.6 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.12.3

File hashes

Hashes for imgemb-0.2.2-py3-none-any.whl
Algorithm Hash digest
SHA256 17760986b172a4dd1825a46bff6a1c3dfda2463ad768e27d6e1de3490bc9fd7b
MD5 9d224a59808966acb82dd134c13e72e3
BLAKE2b-256 06ae78b3148fd181dd865bab0d4e1793afbc6f79fceb3870d9db614cb329e48c

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