peshoet commited on
Commit
1ef98d2
·
verified ·
1 Parent(s): 93090e1

Create app.py

Browse files
Files changed (1) hide show
  1. app.py +7 -14
app.py CHANGED
@@ -1,34 +1,27 @@
1
  import gradio as gr
2
  from tensorflow.keras.models import load_model
3
  import numpy as np
 
4
 
5
- # Load the model
6
- model = load_model(r"C:\Users\uSeR\Documents\ai-tutor-ruangguru\project\waste_classifier_model.keras")
 
7
 
8
  # Prediction function
9
  def classify_image(image):
10
- # Ensure the image is in the expected format
11
  if image is None:
12
  return "No image provided."
13
-
14
- # Convert the image to a numpy array
15
  image = np.array(image)
16
 
17
- # Check if the image has the expected shape and resize if necessary
18
  if image.shape != (128, 128, 3):
19
- image = np.resize(image, (128, 128, 3)) # Resize the image to (128, 128, 3)
20
 
21
- # Normalize the image
22
  image = image / 255.0
23
-
24
- # Add batch dimension
25
  image = np.expand_dims(image, axis=0)
26
 
27
- # Make prediction
28
  prediction = model.predict(image)
29
-
30
- # Interpret the prediction
31
- class_label = 'Organic' if prediction[0][0] > 0.5 else 'Recycleable' # Adjust labels based on your model's output
32
  return class_label
33
 
34
  # Create Gradio interface
 
1
  import gradio as gr
2
  from tensorflow.keras.models import load_model
3
  import numpy as np
4
+ import os
5
 
6
+ # Load the model (ensure it's in the same directory as app.py)
7
+ model_path = os.path.join(os.path.dirname(__file__), 'waste_classifier_model.keras')
8
+ model = load_model(model_path)
9
 
10
  # Prediction function
11
  def classify_image(image):
 
12
  if image is None:
13
  return "No image provided."
14
+
 
15
  image = np.array(image)
16
 
 
17
  if image.shape != (128, 128, 3):
18
+ image = np.resize(image, (128, 128, 3))
19
 
 
20
  image = image / 255.0
 
 
21
  image = np.expand_dims(image, axis=0)
22
 
 
23
  prediction = model.predict(image)
24
+ class_label = 'Organic' if prediction[0][0] > 0.5 else 'Recycleable'
 
 
25
  return class_label
26
 
27
  # Create Gradio interface