|
import streamlit as st |
|
import pandas as pd |
|
import numpy as np |
|
import cv2 |
|
from PIL import Image |
|
import tensorflow as tf |
|
|
|
|
|
@st.cache_resource |
|
def load_model(): |
|
model = tf.keras.models.load_model('path_to_your_saved_model.h5') |
|
return model |
|
|
|
|
|
def preprocess_image(image): |
|
image = np.array(image.convert('RGB')) |
|
image = cv2.resize(image, (224, 224)) |
|
image = image / 255.0 |
|
image = np.expand_dims(image, axis=0) |
|
return image |
|
|
|
|
|
def predict(image, model): |
|
processed_image = preprocess_image(image) |
|
prediction = model.predict(processed_image) |
|
return prediction |
|
|
|
|
|
def main(): |
|
st.title("Food Item Recognition and Estimation") |
|
st.write("Upload an image of a food item and the model will recognize the food item and estimate its calories.") |
|
|
|
model = load_model() |
|
|
|
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"]) |
|
if uploaded_file is not None: |
|
image = Image.open(uploaded_file) |
|
st.image(image, caption='Uploaded Image.', use_column_width=True) |
|
|
|
st.write("") |
|
st.write("Classifying...") |
|
prediction = predict(image, model) |
|
|
|
st.write(f"Predicted class: {np.argmax(prediction)}") |
|
|
|
if __name__ == "__main__": |
|
main() |
|
|