Spaces:
Runtime error
Runtime error
Update app.py
Browse files
app.py
CHANGED
@@ -7,15 +7,21 @@ from tensorflow import keras
|
|
7 |
from keras.models import load_model
|
8 |
|
9 |
# Load the classification model
|
10 |
-
model = "Hugging_face_model_final.h5"
|
11 |
model = tf.keras.models.load_model(model)
|
12 |
|
13 |
# Load the mineral detection model
|
14 |
-
mineral_detection_model = tf.keras.models.load_model("mineral_detection_model_Final_4_18_2024.h5")
|
15 |
|
16 |
# Define the class labels
|
17 |
class_labels = ['biotite', 'granite', 'olivine', 'plagioclase', 'staurolite']
|
18 |
-
|
|
|
|
|
|
|
|
|
|
|
|
|
19 |
# Function to preprocess the image for mineral detection
|
20 |
def preprocess_image_detection(img_array):
|
21 |
if img_array is None:
|
@@ -59,38 +65,40 @@ def classify_image(image):
|
|
59 |
# Check if the input is a mineral
|
60 |
is_mineral = detect_mineral(image)
|
61 |
if not is_mineral:
|
62 |
-
return "Input is not a mineral.", ""
|
63 |
-
|
64 |
# Preprocess the image for classification
|
65 |
image = preprocess_image_classification(np.array(image))
|
66 |
if image is None:
|
67 |
-
return "Error preprocessing image.", ""
|
68 |
-
|
69 |
# Make prediction
|
70 |
prediction = model.predict(image)
|
71 |
class_idx = np.argmax(prediction)
|
72 |
prediction_scores = prediction[0]
|
73 |
-
|
74 |
# Convert prediction scores to percentages
|
75 |
prediction_scores_percentages = [f"{score * 100:.2f}%" for score in prediction_scores]
|
76 |
-
|
77 |
-
#
|
78 |
predicted_class_name = class_labels[class_idx]
|
79 |
predicted_scores = "\n".join([f"{label}: {score}" for label, score in zip(class_labels, prediction_scores_percentages)])
|
|
|
|
|
|
|
80 |
|
81 |
-
|
|
|
82 |
|
83 |
-
|
84 |
-
#
|
85 |
-
with gr.Blocks(theme=gr.themes.Monochrome()) as demo:
|
86 |
with gr.Row():
|
87 |
image_input = gr.Image(elem_id="image_input", type="pil")
|
88 |
output_components = [
|
89 |
-
gr.Textbox(elem_id="predicted_class_name"),
|
90 |
-
gr.Textbox(elem_id="predicted_scores", lines=5)
|
|
|
91 |
]
|
92 |
image_button = gr.Button("Classify Mineral")
|
93 |
image_button.click(classify_image, inputs=image_input, outputs=output_components)
|
94 |
-
|
95 |
-
|
96 |
-
demo.launch(share=True)
|
|
|
7 |
from keras.models import load_model
|
8 |
|
9 |
# Load the classification model
|
10 |
+
model = r"C:\\Users\\nanom\\OneDrive\\Desktop\\midterm implementation\\Hugging_face_model_final.h5"
|
11 |
model = tf.keras.models.load_model(model)
|
12 |
|
13 |
# Load the mineral detection model
|
14 |
+
mineral_detection_model = tf.keras.models.load_model(r"C:\\Users\\nanom\\OneDrive\\Desktop\\mineral detection model\\mineral_detection_model_Final_4_18_2024.h5")
|
15 |
|
16 |
# Define the class labels
|
17 |
class_labels = ['biotite', 'granite', 'olivine', 'plagioclase', 'staurolite']
|
18 |
+
mineral_facts = {
|
19 |
+
'biotite': "Hardness: 2.5-3\nMagnetism: None\nDensity: 2.7-3.3 g/cm³\nColors: Black, brown, green\nDescription: A phyllosilicate mineral of the mica group with a distinctive platy structure.",
|
20 |
+
'granite': "Hardness: 6-7\nMagnetism: None\nDensity: 2.6-2.7 g/cm³\nColors: Gray, pink, white\nDescription: An intrusive igneous rock composed mainly of quartz, feldspar, and mica.",
|
21 |
+
'olivine': "Hardness: 6.5-7\nMagnetism: None\nDensity: 3.2-3.4 g/cm³\nColors: Green, yellow-green, brown\nDescription: A nesosilicate mineral with a green, glassy appearance, commonly found in mafic and ultramafic rocks.",
|
22 |
+
'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.",
|
23 |
+
'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."
|
24 |
+
}
|
25 |
# Function to preprocess the image for mineral detection
|
26 |
def preprocess_image_detection(img_array):
|
27 |
if img_array is None:
|
|
|
65 |
# Check if the input is a mineral
|
66 |
is_mineral = detect_mineral(image)
|
67 |
if not is_mineral:
|
68 |
+
return "Input is not a Microscopic mineral thin section, Please Insert a thin section.", "", ""
|
69 |
+
|
70 |
# Preprocess the image for classification
|
71 |
image = preprocess_image_classification(np.array(image))
|
72 |
if image is None:
|
73 |
+
return "Error preprocessing image.", "", ""
|
74 |
+
|
75 |
# Make prediction
|
76 |
prediction = model.predict(image)
|
77 |
class_idx = np.argmax(prediction)
|
78 |
prediction_scores = prediction[0]
|
79 |
+
|
80 |
# Convert prediction scores to percentages
|
81 |
prediction_scores_percentages = [f"{score * 100:.2f}%" for score in prediction_scores]
|
82 |
+
|
83 |
+
# Get the predicted class name and key facts
|
84 |
predicted_class_name = class_labels[class_idx]
|
85 |
predicted_scores = "\n".join([f"{label}: {score}" for label, score in zip(class_labels, prediction_scores_percentages)])
|
86 |
+
mineral_key_facts = mineral_facts.get(predicted_class_name, "No key facts available for this mineral.")
|
87 |
+
|
88 |
+
return predicted_class_name, predicted_scores, mineral_key_facts
|
89 |
|
90 |
+
app_title = "Mineral Identification using AI"
|
91 |
+
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."
|
92 |
|
93 |
+
with gr.Blocks(title=app_title, css=".gradio-container {display: flex; justify-content: center; align-items: center; height: 100vh;}",theme=gr.themes.Monochrome()) as demo:
|
94 |
+
# Your existing code for creating the interface components
|
|
|
95 |
with gr.Row():
|
96 |
image_input = gr.Image(elem_id="image_input", type="pil")
|
97 |
output_components = [
|
98 |
+
gr.Textbox(label="Mineral Name",elem_id="predicted_class_name"),
|
99 |
+
gr.Textbox(label="Prediction Scores of Model",elem_id="predicted_scores", lines=5),
|
100 |
+
gr.Textbox(label="Key Facts About Mineral",elem_id="mineral_key_facts", lines=8) # Added new Textbox for key facts
|
101 |
]
|
102 |
image_button = gr.Button("Classify Mineral")
|
103 |
image_button.click(classify_image, inputs=image_input, outputs=output_components)
|
104 |
+
demo.launch(share=True)
|
|
|
|