File size: 1,323 Bytes
8b1f705
7896889
dd8bfff
581db9a
8b1f705
 
 
 
 
7896889
 
 
8b1f705
581db9a
8b1f705
 
dd8bfff
8b1f705
 
 
 
581db9a
8b1f705
 
 
 
7896889
dd8bfff
7896889
 
 
 
dd8bfff
8b1f705
7896889
8b1f705
dd8bfff
 
 
 
 
 
7896889
8b1f705
 
 
7896889
8b1f705
7896889
581db9a
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
# app/services/predictor.py
import numpy as np
import tensorflow as tf
from typing import Tuple

from app.configs import (
    get_classification_model,
    sigmoid_to_class,
)
from app.core.preprocessing import preprocess_image
from app.core.validation import is_valid_image


def classify_image(image_bytes: bytes) -> Tuple[str, float]:
    """
    Classify image using the classification model.
    
    Args:
        image_bytes: Raw image bytes
    
    Returns:
        Tuple of (prediction, confidence)
    """
    # Validate image
    if not is_valid_image(image_bytes):
        raise ValueError("Invalid image format")

    # Get model
    model = get_classification_model()
    if model is None:
        raise ValueError("Classification model not loaded")

    # Preprocess
    img_array = preprocess_image(image_bytes, target_size=(224, 224))

    # 🔥 Call SavedModel signature
    result = model(tf.constant(img_array))
    
    if isinstance(result, dict):
        predictions = list(result.values())[0].numpy()
    else:
        predictions = result.numpy()

    # Sigmoid output -> binary classification
    confidence = float(predictions[0][0])
    prediction = sigmoid_to_class(confidence)

    print(f"🔍 CLASSIFICATION: {prediction} (confidence={confidence:.3f})")

    return prediction, confidence