kyc-backend / app /api /dependencies.py
supraptin's picture
Initial deployment to Hugging Face Spaces
bd2c5ca
"""
Shared dependencies for API routes.
"""
from fastapi import UploadFile, HTTPException
from typing import Tuple
import numpy as np
from ..config import settings
from ..utils.image_utils import (
read_image_from_upload,
validate_content_type,
decode_base64_image
)
from ..services.face_recognition import face_recognition_service
from ..services.liveness_detection import liveness_detection_service
from ..services.face_quality import face_quality_service
from ..services.ktp_ocr import ktp_ocr_service
async def get_validated_image(file: UploadFile) -> np.ndarray:
"""
Validate and read an uploaded image file.
Args:
file: Uploaded file
Returns:
Image as numpy array (BGR format)
"""
# Validate content type
validate_content_type(file.content_type, settings.ALLOWED_IMAGE_TYPES)
# Read and decode image
image = await read_image_from_upload(file)
return image
async def get_validated_images(
file1: UploadFile,
file2: UploadFile
) -> Tuple[np.ndarray, np.ndarray]:
"""
Validate and read two uploaded image files.
Args:
file1: First uploaded file
file2: Second uploaded file
Returns:
Tuple of images as numpy arrays (BGR format)
"""
image1 = await get_validated_image(file1)
image2 = await get_validated_image(file2)
return image1, image2
def get_face_recognition_service():
"""Get the face recognition service instance."""
if not face_recognition_service.initialized:
raise HTTPException(
status_code=503,
detail={
"error_code": "MODEL_NOT_LOADED",
"message": "Face recognition model not loaded. Please wait for initialization."
}
)
return face_recognition_service
def get_liveness_service():
"""Get the liveness detection service instance."""
if not liveness_detection_service.initialized:
raise HTTPException(
status_code=503,
detail={
"error_code": "MODEL_NOT_LOADED",
"message": "Liveness detection model not loaded. Please wait for initialization."
}
)
return liveness_detection_service
def get_quality_service():
"""Get the face quality service instance."""
return face_quality_service
def get_ocr_service():
"""Get the KTP OCR service instance."""
if not ktp_ocr_service.initialized:
raise HTTPException(
status_code=503,
detail={
"error_code": "MODEL_NOT_LOADED",
"message": "OCR model not loaded. Please wait for initialization."
}
)
return ktp_ocr_service