gouravs300's picture
Upload 14 files
64c2f0e
raw
history blame contribute delete
No virus
4.57 kB
import tensorflow as tf
import streamlit as st
# import cv2
from PIL import Image
from streamlit_image_select import image_select
# import os
def load_and_prep_image(filename, img_shape=224, scale=True):
"""
Reads in an image from filename, turns it into a tensor and reshapes into
(224, 224, 3).
Parameters
----------
filename (str): string filename of target image
img_shape (int): size to resize target image to, default 224
scale (bool): whether to scale pixel values to range(0, 1), default True
"""
# # Read in the image
# img = tf.io.read_file(filename)
# # Decode it into a tensor
# img = tf.io.decode_image(img)
img = tf.convert_to_tensor(filename)
# Resize the image
img = tf.image.resize(img, [img_shape, img_shape])
if scale:
# Rescale the image (get all values between 0 and 1)
return img/255.
else:
return img
class_names = ['apple_pie', 'baby_back_ribs', 'baklava', 'beef_carpaccio', 'beef_tartare', 'beet_salad', 'beignets',
'bibimbap', 'bread_pudding', 'breakfast_burrito', 'bruschetta', 'caesar_salad', 'cannoli', 'caprese_salad', 'carrot_cake',
'ceviche', 'cheese_plate', 'cheesecake', 'chicken_curry', 'chicken_quesadilla', 'chicken_wings', 'chocolate_cake',
'chocolate_mousse', 'churros', 'clam_chowder', 'club_sandwich', 'crab_cakes', 'creme_brulee', 'croque_madame',
'cup_cakes', 'deviled_eggs', 'donuts', 'dumplings', 'edamame', 'eggs_benedict', 'escargots', 'falafel', 'filet_mignon',
'fish_and_chips', 'foie_gras', 'french_fries', 'french_onion_soup', 'french_toast', 'fried_calamari', 'fried_rice',
'frozen_yogurt', 'garlic_bread', 'gnocchi', 'greek_salad', 'grilled_cheese_sandwich', 'grilled_salmon', 'guacamole',
'gyoza', 'hamburger', 'hot_and_sour_soup', 'hot_dog', 'huevos_rancheros', 'hummus', 'ice_cream', 'lasagna', 'lobster_bisque',
'lobster_roll_sandwich', 'macaroni_and_cheese', 'macarons', 'miso_soup', 'mussels', 'nachos', 'omelette', 'onion_rings',
'oysters', 'pad_thai', 'paella', 'pancakes', 'panna_cotta', 'peking_duck', 'pho', 'pizza', 'pork_chop', 'poutine', 'prime_rib',
'pulled_pork_sandwich', 'ramen', 'ravioli', 'red_velvet_cake', 'risotto', 'samosa', 'sashimi', 'scallops', 'seaweed_salad',
'shrimp_and_grits', 'spaghetti_bolognese', 'spaghetti_carbonara', 'spring_rolls', 'steak', 'strawberry_shortcake', 'sushi',
'tacos', 'takoyaki', 'tiramisu', 'tuna_tartare', 'waffles']
#load model
@st.cache(allow_output_mutation = True)
def cache_model(model_name):
model = tf.keras.models.load_model(model_name)
return (model)
model = cache_model("101_food_class_100_percent_saved_big_model")
# model = tf.keras.models.load_model("101_food_class_100_percent_saved_big_model")
st.write("""
# Food Classification App
"""
)
st.write("""#### ***Upload food image and this app will classify the uploaded image from one of the mentioned categories.***""")
st.write("""
**Some major food categories**
```
pizza, cup_cakes, donuts, samosa, ice_cream, french_fries, waffles etc.
```
for full categories list please visit the [link](https://github.com/gourav300/food_app)
""")
### load file
uploaded_file = st.file_uploader("Upload an image file for above mentioned Food Categories or select a food image", type=["jpg", "png", "jpeg"])
test_img = image_select(
label="Select a sample food image",
images=[
"test_images/Cardamom-Saffron-Cupcakes-1.jpg",
"test_images/doughnut_1.jpg",
"test_images/frenchfries.jpg",
"test_images/Punjabi-Samosa-2.jpg",
"test_images/dumpling.jpg",
"test_images/pizza_1.jpg",
"test_images/waffle.jpg",
"test_images/pancake.jpg"
],
captions=["Cupcake", "Doughnut", "French fries", "Samosa", "Dumpling", "Pizza", "Waffle", "Pancake"],
)
# st.set_option('deprecation.showfileUploaderEncoding', False)
if uploaded_file is not None:
image = Image.open(uploaded_file)
st.image(image,width = 500)#, use_column_width=True)
else:
image = Image.open(test_img)
st.image(image, width = 500)#,use_column_width=True)
# Load the image and make predictions
img = load_and_prep_image(image, scale=False) # don't scale images for EfficientNet predictions
pred_prob = model.predict(tf.expand_dims(img, axis=0)) # model accepts tensors of shape [None, 224, 224, 3]
pred_class = class_names[pred_prob.argmax()] # find the predicted class
st.write(f"## Predicted food: {pred_class}, with Probability: {pred_prob.max():.2f}")