face-intel / tests /providers /test_haar.py
Marwan
Restructure + add reverse face search (PimEyes-style)
f5eeb1c
Raw
History Blame Contribute Delete
2.75 kB
"""Provider tests for HaarDetector."""
from __future__ import annotations
import cv2
import numpy as np
import pytest
from config.settings import Settings
from pipeline.feature_extraction import PipelineOutput
from providers.detection.haar import HaarDetector
@pytest.fixture
def haar_detector():
return HaarDetector(settings=Settings(environment="test", db_path=":memory:"))
@pytest.fixture
def pipeline_output(sample_image_bytes):
"""A PipelineOutput wrapping a synthetic image."""
img = cv2.imdecode(np.frombuffer(sample_image_bytes, np.uint8), cv2.IMREAD_COLOR)
return PipelineOutput(
image=img,
image_hash="test-hash",
width=img.shape[1],
height=img.shape[0],
source="bytes",
)
class TestHaarDetector:
def test_provider_name(self, haar_detector):
assert haar_detector.name == "haar"
def test_capability(self, haar_detector):
from models.providers import ProviderCapability
assert haar_detector.capability == ProviderCapability.DETECTION
def test_is_available(self, haar_detector):
assert haar_detector.is_available() is True
def test_execute_returns_provider_result(self, haar_detector, pipeline_output):
from providers.base import ProviderResult
result = haar_detector.execute(pipeline_output)
assert isinstance(result, ProviderResult)
assert result.provider == "haar"
assert result.success is True
assert result.elapsed_ms > 0
def test_execute_normalized_has_expected_keys(self, haar_detector, pipeline_output):
result = haar_detector.execute(pipeline_output)
assert "boxes" in result.normalized
assert "num_faces" in result.normalized
assert "confidences" in result.normalized
assert "landmarks" in result.normalized
def test_execute_raw_has_expected_keys(self, haar_detector, pipeline_output):
result = haar_detector.execute(pipeline_output)
assert "rectangles" in result.raw
assert "num_faces" in result.raw
def test_execute_on_black_image_finds_no_faces(self, haar_detector):
img = np.zeros((200, 200, 3), dtype=np.uint8)
po = PipelineOutput(image=img, image_hash="x", width=200, height=200, source="bytes")
result = haar_detector.execute(po)
assert result.success is True
assert result.normalized["num_faces"] == 0
def test_execute_handles_empty_image_gracefully(self, haar_detector):
# 1x1 image should not crash
img = np.zeros((1, 1, 3), dtype=np.uint8)
po = PipelineOutput(image=img, image_hash="x", width=1, height=1, source="bytes")
result = haar_detector.execute(po)
assert result.success is True