Skip to main content

Python bindings for OFIQ (Open Source Face Image Quality)

Project description

python-ofiq

Python bindings for OFIQ (Open Source Face Image Quality) — the reference implementation for ISO/IEC 29794-5.

Features

  • One-time initialization, process many images efficiently
  • Unified quality score [0-100] and 25+ individual quality metrics
  • Custom exception hierarchy for precise error handling
  • Full type hint support

Installation

pip install python-ofiq

Download OFIQ models and config (~400 MB):

python-ofiq setup

This stores data in ~/.ofiq/data. Set OFIQ_DATA_DIR to use a custom directory.

Quick Start

import cv2
from ofiq import OFIQ, FaceDetectionError

ofiq = OFIQ()

bgr = cv2.imread("face.png")
rgb = cv2.cvtColor(bgr, cv2.COLOR_BGR2RGB)

try:
    score = ofiq.scalar_quality(rgb)
    print(f"Quality score: {score}")
except FaceDetectionError:
    print("No face detected")

measures = ofiq.vector_quality(rgb)
print(measures)
# {'UnifiedQualityScore': 85.2, 'Sharpness': 92.1, ...}

measures_with_raw = ofiq.vector_quality(rgb, include_raw=True)
print(measures_with_raw["Sharpness"])
# {'scalar': 92.1, 'raw': 0.847}

Exception Handling

from ofiq import (
    OFIQ,
    OFIQError,
    ConfigError,
    QualityError,
    FaceDetectionError,
    LandmarkError,
    SegmentationError,
)

try:
    score = ofiq.scalar_quality(image)
except FaceDetectionError:
    print("No face in image")
except LandmarkError:
    print("Could not extract facial landmarks")
except QualityError as e:
    print(f"Quality assessment failed: {e}")
except OFIQError as e:
    print(f"OFIQ error: {e}")

Thread Safety

OFIQ instances are not thread-safe. The underlying C++ library uses shared mutable state without synchronization:

  • Use one OFIQ instance per thread, or
  • Serialize access with a lock, or
  • Use multiprocessing instead of threading

Scalar vs Raw Scores

Scalar scores are normalized to [0, 100] where higher is always better. Raw scores are the native OFIQ values before normalization — their range and direction vary per measure. Use include_raw=True when you need the original measurement values.

Available Quality Measures

All measures return scalar values in [0, 100] where higher is better, or None if the measure failed:

Measure Description
UnifiedQualityScore Overall quality score
Sharpness Image sharpness/focus
BackgroundUniformity Background consistency
IlluminationUniformity Lighting evenness
LuminanceMean Average brightness
LuminanceVariance Brightness variation
DynamicRange Contrast range
CompressionArtifacts JPEG artifact detection
NaturalColour Color naturalness
HeadPoseYaw Face yaw angle
HeadPosePitch Face pitch angle
HeadPoseRoll Face roll angle
EyesOpen Eye openness
MouthClosed Mouth closure
EyesVisible Eye visibility
ExpressionNeutrality Neutral expression
SingleFacePresent Single face detection
InterEyeDistance Distance between eyes
HeadSize Face size in frame
... and more

API Reference

OFIQ(config_dir="", config_file="ofiq_config.jaxn")

Initialize OFIQ. If config_dir is omitted, uses OFIQ_DATA_DIR env var or ~/.ofiq/data.

scalar_quality(image_rgb) -> float

Compute unified quality score. image_rgb: NumPy uint8 array of shape (H, W, 3) in RGB.

vector_quality(image_rgb, include_raw=False) -> dict

Compute all quality measures. Returns None for failed individual measures.

ofiq.setup(force_download=False) -> str

Download models and config. Returns the data directory path.

ofiq.resolve_data_dir() -> str

Return the resolved data directory.

CLI

python-ofiq setup                    # download models and config
python-ofiq setup --force-download   # re-download even if present
OFIQ_DATA_DIR=/custom/path python-ofiq setup

Version Compatibility

python-ofiq OFIQ C++
0.1.x 1.1.2

Development

Build from source

git clone https://github.com/unicef/python-ofiq.git
cd python-ofiq

docker build -f docker/Dockerfile -t python-ofiq-test .
docker run -it python-ofiq-test python -c "from ofiq import OFIQ; print('OK')"

Run tests

pip install pytest numpy

pytest tests/ -v -m "not integration"

