methestrikerx100 commited on
Commit
2e8164f
·
verified ·
1 Parent(s): 9c61dde

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +24 -14
app.py CHANGED
@@ -12,7 +12,7 @@ loaded_feature_extractor = load_model("feature_extractor_model")
12
 
13
  # Load the SVM model
14
  import pickle
15
- with open("svm_model.pkl", 'rb') as file:
16
  loaded_svm_model = pickle.load(file)
17
 
18
  # Load the mineral detection model
@@ -71,28 +71,31 @@ def classify_image(image):
71
  is_mineral = detect_mineral(image)
72
  if not is_mineral:
73
  return "Input is not a Microscopic mineral thin section, Please Insert a thin section.", "", ""
74
-
75
  # Preprocess the image for classification
76
  image = preprocess_image_classification(np.array(image))
77
  if image is None:
78
  return "Error preprocessing image.", "", ""
79
-
80
  # Extract features using the loaded CNN feature extractor
81
  image_features = loaded_feature_extractor.predict(image)
82
-
83
  # Make prediction using the loaded SVM model
84
  predicted_label = loaded_svm_model.predict(image_features)
85
  class_idx = predicted_label[0]
86
  predicted_class_name = class_labels[class_idx]
87
-
88
- # Get prediction scores for all classes
89
- decision_values = loaded_svm_model.decision_function(image_features)
90
- prediction_scores = [f"{label}: {score:.4f}" for label, score in zip(class_labels, decision_values[0])]
 
 
 
91
  predicted_scores = "\n".join(prediction_scores)
92
-
93
- # Get the mineral key facts
94
  mineral_key_facts = mineral_facts.get(predicted_class_name, "No key facts available for this mineral.")
95
-
96
  return predicted_class_name, predicted_scores, mineral_key_facts
97
 
98
 
@@ -176,6 +179,7 @@ custom_css = """
176
  """
177
 
178
 
 
179
  with gr.Blocks(
180
  title=app_title,
181
  css=custom_css,
@@ -183,7 +187,7 @@ with gr.Blocks(
183
  js=js,
184
  ) as demo:
185
  gr.Markdown(DESCRIPTION)
186
-
187
  # Create interface components
188
  with gr.Row():
189
  image_input = gr.Image(elem_id="image_input", type="pil")
@@ -193,10 +197,16 @@ with gr.Blocks(
193
  gr.Textbox(label="Key Facts About Mineral", elem_id="mineral_key_facts", lines=8),
194
  ]
195
  image_button = gr.Button("Classify Mineral")
196
- image_button.click(classify_image, inputs=image_input, outputs=output_components)
 
 
 
 
197
 
198
  gr.Examples(
199
- examples=["Gradio examples/Biotite1.jpg", "Gradio examples/Biotite2.jpg", "Gradio examples/Olivine1.jpg", "Gradio examples/Plagioclase1.jpg"],
 
 
200
  inputs=image_input,
201
  )
202
 
 
12
 
13
  # Load the SVM model
14
  import pickle
15
+ with open("svm_model_probablity.pkl", 'rb') as file:
16
  loaded_svm_model = pickle.load(file)
17
 
18
  # Load the mineral detection model
 
71
  is_mineral = detect_mineral(image)
72
  if not is_mineral:
73
  return "Input is not a Microscopic mineral thin section, Please Insert a thin section.", "", ""
74
+
75
  # Preprocess the image for classification
76
  image = preprocess_image_classification(np.array(image))
77
  if image is None:
78
  return "Error preprocessing image.", "", ""
79
+
80
  # Extract features using the loaded CNN feature extractor
81
  image_features = loaded_feature_extractor.predict(image)
82
+
83
  # Make prediction using the loaded SVM model
84
  predicted_label = loaded_svm_model.predict(image_features)
85
  class_idx = predicted_label[0]
86
  predicted_class_name = class_labels[class_idx]
87
+
88
+ # Get probabilities for all classes
89
+ probabilities = loaded_svm_model.predict_proba(image_features)[0]
90
+ prediction_scores = [
91
+ f"{label}: {prob:.4f}"
92
+ for label, prob in zip(class_labels, probabilities)
93
+ ]
94
  predicted_scores = "\n".join(prediction_scores)
95
+
96
+ # Get key facts about the predicted mineral
97
  mineral_key_facts = mineral_facts.get(predicted_class_name, "No key facts available for this mineral.")
98
+
99
  return predicted_class_name, predicted_scores, mineral_key_facts
100
 
101
 
 
179
  """
180
 
181
 
182
+ # Gradio Blocks interface
183
  with gr.Blocks(
184
  title=app_title,
185
  css=custom_css,
 
187
  js=js,
188
  ) as demo:
189
  gr.Markdown(DESCRIPTION)
190
+
191
  # Create interface components
192
  with gr.Row():
193
  image_input = gr.Image(elem_id="image_input", type="pil")
 
197
  gr.Textbox(label="Key Facts About Mineral", elem_id="mineral_key_facts", lines=8),
198
  ]
199
  image_button = gr.Button("Classify Mineral")
200
+ image_button.click(
201
+ classify_image,
202
+ inputs=image_input,
203
+ outputs=output_components
204
+ )
205
 
206
  gr.Examples(
207
+ examples=[
208
+ r"C:\Users\nanom\OneDrive\Desktop\Gradio examples\Olivine2.jpg",
209
+ ],
210
  inputs=image_input,
211
  )
212