|
import streamlit as st |
|
import random |
|
from PIL import Image |
|
import requests |
|
import json |
|
from transformers import pipeline |
|
import numpy as np |
|
from transformers import AutoFeatureExtractor |
|
from transformers import AutoModelForImageClassification |
|
|
|
st.set_page_config(layout='wide', |
|
page_title='Food Category Classification & Recipes' |
|
) |
|
|
|
|
|
sidebar_acc = ['App Description', 'About Project'] |
|
sidebar_acc_nav = st.sidebar.radio('**INFORMATION SECTION**', sidebar_acc) |
|
|
|
if sidebar_acc_nav == 'App Description': |
|
st.sidebar.markdown("<h2 style='text-align: center;'> Food Category Classification Description </h2> ", unsafe_allow_html=True) |
|
st.sidebar.markdown("This is a Food Category Image Classifier model that has been trained by [Kaludi](https://huggingface.co/Kaludi) to recognize **12** different categories of foods, which includes **Bread**, **Dairy**, **Dessert**, **Egg**, **Fried Food**, **Fruit**, **Meat**, **Noodles**, **Rice**, **Seafood**, **Soup**, and **Vegetable**. It can accurately classify an image of food into one of these categories by analyzing its visual features. This model can be used by food bloggers, restaurants, and recipe websites to quickly categorize and sort their food images, making it easier to manage their content and provide a better user experience.") |
|
|
|
elif sidebar_acc_nav == 'About Project': |
|
st.sidebar.markdown("<h2 style='text-align: center;'> About Project </h2>", unsafe_allow_html=True) |
|
st.sidebar.markdown("<hr style='text-align: center;'>", unsafe_allow_html=True) |
|
st.sidebar.markdown("<h3 style='text-align: center;'>Project Location:</h3>", unsafe_allow_html=True) |
|
st.sidebar.markdown("<p style='text-align: center;'><strong><a href='https://huggingface.co/Kaludi/food-category-classification-v2.0'>Model</a></strong> | <strong><a href='https://huggingface.co/datasets/Kaludi/food-category-classification-v2.0'>Dataset</a></strong></p>", unsafe_allow_html=True) |
|
st.sidebar.markdown("<hr style='text-align: center;'>", unsafe_allow_html=True) |
|
st.sidebar.markdown("<h3 style='text-align: center;'>Project Creators:</h3>", unsafe_allow_html=True) |
|
st.sidebar.markdown("<p style='text-align: center;'><a href='https://github.com/Kaludii'><strong>AA</strong></a></p>", unsafe_allow_html=True) |
|
st.sidebar.markdown("<p style='text-align: center;'><a href='https://github.com/Kaludii'><strong>AM</strong></a></p>", unsafe_allow_html=True) |
|
st.sidebar.markdown("<p style='text-align: center;'><a href='https://github.com/Kaludii'><strong>BK</strong></a></p>", unsafe_allow_html=True) |
|
st.sidebar.markdown("<p style='text-align: center;'><a href='https://github.com/Kaludii'><strong>DK</strong></a></p>", unsafe_allow_html=True) |
|
|
|
|
|
def main(): |
|
st.title("Food Category Classification & Recipes") |
|
|
|
st.markdown("### Backgroud") |
|
st.markdown("This is a Food Category Image Classifier model that has been trained by [Kaludi](https://huggingface.co/Kaludi) to recognize **12** different categories of foods, which includes **Bread**, **Dairy**, **Dessert**, **Egg**, **Fried Food**, **Fruit**, **Meat**, **Noodles**, **Rice**, **Seafood**, **Soup**, and **Vegetable**. It can accurately classify an image of food into one of these categories by analyzing its visual features. This model can be used by food bloggers, restaurants, and recipe websites to quickly categorize and sort their food images, making it easier to manage their content and provide a better user experience.") |
|
st.header("Try it out!") |
|
|
|
if st.checkbox("Show/Hide Examples"): |
|
st.header("Example Images") |
|
|
|
col1, col2, col3, col4 = st.columns(4) |
|
|
|
with col1: |
|
st.image("examples/example_0.jpg", width=260) |
|
st.image("examples/example_1.jpg", width=260) |
|
|
|
with col2: |
|
st.image("examples/example_2.jpg", width=260) |
|
st.image("examples/example_3.jpg", width=260) |
|
|
|
with col3: |
|
st.image("examples/example_4.jpg", width=260) |
|
st.image("examples/example_5.jpg", width=260) |
|
|
|
with col4: |
|
st.image("examples/example_6.jpg", width=260) |
|
st.image("examples/example_7.jpg", width=260) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
diet_options = ['All', 'Gluten-Free', 'Vegan', 'Vegetarian', 'Dairy-Free'] |
|
diet = st.selectbox('Diet', diet_options) |
|
|
|
|
|
cuisine_options = ['All', 'African', 'Asian', 'Caribbean', 'Central American', 'Europe', 'Middle Eastern', 'North American', 'Oceanic', 'South American'] |
|
|
|
cuisine = st.selectbox('Cuisine', cuisine_options) |
|
|
|
|
|
calories = st.slider("Select Max Calories (Per Serving)", 50, 2000) |
|
|
|
|
|
st.write("Selected: **{}** Max Calories.".format(calories)) |
|
|
|
uploaded_file = st.file_uploader("Upload Files", type=['png','jpeg','jpg']) |
|
|
|
loading_text = st.empty() |
|
|
|
if uploaded_file != None: |
|
loading_text.markdown("Loading...") |
|
img = Image.open(uploaded_file) |
|
extractor = AutoFeatureExtractor.from_pretrained("Kaludi/food-category-classification-v2.0") |
|
model = AutoModelForImageClassification.from_pretrained("Kaludi/food-category-classification-v2.0") |
|
inputs = extractor(img, return_tensors="pt") |
|
outputs = model(**inputs) |
|
|
|
loading_text.empty() |
|
label_num=outputs.logits.softmax(1).argmax(1) |
|
label_num=label_num.item() |
|
|
|
|
|
probs = outputs.logits.softmax(dim=1) |
|
percentage = round(probs[0, label_num].item() * 100, 2) |
|
|
|
st.markdown("### Your Image:") |
|
st.image(img, width=260) |
|
|
|
st.write("The Predicted Classification is:") |
|
|
|
if label_num==0: |
|
st.write("**Bread** (" + f"{percentage}%)") |
|
elif label_num==1: |
|
st.write("**Dairy** (" + f"{percentage}%)") |
|
elif label_num==2: |
|
st.write("**Dessert** (" + f"{percentage}%)") |
|
elif label_num==3: |
|
st.write("**Egg** (" + f"{percentage}%)") |
|
elif label_num==4: |
|
st.write("**Fried Food** (" + f"{percentage}%)") |
|
elif label_num==5: |
|
st.write("**Fruit** (" + f"{percentage}%)") |
|
elif label_num==6: |
|
st.write("**Meat** (" + f"{percentage}%)") |
|
elif label_num==7: |
|
st.write("**Noodles** (" + f"{percentage}%)") |
|
elif label_num==8: |
|
st.write("**Rice** (" + f"{percentage}%)") |
|
elif label_num==9: |
|
st.write("**Seafood** (" + f"{percentage}%)") |
|
elif label_num==10: |
|
st.write("**Soup** (" + f"{percentage}%)") |
|
else: |
|
st.write("**Vegetable** (" + f"{percentage}%)") |
|
|
|
st.write("You Selected **{}** For Diet and **{}** For Cuisine with Max".format(diet, cuisine), calories, "Calories For", ( "**Bread**" if label_num==0 else "**Dairy**" if label_num==1 else "**Dessert**" if label_num==2 else "**Egg**" if label_num==3 else "**Fried Food**" if label_num==4 else "**Fruit**" if label_num==5 else "**Meat**" if label_num==6 else "**Noodles**" if label_num==7 else "**Rice**" if label_num==8 else "**Seafood**" if label_num==9 else "**Soup**" if label_num==10 else "**Vegetable**")) |
|
|
|
|
|
st.markdown("### Your Image:") |
|
st.image(img, width=260) |
|
|
|
url = "https://alcksyjrmd.execute-api.us-east-2.amazonaws.com/default/nutrients_response" |
|
|
|
max_calories = calories |
|
|
|
category = ("Bread" if label_num==0 else "Dairy" if label_num==1 else "Dessert" if label_num==2 else "Egg" if label_num==3 else "Fried" if label_num==4 else "Fruit" if label_num==5 else "Meat" if label_num==6 else "Noodles" if label_num==7 else "Rice" if label_num==8 else "Seafood" if label_num==9 else "**Soup**" if label_num==10 else "Vegetable") |
|
|
|
params = {"f": category, "Calories": max_calories} |
|
|
|
if diet != "All": |
|
params["d"] = diet |
|
|
|
if cuisine != "All": |
|
params["c"] = cuisine |
|
|
|
response = requests.get(url, params=params) |
|
response_json = json.loads(response.content) |
|
|
|
if len(response_json) == 0: |
|
st.markdown("### No Recipe Found:") |
|
st.write("**No Recipe Found, please try another option from the dropdown menus.**") |
|
else: |
|
st.markdown("### Recommended Recipe:") |
|
if len(response_json) > 1: |
|
random_recipe = random.choice(response_json) |
|
st.write("**Title:** ", random_recipe['Title']) |
|
if random_recipe['Image Link'].endswith(".jpg") or random_recipe['Image Link'].endswith(".jpeg") or random_recipe['Image Link'].endswith(".png"): |
|
st.image(random_recipe['Image Link'], width=300) |
|
else: |
|
st.write("**Image Link:** ", random_recipe['Image Link']) |
|
st.write("**Rating:** ", random_recipe['Rating']) |
|
if random_recipe['Description'] != "Description not found": |
|
st.write("**Description:** ", random_recipe['Description']) |
|
st.write("**Ingredients:** ", random_recipe['Ingredients']) |
|
st.write("**Recipe Facts:** ", random_recipe['Recipe Facts']) |
|
st.write("**Directions:** ", random_recipe['Directions']) |
|
st.write("**Nutrition Facts:** ", random_recipe['Nutrition Facts']) |
|
st.write("**Number of Servings:** ", random_recipe['Number of Servings']) |
|
st.write("**Calories:** ", random_recipe['Calories']) |
|
st.write("**Total Fat:** ", random_recipe['Total Fat']) |
|
st.write("**Saturated Fat:** ", random_recipe['Saturated Fat']) |
|
st.write("**Cholesterol:** ", random_recipe['Cholesterol']) |
|
st.write("**Sodium:** ", random_recipe['Sodium']) |
|
st.write("**Total Carbohydrate:** ", random_recipe['Total Carbohydrate']) |
|
st.write("**Dietary Fiber:** ", random_recipe['Dietary Fiber']) |
|
st.write("**Total Sugars:** ", random_recipe['Total Sugars']) |
|
st.write("**Protein:** ", random_recipe['Protein']) |
|
st.write("**Vitamin C:** ", random_recipe['Vitamin C']) |
|
st.write("**Calcium:** ", random_recipe['Calcium']) |
|
st.write("**Iron:** ", random_recipe['Iron']) |
|
st.write("**Potassium:** ", random_recipe['Potassium']) |
|
st.write("**Tags:** ", random_recipe['Tags']) |
|
st.write("**Recipe URL:** ", random_recipe['Recipe URLs']) |
|
st.markdown("### JSON:") |
|
st.write(response_json) |
|
else: |
|
st.write("**Title:** ", response_json[0]['Title']) |
|
if response_json[0]['Image Link'].endswith(".jpg") or response_json[0]['Image Link'].endswith(".jpeg") or response_json[0]['Image Link'].endswith(".png"): |
|
st.image(response_json[0]['Image Link'], width=300) |
|
else: |
|
st.write("**Image Link:** ", response_json[0]['Image Link']) |
|
st.write("**Rating:** ", response_json[0]['Rating']) |
|
if response_json[0]['Description'] != "Description not found": |
|
st.write("**Description:** ", response_json[0]['Description']) |
|
st.write("**Ingredients:** ", response_json[0]['Ingredients']) |
|
st.write("**Recipe Facts:** ", response_json[0]['Recipe Facts']) |
|
st.write("**Directions:** ", response_json[0]['Directions']) |
|
st.write("**Nutrition Facts:** ", response_json[0]['Nutrition Facts']) |
|
st.write("**Number of Servings:** ", response_json[0]['Number of Servings']) |
|
st.write("**Calories:** ", response_json[0]['Calories']) |
|
st.write("**Total Fat:** ", response_json[0]['Total Fat']) |
|
st.write("**Saturated Fat:** ", response_json[0]['Saturated Fat']) |
|
st.write("**Cholesterol:** ", response_json[0]['Cholesterol']) |
|
st.write("**Sodium:** ", response_json[0]['Sodium']) |
|
st.write("**Total Carbohydrate:** ", response_json[0]['Total Carbohydrate']) |
|
st.write("**Dietary Fiber:** ", response_json[0]['Dietary Fiber']) |
|
st.write("**Total Sugars:** ", response_json[0]['Total Sugars']) |
|
st.write("**Protein:** ", response_json[0]['Protein']) |
|
st.write("**Vitamin C:** ", response_json[0]['Vitamin C']) |
|
st.write("**Calcium:** ", response_json[0]['Calcium']) |
|
st.write("**Iron:** ", response_json[0]['Iron']) |
|
st.write("**Potassium:** ", response_json[0]['Potassium']) |
|
st.write("**Tags:** ", response_json[0]['Tags']) |
|
st.write("**Recipe URL:** ", response_json[0]['Recipe URLs']) |
|
st.markdown("### JSON:") |
|
st.write(response_json) |
|
|
|
st.markdown("<hr style='text-align: center;'>", unsafe_allow_html=True) |
|
st.markdown("<p style='text-align: center'><a href='https://github.com/Kaludii'>Github</a> | <a href='https://huggingface.co/Kaludi'>HuggingFace</a></p>", unsafe_allow_html=True) |
|
|
|
if __name__ == '__main__': |
|
main() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|