K-A-Uthman's picture
Update app.py
c449993 verified
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 the trained Random Forest classifier
rf_classifier = load('random_forest_model2.joblib')
# Get the class labels from the trained Random Forest classifier
class_labels = rf_classifier.classes_
# 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(image_data):
# Convert image data to image array
img = image.array_to_img(image_data, scale=False)
img = img.resize((224, 224)) # Resize image to match ResNet50 input size
x = image.img_to_array(img)
x = np.expand_dims(x, axis=0)
x = preprocess_input(x)
# Extract features using ResNet50
features = base_model.predict(x)
features_flattened = features.flatten()
return features_flattened
# 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
predicted_class = rf_classifier.predict([features])[0]
# Decode predicted class using the class labels obtained from the Random Forest classifier
# predicted_class = class_labels[predicted_index]
return predicted_class
# Define Gradio interface
iface = gr.Interface(fn=predict, inputs="image", outputs="text", title="Brain Tumor Classification")
iface.launch()