# Integration tests require models
pytest tests/ -v

License

MIT — see LICENSE.

This project wraps the OFIQ C++ library which is also MIT licensed.

Acknowledgments

  • BSI for developing OFIQ
  • pybind11 for the bindings framework

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distributions

No source distribution files available for this release.See tutorial on generating distribution archives.

Built Distributions

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

python_ofiq-0.2.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (13.9 MB view details)

Uploaded CPython 3.14manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

python_ofiq-0.2.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (13.9 MB view details)

Uploaded CPython 3.13manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

python_ofiq-0.2.0-cp312-cp312-manylinux_2_28_x86_64.whl (13.8 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.28+ x86-64

python_ofiq-0.2.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (13.9 MB view details)

Uploaded CPython 3.12manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

python_ofiq-0.2.0-cp311-cp311-manylinux_2_28_x86_64.whl (13.8 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.28+ x86-64

python_ofiq-0.2.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl (13.9 MB view details)

Uploaded CPython 3.11manylinux: glibc 2.27+ x86-64manylinux: glibc 2.28+ x86-64

File details

Details for the file python_ofiq-0.2.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for python_ofiq-0.2.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 180d51baa39f76cd77f6fa2fb2ce3988ec202d744d71e7cde87681fe693c5ad2
MD5 11149642afd8261b84b118ab7e4906be
BLAKE2b-256 26709f16692a0ddc05f122894d05cc4308fe79663b581241b917ed94b2975a80

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_ofiq-0.2.0-cp314-cp314-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on unicef/python-ofiq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_ofiq-0.2.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for python_ofiq-0.2.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 4228cb9d837928de061410660dd632c8847d08e3d6129127f295d81a922b48bd
MD5 3c1ef11f878a12b1fbf2640dd3518ea7
BLAKE2b-256 bd377b47f4d21f77ae4d0b2d4f04b61a8f5dfd6d1fc80b365a92ab62d5006518

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_ofiq-0.2.0-cp313-cp313-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on unicef/python-ofiq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_ofiq-0.2.0-cp312-cp312-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for python_ofiq-0.2.0-cp312-cp312-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 0ca87b0a8a25206d7c3a5a6a27d5d29b3ea23e53c0273f162867ac4bdd607bb5
MD5 f80d58d990ff8cf5e975520c7549f484
BLAKE2b-256 7b4a98ada719524b0b28aeb269ddab89cc3e8b23591ff7049eba2bbf93d633f8

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_ofiq-0.2.0-cp312-cp312-manylinux_2_28_x86_64.whl:

Publisher: release.yml on unicef/python-ofiq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_ofiq-0.2.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for python_ofiq-0.2.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 3ffdeeeea8ef9330052f29ae79713b5c302b15145ad4c3ac67980c268e8120d7
MD5 45d27bb164a2e21f10defab578931b04
BLAKE2b-256 ce2bd0ef612a0daf0a7c3f4e7cd46e0bd4b3d2175818dfa5c00364cb06eb5a2c

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_ofiq-0.2.0-cp312-cp312-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on unicef/python-ofiq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_ofiq-0.2.0-cp311-cp311-manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for python_ofiq-0.2.0-cp311-cp311-manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 eeb6cbf40939917b939e012b0645936d944abc81e476b63043ac48555944c9e2
MD5 c7d85be9281e683ebfebcf82c0543055
BLAKE2b-256 6470bdb4dde92e9e2218ca0c53b7a5b6de5092810282b32f2158719c1f0710d9

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_ofiq-0.2.0-cp311-cp311-manylinux_2_28_x86_64.whl:

Publisher: release.yml on unicef/python-ofiq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file python_ofiq-0.2.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl.

File metadata

File hashes

Hashes for python_ofiq-0.2.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl
Algorithm Hash digest
SHA256 631231a84b0f65fd06fe86e3bb541648f155cc4539a20688f0d875201d49b1d1
MD5 a8f8bb6376c463fa0ee6b526dfa21d08
BLAKE2b-256 34da2d14e64daed957e96481b50d99f7beca54735de55c94d8542b061c512be9

See more details on using hashes here.

Provenance

The following attestation bundles were made for python_ofiq-0.2.0-cp311-cp311-manylinux_2_27_x86_64.manylinux_2_28_x86_64.whl:

Publisher: release.yml on unicef/python-ofiq

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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