import tensorflow as tf import numpy as np from tensorflow.keras.preprocessing.image import img_to_array from tensorflow.keras.applications.efficientnet import preprocess_input from PIL import Image # Hàm load model từ đường dẫn def load_keras_model(model_path): try: model = tf.keras.models.load_model(model_path) print(f"✅ Đã load model thành công từ: {model_path}") return model except Exception as e: print(f"❌ Lỗi khi load model: {e}") return None # Hàm xử lý ảnh và dự đoán def predict_image_class(model, image, class_names): if model is None: return {"Lỗi": "Model chưa được load. Vui lòng kiểm tra đường dẫn!"} # 1. Tiền xử lý ảnh chuẩn EfficientNet (224x224) image = Image.fromarray(image.astype('uint8'), 'RGB').resize((224, 224)) img_array = np.expand_dims(preprocess_input(img_to_array(image)), axis=0) # 2. Dự đoán predictions = model.predict(img_array) # 3. Trả về kết quả dạng Dictionary {Tên_Bệnh: Tỉ_lệ} return {class_names[i]: float(predictions[0][i]) for i in range(len(class_names))}