Spaces:
Sleeping
Sleeping
Upload 2 files
Browse filesCreated Recipe Generator
- app.py +108 -0
- yolov8n.pt +3 -0
app.py
ADDED
|
@@ -0,0 +1,108 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import streamlit as st
|
| 2 |
+
import torch
|
| 3 |
+
import cv2
|
| 4 |
+
import numpy as np
|
| 5 |
+
from ultralytics import YOLO
|
| 6 |
+
from PIL import Image
|
| 7 |
+
import easyocr
|
| 8 |
+
import requests
|
| 9 |
+
|
| 10 |
+
def load_model():
|
| 11 |
+
# Load the pre-trained YOLOv8 model for object detection
|
| 12 |
+
model = YOLO("yolov8n.pt") # You can use a custom-trained model for food detection
|
| 13 |
+
return model
|
| 14 |
+
|
| 15 |
+
def detect_ingredients(image, model):
|
| 16 |
+
results = model(image)
|
| 17 |
+
detected_items = set()
|
| 18 |
+
for result in results:
|
| 19 |
+
for box in result.boxes:
|
| 20 |
+
cls = result.names[int(box.cls[0])]
|
| 21 |
+
detected_items.add(cls)
|
| 22 |
+
return list(detected_items)
|
| 23 |
+
|
| 24 |
+
def extract_text(image):
|
| 25 |
+
# Use EasyOCR to extract text from the image
|
| 26 |
+
reader = easyocr.Reader(['en']) # Specify language
|
| 27 |
+
results = reader.readtext(np.array(image)) # Convert PIL image to NumPy
|
| 28 |
+
extracted_text = [text[1] for text in results] # Extract detected text
|
| 29 |
+
return extracted_text
|
| 30 |
+
|
| 31 |
+
MEALDB_API_URL = "https://www.themealdb.com/api/json/v1/1"
|
| 32 |
+
|
| 33 |
+
def fetch_ingredients():
|
| 34 |
+
"""Fetches a list of available ingredients from TheMealDB API."""
|
| 35 |
+
url = f"{MEALDB_API_URL}/list.php?i=list"
|
| 36 |
+
response = requests.get(url)
|
| 37 |
+
if response.status_code == 200:
|
| 38 |
+
data = response.json()
|
| 39 |
+
return [item["strIngredient"].lower() for item in data["meals"]]
|
| 40 |
+
return []
|
| 41 |
+
|
| 42 |
+
def filter_valid_ingredients(detected_ingredients):
|
| 43 |
+
"""Filters detected ingredients against TheMealDB ingredient list."""
|
| 44 |
+
valid_ingredients = fetch_ingredients()
|
| 45 |
+
return [ing for ing in detected_ingredients if ing.lower() in valid_ingredients]
|
| 46 |
+
|
| 47 |
+
def get_recipes(ingredients):
|
| 48 |
+
"""Fetch recipes from TheMealDB based on detected ingredients."""
|
| 49 |
+
recipe_list = set()
|
| 50 |
+
|
| 51 |
+
for ingredient in ingredients:
|
| 52 |
+
url = f"{MEALDB_API_URL}/filter.php?i={ingredient}"
|
| 53 |
+
response = requests.get(url)
|
| 54 |
+
|
| 55 |
+
if response.status_code == 200:
|
| 56 |
+
data = response.json()
|
| 57 |
+
if data["meals"]:
|
| 58 |
+
for meal in data["meals"]:
|
| 59 |
+
recipe_list.add(meal["strMeal"])
|
| 60 |
+
|
| 61 |
+
return list(recipe_list) if recipe_list else ["No matching recipes found."]
|
| 62 |
+
|
| 63 |
+
def main():
|
| 64 |
+
st.title("VQA Recipe Generator")
|
| 65 |
+
st.write("Upload an image of ingredients, and we'll suggest recipes you can make!")
|
| 66 |
+
|
| 67 |
+
with st.expander("How It Works"):
|
| 68 |
+
st.write("""
|
| 69 |
+
1. **Upload an Image**: Upload a photo of ingredients you have.
|
| 70 |
+
2. **Ingredient Detection**: The app uses a YOLOv8 model to detect visible ingredients.
|
| 71 |
+
3. **Text Extraction**: Any text in the image (e.g., labels) is extracted using EasyOCR.
|
| 72 |
+
4. **Ingredient Validation**: The detected ingredients are cross-checked with TheMealDB database.
|
| 73 |
+
5. **Recipe Suggestions**: The app fetches recipes that match the available ingredients.
|
| 74 |
+
P.S This is a work in progress so it can't cater all ingredients and can't really detect if ingredients don't have labels.
|
| 75 |
+
""")
|
| 76 |
+
|
| 77 |
+
model = load_model()
|
| 78 |
+
|
| 79 |
+
uploaded_file = st.file_uploader("Upload an image", type=["jpg", "png", "jpeg"])
|
| 80 |
+
if uploaded_file:
|
| 81 |
+
image = Image.open(uploaded_file)
|
| 82 |
+
st.image(image, caption="Uploaded Image", use_column_width=True)
|
| 83 |
+
|
| 84 |
+
# Convert image for processing
|
| 85 |
+
img_array = np.array(image)
|
| 86 |
+
img_array = cv2.cvtColor(img_array, cv2.COLOR_RGB2BGR)
|
| 87 |
+
|
| 88 |
+
with st.spinner("Detecting ingredients..."):
|
| 89 |
+
detected_ingredients = detect_ingredients(img_array, model)
|
| 90 |
+
extracted_text = extract_text(image)
|
| 91 |
+
|
| 92 |
+
# Merge OCR text with detected objects
|
| 93 |
+
detected_ingredients.extend(extracted_text)
|
| 94 |
+
|
| 95 |
+
# Filter only valid ingredients from TheMealDB
|
| 96 |
+
valid_ingredients = filter_valid_ingredients(detected_ingredients)
|
| 97 |
+
|
| 98 |
+
st.subheader("Detected Ingredients:")
|
| 99 |
+
st.write(", ".join(valid_ingredients))
|
| 100 |
+
|
| 101 |
+
recipes = get_recipes(valid_ingredients)
|
| 102 |
+
|
| 103 |
+
st.subheader("Suggested Recipes:")
|
| 104 |
+
for recipe in recipes:
|
| 105 |
+
st.write(f"- {recipe}")
|
| 106 |
+
|
| 107 |
+
if __name__ == "__main__":
|
| 108 |
+
main()
|
yolov8n.pt
ADDED
|
@@ -0,0 +1,3 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
version https://git-lfs.github.com/spec/v1
|
| 2 |
+
oid sha256:f59b3d833e2ff32e194b5bb8e08d211dc7c5bdf144b90d2c8412c47ccfc83b36
|
| 3 |
+
size 6549796
|