Skip to main content

A Python package for validating selfie image quality

Project description

Selfie Validator

A Python package for validating selfie image quality using computer vision techniques. Perfect for applications that need to ensure high-quality selfie input before processing.

Python Version License: MIT

Features

  • Face Detection: Automatically detects faces in selfie images

  • Quality Validation: Checks multiple quality factors:

    • Resolution: Ensures minimum image resolution
    • Sharpness: Validates image clarity using Laplacian variance
    • Lighting: Analyzes brightness levels for optimal visibility
    • Distance: Validates face size relative to image (not too close/far)
    • Angle: Checks head angle alignment using eye detection
    • Eye Detection: Ensures both eyes are clearly visible
  • Flexible Configuration: Customizable thresholds for all validation criteria

  • Multiple Input Formats: Supports NumPy arrays, file paths, and byte data

  • Detailed Results: Comprehensive validation results with specific metrics

  • Backward Compatibility: Drop-in replacement for existing implementations

Installation

pip install selfie-validator

Development Installation

git clone https://github.com/yourusername/selfie-validator.git
cd selfie-validator
pip install -e .

Quick Start

from selfie_validator import SelfieValidator
import cv2

# Initialize validator with default settings
validator = SelfieValidator()

# Load an image
image = cv2.imread("path/to/selfie.jpg")

# Validate the selfie
results = validator.validate(image)

# Check if validation passed
if results["valid"]:
    print("✅ Great selfie!")
else:
    print(f"❌ Issues found: {validator.get_validation_summary(results)}")

# Access detailed metrics
print(f"Face ratio: {results['face_ratio']:.3f}")
print(f"Brightness: {results['brightness']:.1f}")
print(f"Sharpness score: {results['laplacian_var']:.1f}")

Advanced Usage

Custom Configuration

from selfie_validator import SelfieValidator

# Create validator with custom thresholds
validator = SelfieValidator(
    min_resolution=(640, 640),           # Minimum image size
    sharpness_threshold=150.0,           # Higher sharpness requirement
    brightness_range=(80, 200),          # Wider brightness tolerance
    face_ratio_range=(0.15, 0.45),       # Adjust face size requirements
    max_angle_deviation=10.0             # More lenient angle tolerance
)

Different Input Types

# From file path
results = validator.validate("selfie.jpg")

# From bytes (e.g., uploaded file)
with open("selfie.jpg", "rb") as f:
    image_bytes = f.read()
results = validator.validate(image_bytes)

# From NumPy array
import cv2
image = cv2.imread("selfie.jpg")
results = validator.validate(image)

Strict vs Non-Strict Mode

# Strict mode: ALL checks must pass
try:
    results = validator.validate(image, strict_mode=True)
    print("Perfect selfie!")
except SelfieValidationError as e:
    print(f"Validation failed: {e}")

# Non-strict mode: At least 4 out of 6 checks must pass
results = validator.validate(image, strict_mode=False)
if results["valid"]:
    print("Good enough selfie!")

API Reference

SelfieValidator Class

Constructor Parameters

  • min_resolution (tuple): Minimum image resolution (width, height). Default: (480, 480)
  • sharpness_threshold (float): Minimum Laplacian variance for sharpness. Default: 100.0
  • brightness_range (tuple): Acceptable brightness range (min, max). Default: (100, 180)
  • face_ratio_range (tuple): Face area to image area ratio range. Default: (0.18, 0.50)
  • max_angle_deviation (float): Maximum allowed angle deviation in degrees. Default: 8.0

Methods

validate(image, strict_mode=True)

Validates a selfie image and returns detailed results.

Parameters:

  • image: Input image (NumPy array, file path, or bytes)
  • strict_mode (bool): If True, all checks must pass. If False, at least 4/6 checks must pass.

Returns: Dictionary with validation results:

{
    "valid": bool,              # Overall validation result
    "resolution_ok": bool,      # Resolution check result
    "sharpness_ok": bool,       # Sharpness check result
    "light_ok": bool,           # Lighting check result
    "distance_ok": bool,        # Distance check result
    "angle_ok": bool,           # Angle check result
    "eyes_ok": bool,            # Eye detection result
    "faces_detected": int,      # Number of faces found
    "brightness": float,        # Average face brightness
    "face_ratio": float,        # Face area to image area ratio
    "laplacian_var": float,     # Sharpness metric
    "angle": float              # Head angle in degrees (if detectable)
}
get_validation_summary(results)

Returns a human-readable summary of validation results.

Parameters:

  • results: Dictionary returned by validate()

Returns: String with validation summary

Backward Compatibility

For existing codebases, you can use the legacy function:

from selfie_validator import analyze_selfie_image

# Drop-in replacement for existing implementations
results = analyze_selfie_image(image_array)

Error Handling

The package provides specific exceptions for different error scenarios:

