methestrikerx100 commited on
Commit
18d53eb
·
verified ·
1 Parent(s): b4f4155

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +37 -20
app.py CHANGED
@@ -1,6 +1,7 @@
1
  import tensorflow as tf
2
  from PIL import Image
3
  import numpy as np
 
4
  import gradio as gr
5
  from tensorflow import keras
6
  from keras.models import load_model
@@ -9,32 +10,49 @@ from keras.models import load_model
9
  model = "Hugging_face_model_final.h5"
10
  model = tf.keras.models.load_model(model)
11
 
12
- # Load the mineral detection model mineral_detection_model_Final_4_18_2024.h5
13
  mineral_detection_model = tf.keras.models.load_model("mineral_detection_model_Final_4_18_2024.h5")
14
 
15
  # Define the class labels
16
  class_labels = ['biotite', 'granite', 'olivine', 'plagioclase', 'staurolite']
17
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
18
  # Define the function to detect if the input is a mineral
19
  def detect_mineral(image):
20
- # Preprocess the image for mineral detection model
21
  if image is not None:
22
  image = Image.fromarray(np.array(image).astype(np.uint8), 'RGB')
23
- # Rest of your code for detecting minerals
 
 
 
 
 
 
 
 
 
24
  else:
25
  # Handle the case where no image is provided
26
  return "No image provided."
27
- image = np.array(image)
28
- image = Image.fromarray(image.astype(np.uint8), 'RGB')
29
- image = image.resize((150, 150)) # Assuming the model expects 224x224 images
30
- image = np.array(image) / 255.0
31
- image = np.expand_dims(image, axis=0)
32
-
33
- # Make prediction
34
- prediction = mineral_detection_model.predict(image)
35
- is_mineral = prediction[0][0] < 0.5 # Assuming binary classification
36
-
37
- return is_mineral
38
 
39
  # Define the function to make predictions
40
  def classify_image(image):
@@ -44,11 +62,9 @@ def classify_image(image):
44
  return "Input is not a mineral.", ""
45
 
46
  # Preprocess the image for classification
47
- image = np.array(image)
48
- image = Image.fromarray(image.astype(np.uint8), 'RGB')
49
- image = image.resize((224, 224))
50
- image = np.array(image) / 255.0
51
- image = np.expand_dims(image, axis=0)
52
 
53
  # Make prediction
54
  prediction = model.predict(image)
@@ -74,4 +90,5 @@ with gr.Blocks() as demo:
74
  ]
75
  image_button = gr.Button("Classify Image")
76
  image_button.click(classify_image, inputs=image_input, outputs=output_components)
77
- demo.launch( share=True, auth_message="Welcome To Mineral Identification App, Please login to use the app.")
 
 
1
  import tensorflow as tf
2
  from PIL import Image
3
  import numpy as np
4
+ import cv2
5
  import gradio as gr
6
  from tensorflow import keras
7
  from keras.models import load_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:
22
+ return None
23
+ img = (img_array * 255).astype(np.uint8) # Convert back to uint8
24
+ img_array = cv2.resize(img, (150, 150)) # Resize to 150x150
25
+ img_array = img_array.astype(np.uint8)
26
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
27
+ return img_array
28
+
29
+ # Function to preprocess the image for classification
30
+ def preprocess_image_classification(img_array):
31
+ if img_array is None:
32
+ return None
33
+ img = (img_array * 255).astype(np.uint8) # Convert back to uint8
34
+ img_array = cv2.resize(img, (224, 224)) # Resize to 224x224
35
+ img_array = img_array.astype(np.uint8)
36
+ img_array = np.expand_dims(img_array, axis=0) # Add batch dimension
37
+ return img_array
38
+
39
  # Define the function to detect if the input is a mineral
40
  def detect_mineral(image):
 
41
  if image is not None:
42
  image = Image.fromarray(np.array(image).astype(np.uint8), 'RGB')
43
+ image = np.array(image)
44
+ image = Image.fromarray(image.astype(np.uint8), 'RGB')
45
+ image = image.resize((150, 150)) # Assuming the model expects 150x150 images
46
+ image = np.array(image) / 255.0
47
+ image = np.expand_dims(image, axis=0)
48
+
49
+ # Make prediction
50
+ prediction = mineral_detection_model.predict(image)
51
+ is_mineral = prediction[0][0] < 0.5 # Assuming binary classification
52
+ return is_mineral
53
  else:
54
  # Handle the case where no image is provided
55
  return "No image provided."
 
 
 
 
 
 
 
 
 
 
 
56
 
57
  # Define the function to make predictions
58
  def classify_image(image):
 
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)
 
90
  ]
91
  image_button = gr.Button("Classify Image")
92
  image_button.click(classify_image, inputs=image_input, outputs=output_components)
93
+
94
+ demo.launch(share=True, auth_message="Welcome To Mineral Identification App, Please login to use the app.")