File size: 2,716 Bytes
bd2c5ca
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
"""
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