methestrikerx100 commited on
Commit
41e9cfe
·
verified ·
1 Parent(s): df4beca

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +42 -17
app.py CHANGED
@@ -5,18 +5,45 @@ import gradio as gr
5
  from tensorflow import keras
6
  from keras.models import load_model
7
 
8
- model_path = "VGG_16.h5"
9
- model = load_model(model_path, compile=False)
10
-
11
-
12
 
 
 
13
 
14
  # Define the class labels
15
  class_labels = ['biotite', 'granite', 'olivine', 'plagioclase', 'staurolite']
16
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
17
  # Define the function to make predictions
18
  def classify_image(image):
19
- # Preprocess the image
 
 
 
 
 
20
  image = np.array(image)
21
  image = Image.fromarray(image.astype(np.uint8), 'RGB')
22
  image = image.resize((224, 224))
@@ -38,15 +65,13 @@ def classify_image(image):
38
  return predicted_class_name, predicted_scores
39
 
40
  # Create the Gradio interface
41
- iface = gr.Interface(
42
- fn=classify_image,
43
- inputs=gr.Image(type="pil"),
44
- outputs=[
45
- gr.Textbox(label="Predicted Class"),
46
- gr.Textbox(label="Prediction Scores", lines=5),
47
- ],
48
- title="Mineral Identification App",
49
- )
50
-
51
- # Launch the app
52
- iface.launch()
 
5
  from tensorflow import keras
6
  from keras.models import load_model
7
 
8
+ # Load the classification model
9
+ model = "B7_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):
41
+ # Check if the input is a mineral
42
+ is_mineral = detect_mineral(image)
43
+ if not is_mineral:
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))
 
65
  return predicted_class_name, predicted_scores
66
 
67
  # Create the Gradio interface
68
+ with gr.Blocks() as demo:
69
+ with gr.Row():
70
+ image_input = gr.Image(elem_id="image_input", type="pil")
71
+ output_components = [
72
+ gr.Textbox(elem_id="predicted_class_name"),
73
+ gr.Textbox(elem_id="predicted_scores", lines=5)
74
+ ]
75
+ image_button = gr.Button("Classify Image")
76
+ image_button.click(classify_image, inputs=image_input, outputs=output_components)
77
+ demo.launch(auth=("admin", "pass1234"), share=True, auth_message="Welcome To Mineral Identification App, Please login to use the app.")