File size: 1,428 Bytes
e58e243 |
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 |
import tensorflow as tf
import numpy as np
from PIL import Image
from huggingface_hub import hf_hub_download
import json
# Download model and class names from Hugging Face
model_path = hf_hub_download(repo_id="abdo1176/brain-model-test", filename="model.keras")
class_names_path = hf_hub_download(repo_id="abdo1176/brain-model-test", filename="class_names.json")
# Load model and class names
model = tf.keras.models.load_model(model_path)
with open(class_names_path, 'r') as f:
class_names = json.load(f)
def preprocess_image(image_path):
"""Preprocess image for model prediction"""
image = Image.open(image_path).convert('RGB')
image = image.resize((224, 224))
image = np.array(image) / 255.0
image = np.expand_dims(image, axis=0)
return image
def predict_brain_tumor(image_path):
"""Predict brain tumor from MRI image"""
image = preprocess_image(image_path)
predictions = model.predict(image)
predicted_idx = np.argmax(predictions[0])
confidence = float(predictions[0][predicted_idx])
return {
"predicted_class": class_names[predicted_idx],
"confidence": confidence,
"all_predictions": {class_names[i]: float(predictions[0][i]) for i in range(len(class_names))}
}
# Example usage:
# result = predict_brain_tumor("path/to/your/mri_image.jpg")
# print(f"Prediction: {result['predicted_class']} (Confidence: {result['confidence']:.2%})")
|