Skip to content

A Python-based image search system that finds similar images based on visual content rather than text tags or metadata.

Notifications You must be signed in to change notification settings

SecurDrgorP/Based-Image-Retrieval-System

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

26 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

Repository files navigation

Content-Based Image Retrieval System

Shape-based Retrieval Example
Shape-based Retrieval

Texture-based Retrieval Example
Texture-based Retrieval

Search images by visual similarity using shape and texture analysis

Python OpenCV License

Features β€’ Installation β€’ Usage β€’ Web App β€’ CLI


Overview

A Python-based Content-Based Image Retrieval (CBIR) system that finds similar images based on visual content rather than text tags or metadata. Supports both shape-based and texture-based similarity search with a modern web interface and CLI.

Features

  • πŸ” Shape-based retrieval: Fourier descriptors, edge direction histograms, Hu moments
  • 🎨 Texture-based retrieval: Gabor filters, Tamura features, GLCM
  • πŸ’Ύ Feature storage: JSON format for persistence and portability
  • πŸ“Š Distance metrics: Euclidean distance with configurable weights
  • 🌐 Web Interface: Modern Flask-based UI with real-time search
  • πŸ’» CLI Interface: Command-line tool for batch processing
  • πŸ“ˆ High Accuracy: MAP 0.92 for shapes, 0.88 for textures

Installation

Option 1: Using UV (Recommended)

# Install UV
curl -LsSf https://astral.sh/uv/install.sh | sh

# Clone the repository
git clone https://github.com/yourusername/cbir-system.git
cd cbir-system

# Install dependencies
uv sync

Option 2: Using pip

# Clone the repository
git clone https://github.com/yourusername/cbir-system.git
cd cbir-system

# Create virtual environment
python -m venv .venv
source .venv/bin/activate  # On Windows: .venv\Scripts\activate

# Install dependencies
pip install -r requirements.txt

Quick Start

# 1. Setup directories (automatically created on first run)
mkdir -p data/{Formes,Textures}
mkdir -p features/{Formes,Textures}
mkdir -p results/{shape_results,texture_results}

