K-A-Uthman commited on
Commit
13b7f6a
·
verified ·
1 Parent(s): b67776b

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +17 -7
app.py CHANGED
@@ -8,32 +8,42 @@ import gradio as gr
8
  from joblib import load
9
 
10
 
 
 
 
 
 
 
 
11
  # Load pre-trained ResNet50 model without top layers
12
  base_model = ResNet50(weights='imagenet', include_top=False)
13
 
14
  # Function to extract features using ResNet50
15
- def extract_resnet_features(img_path):
16
- img = image.load_img(img_path, target_size=(224, 224))
 
 
17
  x = image.img_to_array(img)
18
  x = np.expand_dims(x, axis=0)
19
  x = preprocess_input(x)
 
 
20
  features = base_model.predict(x)
21
  features_flattened = features.flatten()
22
  return features_flattened
23
 
24
 
25
- # Load the trained Random Forest classifier
26
- rf_classifier = load('random_forest_model2.joblib')
27
-
28
  # Function to make predictions
29
  def predict(image):
30
  # Convert image to feature vector using ResNet50 (you can replace this with your feature extraction method)
31
  features = extract_resnet_features(image)
32
 
33
  # Make prediction using Random Forest classifier
34
- prediction = rf_classifier.predict([features])[0]
35
 
36
- return prediction
 
 
37
 
38
  # Define Gradio interface
39
  iface = gr.Interface(fn=predict, inputs="image", outputs="text", title="Brain Tumor Classification")
 
8
  from joblib import load
9
 
10
 
11
+ # Load the trained Random Forest classifier
12
+ rf_classifier = load('random_forest_model2.joblib')
13
+
14
+ # Get the class labels from the trained Random Forest classifier
15
+ class_labels = rf_classifier.classes_
16
+
17
+
18
  # Load pre-trained ResNet50 model without top layers
19
  base_model = ResNet50(weights='imagenet', include_top=False)
20
 
21
  # Function to extract features using ResNet50
22
+ def extract_resnet_features(image_data):
23
+ # Convert image data to image array
24
+ img = image.array_to_img(image_data, scale=False)
25
+ img = img.resize((224, 224)) # Resize image to match ResNet50 input size
26
  x = image.img_to_array(img)
27
  x = np.expand_dims(x, axis=0)
28
  x = preprocess_input(x)
29
+
30
+ # Extract features using ResNet50
31
  features = base_model.predict(x)
32
  features_flattened = features.flatten()
33
  return features_flattened
34
 
35
 
 
 
 
36
  # Function to make predictions
37
  def predict(image):
38
  # Convert image to feature vector using ResNet50 (you can replace this with your feature extraction method)
39
  features = extract_resnet_features(image)
40
 
41
  # Make prediction using Random Forest classifier
42
+ predicted_index = rf_classifier.predict([features_flattened])[0]
43
 
44
+ # Decode predicted class using the class labels obtained from the Random Forest classifier
45
+ predicted_class = class_labels[predicted_index]
46
+ return predicted_class
47
 
48
  # Define Gradio interface
49
  iface = gr.Interface(fn=predict, inputs="image", outputs="text", title="Brain Tumor Classification")