BrandedYeoh123/Food-Scanner-AI
0
1import gradio as gr2import tensorflow as tf3import numpy as np4from PIL import Image5 6# Load the trained model7import tensorflow as tf8 9model = tf.keras.models.load_model("keras_model.h5", compile=False)10 11# Manually compile the model12model.compile(optimizer='adam', loss='categorical_crossentropy', metrics=['accuracy']) # Ensure you upload model.h513 14labels = ["Pizza", "Burger", "Salad", "Pasta"] # Update with your food classes15 16def predict_image(img):17 from PIL import Image18 import numpy as np19 20 if isinstance(img, np.ndarray):21 img = Image.fromarray(img) # Convert NumPy array to PIL Image22 23 img = img.convert("RGB") # Convert image to RGB24 img = img.resize((224, 224)) # Resize for the model25 img_array = np.array(img) / 255.0 # Normalize26 img_array = np.expand_dims(img_array, axis=0) # Add batch dimension27 28 prediction = model.predict(img_array)29 return {labels[i]: float(prediction[0][i]) for i in range(len(labels))}30 31interface = gr.Interface(fn=predict_image, inputs="image", outputs="label")32interface.launch(share=True)33 