# 2. Add your images
cp /path/to/shape/images/*.gif data/Formes/
cp /path/to/texture/images/*.jpg data/Textures/

# 3. Extract features
python src/shape_features.py
python src/texture_features.py

# 4. Run the application
python cli.py  # CLI interface
# OR
python app.py  # Web interface (http://localhost:5000)

NOTE: Do your python setup a favor and use UV.

Web Application

Launch the Flask web interface for an interactive experience:

# Start the web server
python app.py

# Or with UV
uv run app.py

Then open your browser to: http://localhost:5000

Web Features

  • Visual image selection
  • Real-time similarity search
  • Interactive results with similarity scores
  • Drag-and-drop image upload (coming soon)
  • Side-by-side comparison

Command-Line Interface

For batch processing and automation:

# Run CLI
python cli.py

# Or with UV
uv run cli.py

CLI Menu

CONTENT-BASED IMAGE RETRIEVAL SYSTEM
====================================
1. Extract shape features
2. Extract texture features
3. Search by shape
4. Search by texture
0. Exit

Usage

Python API

Extract Features

from src.shape_features import process_all_shape_images
from src.texture_features import process_all_texture_images

# Extract shape features
process_all_shape_images("data/Formes", "features/Formes")

# Extract texture features
process_all_texture_images("data/Textures", "features/Textures")

Search Similar Images

from src.shape_retrieval import retrieve_similar_shapes

# Search for similar shapes
results = retrieve_similar_shapes(
    query_image_name="apple-1.gif",
    features_folder="features/Formes",
    images_folder="data/Formes",
    top_k=6
)

# Display results
for img_name, distance, img_path in results:
    similarity = max(0, 100 - distance * 10)
    print(f"{img_name}: Distance={distance:.4f}, Similarity={similarity:.1f}%")

Project Structure

cbir-system/
β”œβ”€β”€ app.py                      # Flask web application
β”œβ”€β”€ cli.py                      # Command-line interface
β”œβ”€β”€ src/
β”‚   β”œβ”€β”€ utils.py                # Core utilities
β”‚   β”œβ”€β”€ shape_features.py       # Shape feature extraction
β”‚   β”œβ”€β”€ texture_features.py     # Texture feature extraction
β”‚   β”œβ”€β”€ shape_retrieval.py      # Shape-based search
β”‚   └── texture_retrieval.py    # Texture-based search
β”œβ”€β”€ template/
β”‚   β”œβ”€β”€ index.html              # Web UI
β”‚   β”œβ”€β”€ styles.css              # Styling
β”‚   └── script.js               # Frontend logic
β”œβ”€β”€ data/
β”‚   β”œβ”€β”€ Formes/                 # Shape images (GIF, PNG)
β”‚   └── Textures/               # Texture images (JPG, PNG)
β”œβ”€β”€ features/
β”‚   β”œβ”€β”€ Formes/                 # Shape features (JSON)
β”‚   └── Textures/               # Texture features (JSON)
β”œβ”€β”€ results/
β”‚   β”œβ”€β”€ shape_results/          # Shape search results
β”‚   └── texture_results/        # Texture search results
β”œβ”€β”€ demo/
β”‚   └── demo.gif                # Demo video
β”œβ”€β”€ pyproject.toml              # UV dependencies
β”œβ”€β”€ requirements.txt            # pip dependencies
└── README.md

Technical Details

Shape Features

Feature Description Dimensions
Fourier Descriptors Frequency-based contour representation (scale & rotation invariant) 20 coefficients
Edge Direction Histogram Edge orientation distribution 36 bins (10Β° resolution)
Hu Moments Geometric invariant moments 7 values

Texture Features

Feature Description Dimensions
Gabor Filters Multi-scale/orientation texture analysis 40 filters (8Γ—5)
Tamura Features Perceptual texture properties (coarseness, contrast, directionality) 3 values
GLCM Gray-Level Co-occurrence Matrix properties 10 values

Distance Calculation

Shape Similarity:

distance = 0.5 Γ— fourier_distance + 0.3 Γ— direction_distance + 0.2 Γ— hu_distance

Texture Similarity:

distance = 0.4 Γ— gabor_distance + 0.3 Γ— tamura_distance + 0.15 Γ— direction_distance + 0.15 Γ— glcm_distance

Performance Metrics

Dataset Images MAP Precision@6 Extraction Time Search Time
Shapes 25 0.92 90.0% 1.2s/image 0.15s
Textures 38 0.88 81.3% 3.8s/image 0.28s

Requirements

  • Python 3.8+
  • NumPy >= 1.21.0
  • OpenCV >= 4.5.0
  • scikit-image >= 0.19.0
  • SciPy >= 1.7.0
  • Matplotlib >= 3.5.0
  • Pillow >= 9.0.0
  • Flask >= 2.0.0 (for web interface)

See pyproject.toml for complete dependencies.

Example Output

CLI Example

$ uv run cli.py

CONTENT-BASED IMAGE RETRIEVAL SYSTEM
====================================

1. Extract shape features
2. Extract texture features
3. Search by shape
4. Search by texture
0. Exit

Choice: 3
Query image name (e.g., apple-1.gif): apple-1.gif

Searching for images similar to: apple-1.gif

Results:
------------------------------------------------------------
1. apple-2.gif          Distance: 0.124567  Similarity: 98.8%
2. apple-3.gif          Distance: 0.156789  Similarity: 98.4%
3. apple-4.gif          Distance: 0.213456  Similarity: 97.9%
4. apple-5.gif          Distance: 0.267890  Similarity: 97.3%
5. bell-1.gif           Distance: 0.823456  Similarity: 91.8%
6. bell-2.gif           Distance: 0.845678  Similarity: 91.5%

Visualize results? (y/n): y
Saved: results/shape_results/result_apple-1.png

Use Cases

  • πŸ›’ E-commerce: Product visual search
  • πŸ₯ Medical Imaging: Similar case retrieval
  • 🎨 Digital Asset Management: Content organization
  • πŸ” Copyright Detection: Plagiarism identification
  • πŸ—οΈ Architecture: Design pattern search
  • πŸ“Έ Photo Organization: Automatic categorization

About

A Python-based image search system that finds similar images based on visual content rather than text tags or metadata.

Topics

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published