Create app.py
Browse files
app.py
ADDED
|
@@ -0,0 +1,47 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import pandas as pd
|
| 3 |
+
import numpy as np
|
| 4 |
+
import cv2
|
| 5 |
+
from PIL import Image
|
| 6 |
+
import tensorflow as tf
|
| 7 |
+
|
| 8 |
+
# Function to load the model
|
| 9 |
+
@st.cache_resource
|
| 10 |
+
def load_model():
|
| 11 |
+
model = tf.keras.models.load_model('path_to_your_saved_model.h5') # Provide the path to your model
|
| 12 |
+
return model
|
| 13 |
+
|
| 14 |
+
# Function to preprocess the image
|
| 15 |
+
def preprocess_image(image):
|
| 16 |
+
image = np.array(image.convert('RGB'))
|
| 17 |
+
image = cv2.resize(image, (224, 224)) # Resize the image to the input shape required by your model
|
| 18 |
+
image = image / 255.0 # Normalize the image
|
| 19 |
+
image = np.expand_dims(image, axis=0)
|
| 20 |
+
return image
|
| 21 |
+
|
| 22 |
+
# Function to predict the class
|
| 23 |
+
def predict(image, model):
|
| 24 |
+
processed_image = preprocess_image(image)
|
| 25 |
+
prediction = model.predict(processed_image)
|
| 26 |
+
return prediction
|
| 27 |
+
|
| 28 |
+
# Main app
|
| 29 |
+
def main():
|
| 30 |
+
st.title("Food Item Recognition and Estimation")
|
| 31 |
+
st.write("Upload an image of a food item and the model will recognize the food item and estimate its calories.")
|
| 32 |
+
|
| 33 |
+
model = load_model()
|
| 34 |
+
|
| 35 |
+
uploaded_file = st.file_uploader("Choose an image...", type=["jpg", "jpeg", "png"])
|
| 36 |
+
if uploaded_file is not None:
|
| 37 |
+
image = Image.open(uploaded_file)
|
| 38 |
+
st.image(image, caption='Uploaded Image.', use_column_width=True)
|
| 39 |
+
|
| 40 |
+
st.write("")
|
| 41 |
+
st.write("Classifying...")
|
| 42 |
+
prediction = predict(image, model)
|
| 43 |
+
|
| 44 |
+
st.write(f"Predicted class: {np.argmax(prediction)}") # Update with your model's prediction logic
|
| 45 |
+
|
| 46 |
+
if __name__ == "__main__":
|
| 47 |
+
main()
|