Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -6,26 +6,25 @@ import gradio as gr
|
|
6 |
from tensorflow import keras
|
7 |
from keras.models import load_model
|
8 |
import folium
|
9 |
-
import re
|
10 |
|
|
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
-
|
16 |
-
|
17 |
-
|
18 |
-
# Load the CNN feature extractor model
|
19 |
from tensorflow.keras.models import load_model
|
|
|
20 |
loaded_feature_extractor = load_model("feature_extractor_model")
|
21 |
|
22 |
# Load the SVM model
|
23 |
import pickle
|
|
|
24 |
with open("svm_model_probablity.pkl", 'rb') as file:
|
25 |
loaded_svm_model = pickle.load(file)
|
26 |
|
27 |
# Load the mineral detection model
|
28 |
-
mineral_detection_model = tf.keras.models.load_model(
|
|
|
29 |
|
30 |
# Define the class labels
|
31 |
class_labels = ['biotite', 'granite', 'olivine', 'plagioclase', 'staurolite']
|
@@ -36,6 +35,11 @@ mineral_facts = {
|
|
36 |
'plagioclase': "Hardness: 6-6.5\nMagnetism: None\nDensity: 2.6-2.8 g/cm³\nColors: White, gray, green\nDescription: A series of feldspar minerals ranging from sodium-rich albite to calcium-rich anorthite.",
|
37 |
'staurolite': "Hardness: 7-7.5\nMagnetism: None\nDensity: 3.6-3.8 g/cm³\nColors: Brown, reddish-brown, black\nDescription: A nesosilicate mineral with a distinctive cruciform twinning habit, commonly found in metamorphic rocks."
|
38 |
}
|
|
|
|
|
|
|
|
|
|
|
39 |
# Function to preprocess the image for mineral detection
|
40 |
def preprocess_image_detection(img_array):
|
41 |
if img_array is None:
|
@@ -46,6 +50,7 @@ def preprocess_image_detection(img_array):
|
|
46 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
47 |
return img_array
|
48 |
|
|
|
49 |
# Function to preprocess the image for classification
|
50 |
def preprocess_image_classification(img_array):
|
51 |
if img_array is None:
|
@@ -56,6 +61,7 @@ def preprocess_image_classification(img_array):
|
|
56 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
57 |
return img_array
|
58 |
|
|
|
59 |
# Define the function to detect if the input is a mineral
|
60 |
def detect_mineral(image):
|
61 |
if image is not None:
|
@@ -74,17 +80,23 @@ def detect_mineral(image):
|
|
74 |
# Handle the case where no image is provided
|
75 |
return "No image provided."
|
76 |
|
|
|
77 |
# Define the function to make predictions
|
78 |
def classify_image(image):
|
|
|
|
|
|
|
|
|
79 |
# Check if the input is a mineral
|
80 |
is_mineral = detect_mineral(image)
|
81 |
if not is_mineral:
|
82 |
-
|
|
|
83 |
|
84 |
# Preprocess the image for classification
|
85 |
image = preprocess_image_classification(np.array(image))
|
86 |
if image is None:
|
87 |
-
return "Error preprocessing image.", "", ""
|
88 |
|
89 |
# Extract features using the loaded CNN feature extractor
|
90 |
image_features = loaded_feature_extractor.predict(image)
|
@@ -100,18 +112,20 @@ def classify_image(image):
|
|
100 |
# Convert prediction scores to percentages
|
101 |
prediction_scores_percentages = [f"{score * 100:.2f}%" for score in probabilities]
|
102 |
|
103 |
-
predicted_scores = "\n".join(
|
|
|
104 |
|
105 |
# Get key facts about the predicted mineral
|
106 |
mineral_key_facts = mineral_facts.get(predicted_class_name, "No key facts available for this mineral.")
|
107 |
|
108 |
-
|
109 |
-
|
110 |
-
|
111 |
-
|
112 |
-
|
113 |
-
|
114 |
|
|
|
115 |
DESCRIPTION = '''
|
116 |
<div>
|
117 |
<h1 style="text-align: center;">Microscopic Mineral Identification App</h1>
|
@@ -127,40 +141,29 @@ def welcome(name):
|
|
127 |
return f"Welcome to Gradio, {name}!"
|
128 |
|
129 |
|
130 |
-
|
131 |
-
|
132 |
app_title = "Mineral Identification using AI"
|
133 |
app_description = "This application uses advanced machine learning models to accurately identify and classify different types of minerals from images. Simply upload an image, and the system will provide the predicted mineral class along with its key characteristics and properties."
|
134 |
|
135 |
custom_css = """
|
136 |
.gradio-container {display: flex; justify-content: center; align-items: center; height: 100vh;background-color: #f0f0f0;}
|
137 |
-
|
138 |
-
|
139 |
#title-container {
|
140 |
display: flex;
|
141 |
align-items: center;
|
142 |
justify-content: center;
|
143 |
margin-bottom: 20px;
|
144 |
}
|
145 |
-
|
146 |
#app-title {
|
147 |
margin-right: 20px; /* Adjust the spacing between the title and logo */
|
148 |
}
|
149 |
-
|
150 |
#logo-img {
|
151 |
width: 50px; /* Adjust the logo size as needed */
|
152 |
height: 50px;
|
153 |
}
|
154 |
-
|
155 |
"""
|
156 |
-
|
157 |
-
|
158 |
-
# Gradio Blocks interface
|
159 |
with gr.Blocks(
|
160 |
-
|
161 |
-
|
162 |
-
|
163 |
-
|
164 |
) as demo:
|
165 |
gr.Markdown(DESCRIPTION)
|
166 |
|
@@ -179,9 +182,14 @@ with gr.Blocks(
|
|
179 |
outputs=output_components
|
180 |
)
|
181 |
|
182 |
-
|
183 |
-
|
|
|
|
|
|
|
|
|
184 |
inputs=image_input,
|
|
|
185 |
)
|
186 |
|
187 |
demo.launch(auth_message="Welcome to the Mineral Identification App.")
|
|
|
6 |
from tensorflow import keras
|
7 |
from keras.models import load_model
|
8 |
import folium
|
|
|
9 |
|
10 |
+
from Map import *
|
11 |
|
12 |
|
13 |
|
14 |
|
|
|
|
|
|
|
|
|
15 |
from tensorflow.keras.models import load_model
|
16 |
+
|
17 |
loaded_feature_extractor = load_model("feature_extractor_model")
|
18 |
|
19 |
# Load the SVM model
|
20 |
import pickle
|
21 |
+
|
22 |
with open("svm_model_probablity.pkl", 'rb') as file:
|
23 |
loaded_svm_model = pickle.load(file)
|
24 |
|
25 |
# Load the mineral detection model
|
26 |
+
mineral_detection_model = tf.keras.models.load_model(
|
27 |
+
"mineral_detection_model_Final_4_18_2024.h5")
|
28 |
|
29 |
# Define the class labels
|
30 |
class_labels = ['biotite', 'granite', 'olivine', 'plagioclase', 'staurolite']
|
|
|
35 |
'plagioclase': "Hardness: 6-6.5\nMagnetism: None\nDensity: 2.6-2.8 g/cm³\nColors: White, gray, green\nDescription: A series of feldspar minerals ranging from sodium-rich albite to calcium-rich anorthite.",
|
36 |
'staurolite': "Hardness: 7-7.5\nMagnetism: None\nDensity: 3.6-3.8 g/cm³\nColors: Brown, reddish-brown, black\nDescription: A nesosilicate mineral with a distinctive cruciform twinning habit, commonly found in metamorphic rocks."
|
37 |
}
|
38 |
+
|
39 |
+
|
40 |
+
# Function to preprocess the image for mineral detection
|
41 |
+
|
42 |
+
|
43 |
# Function to preprocess the image for mineral detection
|
44 |
def preprocess_image_detection(img_array):
|
45 |
if img_array is None:
|
|
|
50 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
51 |
return img_array
|
52 |
|
53 |
+
|
54 |
# Function to preprocess the image for classification
|
55 |
def preprocess_image_classification(img_array):
|
56 |
if img_array is None:
|
|
|
61 |
img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
|
62 |
return img_array
|
63 |
|
64 |
+
|
65 |
# Define the function to detect if the input is a mineral
|
66 |
def detect_mineral(image):
|
67 |
if image is not None:
|
|
|
80 |
# Handle the case where no image is provided
|
81 |
return "No image provided."
|
82 |
|
83 |
+
|
84 |
# Define the function to make predictions
|
85 |
def classify_image(image):
|
86 |
+
if image is None:
|
87 |
+
# Handle the case where no image is provided
|
88 |
+
return "No image provided.", "", "", ""
|
89 |
+
|
90 |
# Check if the input is a mineral
|
91 |
is_mineral = detect_mineral(image)
|
92 |
if not is_mineral:
|
93 |
+
# Return placeholders for non-mineral input
|
94 |
+
return "Input is not a Microscopic mineral thin section, Please Insert a thin section.", "", "", ""
|
95 |
|
96 |
# Preprocess the image for classification
|
97 |
image = preprocess_image_classification(np.array(image))
|
98 |
if image is None:
|
99 |
+
return "Error preprocessing image.", "", "", ""
|
100 |
|
101 |
# Extract features using the loaded CNN feature extractor
|
102 |
image_features = loaded_feature_extractor.predict(image)
|
|
|
112 |
# Convert prediction scores to percentages
|
113 |
prediction_scores_percentages = [f"{score * 100:.2f}%" for score in probabilities]
|
114 |
|
115 |
+
predicted_scores = "\n".join(
|
116 |
+
[f"{label}: {score}" for label, score in zip(class_labels, prediction_scores_percentages)])
|
117 |
|
118 |
# Get key facts about the predicted mineral
|
119 |
mineral_key_facts = mineral_facts.get(predicted_class_name, "No key facts available for this mineral.")
|
120 |
|
121 |
+
if predicted_class_name:
|
122 |
+
# Generate the mineral map
|
123 |
+
mineral_map = generate_mineral_map(predicted_class_name)
|
124 |
+
mineral_map_html = mineral_map._repr_html_()
|
125 |
+
else:
|
126 |
+
mineral_map_html = ""
|
127 |
|
128 |
+
return predicted_class_name, predicted_scores, mineral_key_facts, mineral_map_html
|
129 |
DESCRIPTION = '''
|
130 |
<div>
|
131 |
<h1 style="text-align: center;">Microscopic Mineral Identification App</h1>
|
|
|
141 |
return f"Welcome to Gradio, {name}!"
|
142 |
|
143 |
|
|
|
|
|
144 |
app_title = "Mineral Identification using AI"
|
145 |
app_description = "This application uses advanced machine learning models to accurately identify and classify different types of minerals from images. Simply upload an image, and the system will provide the predicted mineral class along with its key characteristics and properties."
|
146 |
|
147 |
custom_css = """
|
148 |
.gradio-container {display: flex; justify-content: center; align-items: center; height: 100vh;background-color: #f0f0f0;}
|
|
|
|
|
149 |
#title-container {
|
150 |
display: flex;
|
151 |
align-items: center;
|
152 |
justify-content: center;
|
153 |
margin-bottom: 20px;
|
154 |
}
|
|
|
155 |
#app-title {
|
156 |
margin-right: 20px; /* Adjust the spacing between the title and logo */
|
157 |
}
|
|
|
158 |
#logo-img {
|
159 |
width: 50px; /* Adjust the logo size as needed */
|
160 |
height: 50px;
|
161 |
}
|
|
|
162 |
"""
|
|
|
|
|
|
|
163 |
with gr.Blocks(
|
164 |
+
title=app_title,
|
165 |
+
css=custom_css,
|
166 |
+
theme=gr.themes.Monochrome(),
|
|
|
167 |
) as demo:
|
168 |
gr.Markdown(DESCRIPTION)
|
169 |
|
|
|
182 |
outputs=output_components
|
183 |
)
|
184 |
|
185 |
+
# Add a new row for the map
|
186 |
+
with gr.Row():
|
187 |
+
mineral_map = gr.HTML(label="Mineral Locations Map", elem_id="mineral_map")
|
188 |
+
|
189 |
+
image_button.click(
|
190 |
+
classify_image,
|
191 |
inputs=image_input,
|
192 |
+
outputs=output_components + [mineral_map]
|
193 |
)
|
194 |
|
195 |
demo.launch(auth_message="Welcome to the Mineral Identification App.")
|