File size: 4,032 Bytes
53767de
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
import torch
import cv2
import numpy as np
from PIL import Image
import google.generativeai as genai
import gradio as gr
from ultralytics import YOLO
import os

# Force PyTorch to use CPU
device = torch.device("cpu")

# Configure Google AI API
# genai.configure(api_key='AIzaSyATgL92t9qBCe4eqFX1cSfyfkzKMooQM48')
# model = genai.GenerativeModel('gemini-2.0-flash')

genai.configure(api_key='AIzaSyATgL92t9qBCe4eqFX1cSfyfkzKMooQM48')

generation_config = {
    "temperature": 1,
    "top_p": 0.95,
    "top_k": 40,
    "max_output_tokens": 8192,
    "response_mime_type": "text/plain",
}

model = genai.GenerativeModel(
    model_name="gemini-1.5-flash",
    generation_config=generation_config,
)

# Test Gemini API connection
try:
    test_response = model.generate_content("Say Hello!")
    print("Test Response from Gemini:", test_response.text)
except Exception as e:
    print("Error: Gemini API connection failed! Check API key.")
    print(e)
    exit()

class FoodDetectionSystem:
    def __init__(self, model_path):
        self.model = YOLO('best.pt')  # Load trained YOLOv8 model
        self.model.to(device)  # Ensure model runs on CPU

    def detect_food(self, image_path):
        """Detect food items in the image"""
        results = self.model(image_path)  # Run inference on image
        detected_items = []
        
        for result in results:
            boxes = result.boxes.cpu()  # Ensure bounding boxes are on CPU
            for box in boxes:
                class_id = int(box.cls[0])
                conf = float(box.conf[0])
                if conf > 0.5:  # Confidence threshold
                    detected_items.append(result.names[class_id])
        
        return list(set(detected_items))  # Remove duplicates

def generate_recipe(ingredients, calorie_requirement):
    """Generate recipe using Gemini AI"""
    prompt = f"""

    Create a healthy recipe using some or all of these ingredients: {', '.join(ingredients)}.

    The recipe should be approximately {calorie_requirement} calories suggest 2 recipies.

    Please provide:

    Recipi Number 

    1. Recipe name

    2. Ingredients list with quantities

    3. Step-by-step instructions

    4. Approximate calorie count per serving

    """
    
    response = model.generate_content(prompt)
    return response.text

def process_image_and_generate_recipe(image, calorie_requirement):
    """Main function to process image and generate recipe"""
    try:
        # Save uploaded image temporarily
        temp_path = "temp_upload.jpg"
        image.save(temp_path)
        
        print("Image saved at:", temp_path)
        print("food detection start")
        # Initialize and use food detection system
        detector = FoodDetectionSystem('path_to_your_trained_model.pt')
        detected_foods = detector.detect_food(temp_path)
        print(detected_foods)
        if not detected_foods:
            return "No food items detected in the image. Please try another image."
        
        # Generate recipe
        recipe = generate_recipe(detected_foods, calorie_requirement)
        
        # Clean up
        os.remove(temp_path)
        
        return f"Detected Foods: {', '.join(detected_foods)}\n\nGenerated Recipe:\n{recipe}"
    
    except Exception as e:
        return f"An error occurred: {str(e)}"

# Create Gradio interface
def create_interface():
    iface = gr.Interface(
        fn=process_image_and_generate_recipe,
        inputs=[
            gr.Image(type="pil", label="Upload Food Image"),
            gr.Number(label="Desired Calorie Count", value=500)
        ],
        outputs=gr.Textbox(label="Results"),
        title="Food Detection and Recipe Generator",
        description="Upload a food image to detect ingredients and generate a recipe based on your calorie requirements."
    )
    return iface

if __name__ == "__main__":
    iface = create_interface()
    iface.launch()