from selfie_validator import SelfieValidator
from selfie_validator.exceptions import (
    SelfieValidationError,
    InvalidImageError,
    NoFaceDetectedError
)

validator = SelfieValidator()

try:
    results = validator.validate("path/to/image.jpg", strict_mode=True)
except InvalidImageError as e:
    print(f"Image processing error: {e}")
except NoFaceDetectedError as e:
    print(f"No face found: {e}")
except SelfieValidationError as e:
    print(f"Validation error: {e}")

Quality Criteria Details

Resolution Check

  • Ensures image meets minimum size requirements
  • Default: 480x480 pixels
  • Helps guarantee sufficient detail for analysis

Sharpness Check

  • Uses Laplacian variance to measure image clarity
  • Default threshold: 100.0
  • Higher values indicate sharper images

Lighting Check

  • Analyzes average brightness in the face region
  • Default range: 100-180 (0-255 scale)
  • Ensures face is neither too dark nor overexposed

Distance Check

  • Measures face area relative to total image area
  • Default range: 18%-50% of image
  • Ensures face is appropriately sized (not too close/far)

Angle Check

  • Uses eye detection to measure head tilt
  • Default tolerance: ±8 degrees
  • Ensures face is relatively straight

Eye Detection

  • Confirms both eyes are visible and detectable
  • Essential for angle calculation
  • Indicates face is properly oriented

Examples

Integration with Web Applications

from flask import Flask, request, jsonify
from selfie_validator import SelfieValidator
import cv2
import numpy as np

app = Flask(__name__)
validator = SelfieValidator()

@app.route('/validate-selfie', methods=['POST'])
def validate_selfie():
    if 'image' not in request.files:
        return jsonify({'error': 'No image provided'}), 400
    
    file = request.files['image']
    
    # Convert uploaded file to OpenCV format
    file_bytes = np.frombuffer(file.read(), np.uint8)
    image = cv2.imdecode(file_bytes, cv2.IMREAD_COLOR)
    
    try:
        results = validator.validate(image, strict_mode=False)
        return jsonify({
            'valid': results['valid'],
            'summary': validator.get_validation_summary(results),
            'details': results
        })
    except Exception as e:
        return jsonify({'error': str(e)}), 400

Batch Processing

import os
from selfie_validator import SelfieValidator

validator = SelfieValidator()
image_folder = "path/to/selfies"

results = []
for filename in os.listdir(image_folder):
    if filename.lower().endswith(('.jpg', '.jpeg', '.png')):
        image_path = os.path.join(image_folder, filename)
        try:
            result = validator.validate(image_path, strict_mode=False)
            results.append({
                'filename': filename,
                'valid': result['valid'],
                'score': sum([
                    result['resolution_ok'],
                    result['sharpness_ok'],
                    result['light_ok'],
                    result['distance_ok'],
                    result['angle_ok'],
                    result['eyes_ok']
                ]) / 6.0  # Quality score 0-1
            })
        except Exception as e:
            print(f"Error processing {filename}: {e}")

# Sort by quality score
results.sort(key=lambda x: x['score'], reverse=True)
print("Best selfies:", [r['filename'] for r in results[:5]])

Requirements

  • Python 3.8+
  • OpenCV (opencv-python)
  • NumPy
  • Pillow (optional, for additional image format support)

Development Setup

git clone https://github.com/du2x/selfie-validator.git
cd selfie-validator

# Install in development mode with test dependencies
pip install -e ".[dev]"

# Format code
black .

# Type checking
mypy .

License

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

Changelog

v0.1.0

  • Initial release
  • Core validation functionality
  • Support for multiple input formats
  • Comprehensive test suite
  • Full documentation

Support


Made with ❤️ for better selfie validation

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

selfie_validator-0.1.0.tar.gz (11.6 kB view details)

Uploaded Source

Built Distribution

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

selfie_validator-0.1.0-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

Details for the file selfie_validator-0.1.0.tar.gz.

File metadata

  • Download URL: selfie_validator-0.1.0.tar.gz
  • Upload date:
  • Size: 11.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.1.0 CPython/3.13.0

File hashes

Hashes for selfie_validator-0.1.0.tar.gz
Algorithm Hash digest
SHA256 7ce1dc73469891ad169f1e5cca5a61b1594414954d14529a26d3a592c5ec4be8
MD5 70c07a4468dd828121372358bf820f1e
BLAKE2b-256 ee4cea9d35cad02fdf4822bd216a69792b187ffc01f90fea7a83b81c7baae0a0

See more details on using hashes here.

File details

Details for the file selfie_validator-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for selfie_validator-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 b3d53720ead3a7686540f95e9479943f7e5ad8fa8e4811aa47b9d7f0ddc2d7bd
MD5 8c60608eaf8c9c82744fa8d367c09767
BLAKE2b-256 08ea8589efc7619cf02acf792b2b82a16440dfd2f68f248cb320decdfd97265e

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