A comprehensive computer vision toolkit for image quality assessment, blur detection, and duplicate image identification using OpenCV, PyTorch, and deep learning models.
ClairVision provides multiple tools for image processing and analysis:
- Blur Detection - Identify blurry vs. sharp images using Laplacian variance and Resnet50
- Duplicate Detection - Find and group similar/duplicate images using CLIP embeddings
- Image Quality Assessment - Evaluate image quality using neural networks
clairvisionV0.11/
βββ BlurDetection2/ # Blur detection module
β βββ blur_detection/
β β βββ detection.py # Core blur detection algorithm
β β βββ best_blur_detector.pth # Pre-trained blur detection model
β β βββ resenet50.ipynb # ResNet50 model notebook
β β βββ images_test/ # Test images for blur detection
β βββ process.py # Main blur detection processing script
β βββ sort_blur.py # Script to sort images into sharp/blurry folders
β βββ results.json # Output file with blur detection results
β
βββ model3/ # Duplicate detection module
β βββ duplicate_detection.py # CLIP-based duplicate detection
β βββ dup_detc_adv.py # Advanced duplicate detection (commented - future enhancement)
β βββ input_images/ # Input images for duplicate detection
β βββ duplicates_sorted/ # Output: grouped duplicates
β βββ duplicateeeeeeee_sorted/ # Output: alternative grouping results
β βββ results/ # Duplicate detection results
β
βββ best_pick/ # Additional utilities
β βββ scripts/ # Processing scripts
β βββ face_detect/ # Face detection utilities
β
βββ .gitignore # Git ignore rules
βββ .venv/ # Virtual environment (excluded from git)
βββ README.md # This file
Algorithm: Laplacian variance-based blur detection
- Method: Computes variance of Laplacian edges in image
- Default Threshold: 100 (configurable)
- Optimal Threshold (for reference dataset): 207.95
- Input: Single images or directory of images
- Output: JSON file with blur scores and classification
Key Files:
detection.py: Containsestimate_blur()function using OpenCVprocess.py: Batch processing script with CLI argumentssort_blur.py: Organizes results into sharp/blurry directories
Usage:
# Basic blur detection
python process.py -i path/to/images -s results.json
# With custom threshold
python process.py -i path/to/images -t 207.95 -s results.json
# With verbose output
python process.py -i path/to/images -t 207.95 -v
# Sort results into directories
python sort_blur.pyOutput Structure:
{
"images": ["path/to/images"],
"threshold": 207.95,
"fix_size": true,
"results": [
{
"input_path": "image.jpg",
"score": 250.5,
"blurry": false
}
]
}Algorithm: CLIP-based semantic similarity with cosine distance
- Model: Vision Transformer (ViT-B/32) via OpenCV CLIP
- Similarity Threshold: 0.93 (configurable)
- Method: Embeddings extraction + cosine similarity matrix
- Input: Directory of images
- Output: Grouped duplicates in separate folders
Key Files:
duplicate_detection.py: Main duplicate detection using CLIPdup_detc_adv.py: Advanced multi-model approach (hybrid CLIP + EfficientNet - commented)
Usage:
# Run duplicate detection
python duplicate_detection.py
# Results saved to: duplicateeeeeeee_sorted/Configuration (in duplicate_detection.py):
input_folder = "input_images" # Input image directory
output_folder = "duplicateeeeeeee_sorted"
similarity_threshold = 0.93 # 0.9-0.95 recommended
device = "cuda" if torch.cuda.is_available() else "cpu"Neural network-based image quality evaluation (NIMA-style models)
- Multiple architecture support (InceptionResNet, MobileNet, NASNet)
- Feature extraction and scoring pipeline
- Training and evaluation scripts available
opencv-python
numpy
torch
torchvision
clip
scikit-learn
pillow
tqdm
# Create virtual environment
python -m venv venv
# Activate (Windows)
venv\Scripts\activate
# Activate (Linux/Mac)
source venv/bin/activate
# Install dependencies
pip install -r requirements.txtThe optimal threshold depends on your dataset characteristics:
Reference Dataset Analysis (700 images):
- Score Range: 14.77 - 9300.36
- Mean: 430.24
- Median: 207.06
- Standard Deviation: 718.83
- Percentiles:
- 10th: 58.69
- 25th: 104.94
- 50th: 207.06
- 75th: 466.62
- 90th: 1038.21
Threshold Selection:
- For ~50% sharp/blur split: Use 207.95 (median)
- For stricter quality: Increase threshold (e.g., 400+)
- For more lenient: Decrease threshold (e.g., 100-150)
Formula: Images with score β₯ threshold = SHARP; score < threshold = BLURRY
python BlurDetection2/process.py -i input_folder -t 207.95 -s results.jsonpython BlurDetection2/sort_blur.py
# Output: sorted_output/sharp/ and sorted_output/blurry/# Copy sharp images to model3/input_images/
python model3/duplicate_detection.py
# Output: duplicates grouped in duplicateeeeeeee_sorted/| File | Size | Purpose | Location |
|---|---|---|---|
resenet50.ipynb |
~50KB | ResNet50 model weights notebook | BlurDetection2/blur_detection/ |
best_blur_detector.pth |
~244MB | Pre-trained blur detection model | BlurDetection2/blur_detection/ |
Note: Model weight files (*.h5, *.pth, *.pt) are excluded from git except ResNet50 weights (tracked via .gitignore exception).
- Speed: ~10-50ms per image (CPU)
- Speed: ~5-20ms per image (GPU)
- Memory: Minimal (~50MB)
- Speed: Highly dependent on number of images and GPU availability
- Bottleneck: CLIP embedding extraction (~100-200ms per image)
- Memory: ~2GB for 1000 images (GPU)
- Use
--variable-sizeflag to skip image normalization (faster but less consistent scoring) - Enable GPU for duplicate detection (30-50x speedup)
- Process images in batches for better resource utilization
Processing complete:
- Sharp images: 179 (25.6%)
- Blurry images: 176 (25.1%)
- Total processed: 355 images
- Threshold used: 207.95
- Score range: 14.77 - 9300.36
Processing complete:
- Total images processed: 350
- Duplicate groups found: 12
- Largest group: 5 images
- Average group size: 2.3 images
from blur_detection.detection import estimate_blur
import cv2
image = cv2.imread('test.jpg')
blur_map, score, is_blurry = estimate_blur(image, threshold=207.95)
print(f"Blur score: {score}, Blurry: {is_blurry}")import json
from pathlib import Path
thresholds = [100, 150, 200, 250, 300]
for threshold in thresholds:
os.system(f'python process.py -i images/ -t {threshold} -s results_{threshold}.json')"CUDA out of memory" during duplicate detection
- Reduce batch size or process fewer images at once
- Use CPU mode: Set
device = "cpu"in duplicate_detection.py
"No module named 'clip'"
- Install:
pip install git+https://github.com/openai/CLIP.git
Low blur detection accuracy
- Dataset-specific thresholds needed
- Run analyze_threshold.py to find optimal threshold for your data
Images not being copied in sort_blur.py
- Verify image paths in results.json are correct
- Check file permissions and directory existence
- Advanced duplicate detection combining CLIP + EfficientNet (see dup_detc_adv.py)
- Face detection and quality assessment
- Multi-model ensemble for blur detection
- Real-time video stream processing
- Web API interface for remote processing