K-A-Uthman's picture
Create app.py
e4a27d7 verified
raw
history blame
1.34 kB
from tensorflow.keras.applications import ResNet50
from tensorflow.keras.preprocessing import image
from tensorflow.keras.applications.resnet50 import preprocess_input
import numpy as np
from sklearn.ensemble import RandomForestClassifier
import gradio as gr
from joblib import load
# Load pre-trained ResNet50 model without top layers
base_model = ResNet50(weights='imagenet', include_top=False)
# Function to extract features using ResNet50
def extract_resnet_features(img_path):
img = image.load_img(img_path, target_size=(224, 224))
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
features = base_model.predict(x)
features_flattened = features.flatten()
return features_flattened
# Load the trained Random Forest classifier
rf_classifier = load('random_forest_model2.joblib')
# Function to make predictions
def predict(image):
# Convert image to feature vector using ResNet50 (you can replace this with your feature extraction method)
features = extract_resnet_features(image)
# Make prediction using Random Forest classifier
prediction = rf_classifier.predict([features])[0]
return prediction
# Define Gradio interface
iface = gr.Interface(fn=predict, inputs="image", outputs="text", title="Brain Tumor Classification")
iface.launch()