File size: 1,339 Bytes
e4a27d7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
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()