| | import numpy as np |
| | from typing import Dict, Any, Union |
| | import tensorflow as tf |
| | import keras |
| | from config import CLASS_NAMES |
| |
|
| | def predict_skin_condition(img_array: np.ndarray, model: tf.keras.Model) -> Dict[str, Union[str, float]]: |
| | """ |
| | Predict skin condition from an input image using the provided model. |
| | |
| | Args: |
| | img_array: Input image as numpy array (H, W, C) with RGB channels |
| | model: Loaded Keras/TensorFlow model for prediction |
| | |
| | Returns: |
| | Dictionary containing predicted condition name and confidence score |
| | """ |
| | try: |
| | |
| | if len(img_array.shape) == 3: |
| | img_array = np.expand_dims(img_array, axis=0) |
| | |
| | |
| | preprocessed_img = keras.applications.efficientnet.preprocess_input(img_array) |
| | |
| | |
| | with tf.device('/CPU:0'): |
| | pred_probs = model.predict(preprocessed_img, verbose=0)[0] |
| | |
| | |
| | top_index = np.argmax(pred_probs) |
| | |
| | |
| | if top_index >= len(CLASS_NAMES): |
| | raise ValueError(f"Predicted index {top_index} exceeds available class names") |
| | |
| | return { |
| | "condition": CLASS_NAMES[top_index], |
| | "confidence": float(pred_probs[top_index]) |
| | } |
| | |
| | except Exception as e: |
| | |
| | |
| | |
| | |
| | return { |
| | "condition": "error", |
| | "confidence": 0.0 |
| | } |