Taidat commited on
Commit
e7e0f6d
·
verified ·
1 Parent(s): 2236fba

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +10 -10
app.py CHANGED
@@ -6,30 +6,30 @@ from PIL import Image
6
  # Load model
7
  model = tf.keras.models.load_model("model.h5")
8
 
9
- # Correct class names in training order
10
  class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs', 'metal', 'organic', 'paper', 'plastic']
11
-
12
- # Match training input size
13
  IMG_SIZE = (224, 224)
14
 
15
  def preprocess(image):
16
  image = image.resize(IMG_SIZE)
17
  image_array = tf.keras.preprocessing.image.img_to_array(image)
18
- image_array = image_array / 255.0 # Normalize if this was done in training
19
  return np.expand_dims(image_array, axis=0)
20
 
21
  def predict(image):
22
  input_tensor = preprocess(image)
23
  predictions = model.predict(input_tensor)[0]
24
- top_class = class_names[np.argmax(predictions)]
25
- confidence = np.max(predictions) * 100
26
- return f"{top_class} ({confidence:.2f}%)"
 
27
 
28
  demo = gr.Interface(
29
  fn=predict,
30
- inputs=gr.Image(type="pil", label="Upload an image of a recyclable item to classify it."),
31
- outputs=gr.Textbox(label="Output"),
32
- title="Waste Image Classifier"
 
33
  )
34
 
35
  if __name__ == "__main__":
 
6
  # Load model
7
  model = tf.keras.models.load_model("model.h5")
8
 
9
+ # Classes in training order
10
  class_names = ['batteries', 'clothes', 'e-waste', 'glass', 'light blubs', 'metal', 'organic', 'paper', 'plastic']
 
 
11
  IMG_SIZE = (224, 224)
12
 
13
  def preprocess(image):
14
  image = image.resize(IMG_SIZE)
15
  image_array = tf.keras.preprocessing.image.img_to_array(image)
16
+ image_array = image_array / 255.0 # Normalize
17
  return np.expand_dims(image_array, axis=0)
18
 
19
  def predict(image):
20
  input_tensor = preprocess(image)
21
  predictions = model.predict(input_tensor)[0]
22
+ top_idx = np.argmax(predictions)
23
+ top_class = class_names[top_idx]
24
+ confidence = predictions[top_idx] * 100
25
+ return f"Predicted: {top_class} ({confidence:.2f}%)"
26
 
27
  demo = gr.Interface(
28
  fn=predict,
29
+ inputs=gr.Image(type="pil", label="Upload an image of a recyclable item"),
30
+ outputs=gr.Textbox(label="Prediction"),
31
+ title="♻️ Waste Image Classifier",
32
+ description="Classify recyclable waste images into 9 categories using a MobileNetV2-based model."
33
  )
34
 
35
  if __name__ == "__main__":