File size: 1,756 Bytes
e4a27d7
 
 
 
 
 
 
 
 
 
13b7f6a
 
 
 
 
 
 
e4a27d7
 
 
 
13b7f6a
 
 
 
e4a27d7
 
 
13b7f6a
e4a27d7
 
 
 
 
 
 
 
 
 
 
c449993
e4a27d7
13b7f6a
c449993
13b7f6a
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
41
42
43
44
45
46
47
48
49
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()