Computer Vision

X-Ray-Image-Search-System

Python Created Feb 2026 Updated Feb 2026

šŸ” X-Ray Image Search System

A production-ready prototype for searching medical X-ray images using both text descriptions and visual similarity. Built with state-of-the-art deep learning models for medical imaging applications.


šŸš€ Features

āœ… Core Functionality

  • Text-Based Search: Find X-rays using natural language (e.g., "chest X-ray with pneumonia")
  • Image-Based Search: Upload an X-ray to find visually similar images
  • Dual Model Architecture:
    • CLIP (ViT-B/32) for text-to-image search
    • ResNet50 for image-to-image similarity
  • GPU Acceleration: Automatic CUDA detection and usage
  • Smart Caching: Embeddings cached for fast subsequent searches
  • Category Filtering: Filter results by anatomical categories
  • Similarity Thresholding: Control result quality with adjustable thresholds

šŸŽ Bonus Features

  • Ranking scores displayed for all results
  • Category filters (chest, spine, dental, etc.)
  • Real-time similarity metrics
  • Responsive grid layout
  • Dataset statistics dashboard

šŸ“ Project Structure

xray-search-system/
ā”œā”€ā”€ data_processor.py      # Dataset handling & embedding generation
ā”œā”€ā”€ search_engine.py       # Search logic (text & image)
ā”œā”€ā”€ app.py                 # Streamlit UI
ā”œā”€ā”€ requirements.txt       # Python dependencies
ā”œā”€ā”€ README.md             # This file
ā”œā”€ā”€ dataset/              # Dataset directory (you create this)
│   ā”œā”€ā”€ images/           # X-ray images (.png)
│   ā”œā”€ā”€ metadata.csv      # Auto-generated metadata
│   ā”œā”€ā”€ resnet_embeddings.pkl   # Cached ResNet embeddings
│   └── clip_embeddings.pkl     # Cached CLIP embeddings
└── screenshots/          # Demo screenshots (optional)

šŸš€ Quick Start

1ļøāƒ£ Installation

# Clone or download this project
cd xray-search-system

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

# Install dependencies
pip install -r requirements.txt

2ļøāƒ£ Dataset Setup

Option A: NIH Chest X-ray Dataset (Recommended)

Visit: https://nihcc.app.box.com/v/ChestXray-NIHCC Download images_001.tar.gz (~7GB, contains ~4,600 images) Extract to dataset/images/

Option B: Custom Dataset

Create this structure:

dataset/
ā”œā”€ā”€ images/
│   ā”œā”€ā”€ image1.png
│   ā”œā”€ā”€ image2.png
│   └── ...
└── metadata.csv (optional)

3ļøāƒ£ Run the Application

streamlit run app.py

šŸ’» Usage

Text Search

  1. Select "Text Search" mode
  2. Enter a query like:
    • "chest x-ray with pneumonia"
    • "normal chest radiograph"
    • "lung nodule"
  3. Adjust filters (number of results, similarity threshold, category)
  4. Click "Search"

Image Search

  1. Select "Image Search" mode
  2. Upload an X-ray image (PNG, JPG, JPEG)
  3. Adjust filters
  4. Click "Find Similar Images"

🧠 Technical Details

Architecture

Text Search Pipeline:

Text Query → CLIP Text Encoder → Text Embedding (512-dim)
                                        ↓
                              Cosine Similarity
                                        ↓
              Database CLIP Image Embeddings (N x 512)
                                        ↓
                              Top-K Results

Image Search Pipeline:

Query Image → ResNet50 → Feature Embedding (2048-dim)
                                ↓
                      Cosine Similarity
                                ↓
         Database ResNet Embeddings (N x 2048)
                                ↓
                        Top-K Results

šŸ› Troubleshooting

Issue: "Dataset not found"

Solution: Ensure dataset/images/ directory exists with PNG/JPG files

Issue: "CUDA out of memory"

Solution: System will auto-fallback to CPU

Issue: "No module named 'clip'"

Solution: Install CLIP from GitHub:

pip install git+https://github.com/openai/CLIP.git

šŸ“ License & Attribution

Dataset: NIH Chest X-ray Dataset

  • License: Public Domain
  • Source: NIH Clinical Center

Code: MIT License


šŸ¤ Contributing

For improvements or bug fixes:

  1. Test on subset (100 images) first
  2. Verify GPU and CPU modes both work
  3. Check embeddings cache correctly
  4. Ensure UI remains responsive

šŸ“§ Support

For issues:

  1. Check "Troubleshooting" section above
  2. Verify dataset setup is correct
  3. Ensure all dependencies installed
  4. Check GPU drivers if using CUDA

šŸŽ‰ Acknowledgments

  • OpenAI for CLIP model
  • NIH Clinical Center for chest X-ray dataset
  • PyTorch and Streamlit teams

Built with ā¤ļø for medical image retrieval