File size: 3,595 Bytes
e568c32
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import tensorflow as tf
import numpy as np
import os

import requests
import json
# def view_and_predict(target_dir, target_class, model_path):

#     # Reading image and plotting image
#     img = tf.io.read_file(target_folder + '/' + random_image[0])
#     img = tf.io.decode_image(img)
#     img = tf.image.resize(img,(224,224))
#     img_show = img/255.

#     pred = model_pred(model_path, img, class_names)

#     plt.imshow(img_show)
#     plt.title(f"Real Label: {target_class},   prediction: {pred}")
#     plt.axis('off');

#     return img


classes = ['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']

def load_prepare_image(filepath, img_size, rescale=False):
    img = tf.io.decode_image(filepath, channels=3)
    img = tf.image.resize(img, img_size)

    if rescale:
        return img/255.
    else:
        return img

def model_pred(model_path, img, class_names=classes):
    # Load TFLite model and allocate tensors.
    interpreter = tf.lite.Interpreter(model_path=model_path)
    #allocate the tensors
    interpreter.allocate_tensors()

    input_tensor= np.array(np.expand_dims(img,0), dtype=np.float32)
    input_index = interpreter.get_input_details()[0]["index"]

    # setting input tensor
    interpreter.set_tensor(input_index, input_tensor)

    #Run the inference
    interpreter.invoke()
    output_details = interpreter.get_output_details()

    # output data of image
    output_data = interpreter.get_tensor(output_details[0]['index'])

    pred = output_data.argmax()

    food_name = class_names[pred]

    return food_name

def fetch_recipe(food_name):
    url = "https://recipesapi2.p.rapidapi.com/recipes/"+food_name
    querystring = {"maxRecipes":"1"}

    headers = {
        'x-rapidapi-host': "recipesapi2.p.rapidapi.com",
        'x-rapidapi-key': "f6f6823b91msh9e92fed91d5356ap136f5djsn494d8f582fb3"
        }

    response = requests.request("GET", url, headers=headers, params=querystring)
    json_data = json.loads(response.text)

    recipe_data = json_data['data'][0]

    return recipe_data