methestrikerx100 commited on
Commit
4e68c03
·
verified ·
1 Parent(s): 3aae9d2

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +27 -20
app.py CHANGED
@@ -6,16 +6,12 @@ import gradio as gr
6
  from tensorflow import keras
7
  from keras.models import load_model
8
 
9
-
10
-
11
-
12
- #Back_End_code
13
  # Load the classification model
14
- model = r"C:\\Users\\nanom\\OneDrive\\Desktop\\midterm implementation\\Hugging_face_model_final.h5"
15
  model = tf.keras.models.load_model(model)
16
 
17
  # Load the mineral detection model
18
- 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")
19
 
20
  # Define the class labels
21
  class_labels = ['biotite', 'granite', 'olivine', 'plagioclase', 'staurolite']
@@ -26,7 +22,7 @@ mineral_facts = {
26
  '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.",
27
  '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."
28
  }
29
- # function to preprocess the image for mineral detection
30
  def preprocess_image_detection(img_array):
31
  if img_array is None:
32
  return None
@@ -36,17 +32,17 @@ def preprocess_image_detection(img_array):
36
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
37
  return img_array
38
 
39
- # function to preprocess the image for classification
40
  def preprocess_image_classification(img_array):
41
  if img_array is None:
42
  return None
43
- img = (img_array * 255).astype(np.uint8)
44
- img_array = cv2.resize(img, (224, 224)) # resize to 224x224
45
  img_array = img_array.astype(np.uint8)
46
- img_array = np.expand_dims(img_array, axis=0)
47
  return img_array
48
 
49
- # function to detect if the input is a mineral
50
  def detect_mineral(image):
51
  if image is not None:
52
  image = Image.fromarray(np.array(image).astype(np.uint8), 'RGB')
@@ -64,24 +60,24 @@ def detect_mineral(image):
64
  # Handle the case where no image is provided
65
  return "No image provided."
66
 
67
- # function to make predictions
68
  def classify_image(image):
69
  # Check if the input is a mineral
70
  is_mineral = detect_mineral(image)
71
  if not is_mineral:
72
  return "Input is not a Microscopic mineral thin section, Please Insert a thin section.", "", ""
73
 
74
- # preprocess the image for classification
75
  image = preprocess_image_classification(np.array(image))
76
  if image is None:
77
  return "Error preprocessing image.", "", ""
78
 
79
- # prediction
80
  prediction = model.predict(image)
81
  class_idx = np.argmax(prediction)
82
  prediction_scores = prediction[0]
83
 
84
- # convert prediction scores to percentages
85
  prediction_scores_percentages = [f"{score * 100:.2f}%" for score in prediction_scores]
86
 
87
  # Get the predicted class name and key facts
@@ -91,12 +87,23 @@ def classify_image(image):
91
 
92
  return predicted_class_name, predicted_scores, mineral_key_facts
93
 
94
-
95
- #Front_End_code
96
  app_title = "Mineral Identification using AI"
97
  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."
98
 
99
- 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:
 
 
 
 
 
 
 
 
 
 
 
 
 
100
  with gr.Row():
101
  image_input = gr.Image(elem_id="image_input", type="pil")
102
  output_components = [
@@ -106,4 +113,4 @@ with gr.Blocks(title=app_title, css=".gradio-container {display: flex; justify-c
106
  ]
107
  image_button = gr.Button("Classify Mineral")
108
  image_button.click(classify_image, inputs=image_input, outputs=output_components)
109
- demo.launch(share=True)
 
6
  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']
 
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:
28
  return None
 
32
  img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
33
  return img_array
34
 
35
+ # Function to preprocess the image for classification
36
  def preprocess_image_classification(img_array):
37
  if img_array is None:
38
  return None
39
+ img = (img_array * 255).astype(np.uint8) # Convert back to uint8
40
+ img_array = cv2.resize(img, (224, 224)) # Resize to 224x224
41
  img_array = img_array.astype(np.uint8)
42
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
43
  return img_array
44
 
45
+ # Define the function to detect if the input is a mineral
46
  def detect_mineral(image):
47
  if image is not None:
48
  image = Image.fromarray(np.array(image).astype(np.uint8), 'RGB')
 
60
  # Handle the case where no image is provided
61
  return "No image provided."
62
 
63
+ # Define the function to make predictions
64
  def classify_image(image):
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
 
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(
94
+ title=app_title,
95
+ css=".gradio-container {display: flex; justify-content: center; align-items: center; height: 100vh;}",
96
+ theme=gr.themes.Monochrome(),
97
+
98
+
99
+
100
+
101
+
102
+
103
+
104
+
105
+ ) as demo:
106
+ # Your existing code for creating the interface components
107
  with gr.Row():
108
  image_input = gr.Image(elem_id="image_input", type="pil")
109
  output_components = [
 
113
  ]
114
  image_button = gr.Button("Classify Mineral")
115
  image_button.click(classify_image, inputs=image_input, outputs=output_components)
116
+ demo.launch(auth=("admin", "pass1234"),auth_message="Welcome To Mineral Identification App.")