Suhani-2407 commited on
Commit
0e00e06
·
verified ·
1 Parent(s): 72b77c0

Update app.py

Browse files
Files changed (1) hide show
  1. app.py +41 -0
app.py CHANGED
@@ -0,0 +1,41 @@
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ import gradio as gr
2
+ import tensorflow as tf
3
+ import numpy as np
4
+ from PIL import Image
5
+ from tensorflow.keras.preprocessing.image import ImageDataGenerator
6
+
7
+ # Load the trained model
8
+ model = tf.keras.models.load_model("MobileNet_model.h5") # Ensure the model file is in the same directory
9
+
10
+ # Define class names from your dataset
11
+ class_names = ["Fake", "Low", "Medium", "High"] # Update based on test_generator.class_indices.keys()
12
+
13
+ # Image Preprocessing
14
+ img_size = (128, 128) # Same as used in test_generator
15
+
16
+ def preprocess_image(image):
17
+ image = image.resize(img_size) # Resize to (128,128)
18
+ image = np.array(image) / 255.0 # Normalize as done in ImageDataGenerator (rescale=1./255)
19
+ image = np.expand_dims(image, axis=0) # Add batch dimension
20
+ return image
21
+
22
+ # Prediction Function
23
+ def predict(image):
24
+ image = preprocess_image(image)
25
+ predictions = model.predict(image)
26
+ predicted_class = np.argmax(predictions, axis=1)[0] # Get the predicted class index
27
+ confidence_scores = {class_names[i]: float(predictions[0][i]) for i in range(len(class_names))} # Get probability scores
28
+
29
+ return {"Predicted Class": class_names[predicted_class], "Confidence Scores": confidence_scores}
30
+
31
+ # Gradio Interface
32
+ interface = gr.Interface(
33
+ fn=predict,
34
+ inputs=gr.Image(type="pil"),
35
+ outputs=gr.JSON(), # Returns class and confidence scores
36
+ title="Waste Classification Model",
37
+ description="Upload an image to classify it into one of four categories: Fake, Low, Medium, or High."
38
+ )
39
+
40
+ if __name__ == "__main__":
41
+ interface.launch()