CoolFace
Apppublic

DerradjAdel/Lifeline_labs_AI

sourceHugging Faceotherupdated 1y agoView on Hugging Face
0likes
inference.py40 linesDownload Raw Back to root
1import tensorflow as tf2import numpy as np3from image_preprocessor import preprocess_for_model4 5# Path to your 3-class Keras model file6MODEL_PATH = "lifeline_labs_3classes_model.keras"7 8# Load the model once at import9model = tf.keras.models.load_model(MODEL_PATH)10 11# Define the class labels in the same order your model was trained on12labels = ['Normal','Pneumonia','Lung_Opacity']13 14def predict_image(image_bytes, augment=False):15    """16    Given raw image bytes, preprocess and predict.17 18    Returns a dict with:19      - predicted_class (str)20      - confidence (float)21      - probabilities (list of floats)22 23    Args:24      image_bytes (bytes): Raw image data.25      augment (bool): Whether to apply random augmentation before inference.26    """27    # Preprocess (resize, normalize, optional augment)28    inp = preprocess_for_model(image_bytes, augment=augment)29 30    # Run inference (model expects shape (1, H, W, C))31    probs = model.predict(inp)[0]  # shape (3,)32 33    # Determine top class34    idx = int(np.argmax(probs))35    return {36        "predicted_class": labels[idx],37        "confidence": float(probs[idx]),38        "probabilities": probs.tolist()39    }40