forked from CleanPegasus/healthy_ly
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_cloud.py
177 lines (106 loc) · 3.56 KB
/
main_cloud.py
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
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
from flask import Flask
import cv2
import numpy as np
import tensorflow as tf
#from tensorflow.keras.models import load_model
import requests
import json
from firebase import Firebase
from PIL import Image
import urllib
model = tf.keras.models.load_model('new_model.h5')
food_category = np.load('food_category.npy')
print(food_category.shape)
config = {
"apiKey": "AIzaSyDQ1lcaiMTXR3Dmey9VY_gFJK2mt05MMds",
"authDomain": "recipify-c54b7.firebaseapp.com",
"databaseURL": "https://recipify-c54b7.firebaseio.com",
"storageBucket": "recipify-c54b7.appspot.com"
}
firebase = Firebase(config)
def get_image():
db = firebase.database()
users = db.child("url").get()
url = users.val()
print(url)
#response = requests.get(url)
#print(BytesIO(response.content))
#img = Image.open(urllib.request.urlopen(url))
resp = urllib.request.urlopen(url)
img = np.asarray(bytearray(resp.read()), dtype = "uint8")
img = cv2.imdecode(img, cv2.IMREAD_COLOR)
img = img[483:3357, 2:2158]
img = cv2.resize(img, (128, 128))
image = np.asarray(img)/255
image = np.reshape(image, [-1, 128, 128, 3])
return image
def predict():
food_image = get_image()
food = food_category[model.predict(food_image).argmax()]
food = food.replace("_", " ")
return food
def get_food_nutrients(food):
url = "https://nutritionix-api.p.rapidapi.com/v1_1/search/{}".format(food)
querystring = {"fields": "item_name,nf_calories,nf_total_fat"}
headers = {
'x-rapidapi-host': "nutritionix-api.p.rapidapi.com",
'x-rapidapi-key': "e4573ac34fmshc71719554be1369p1d0fcajsnc45c98c12e74"
}
response = requests.request("GET", url, headers=headers, params=querystring)
# print(response.text)
output = response.json()
calories = []
fat = []
hits = output["hits"]
calories = hits[0]["fields"]["nf_calories"]
fat = hits[0]["fields"]["nf_total_fat"]
nutrients = {"calories": calories,
"fat": fat}
# nutrients = json.dumps(res)
return nutrients
def get_food_details(food):
url = "https://recipe-puppy.p.rapidapi.com/"
headers = {
'x-rapidapi-host': "recipe-puppy.p.rapidapi.com",
'x-rapidapi-key': "e4573ac34fmshc71719554be1369p1d0fcajsnc45c98c12e74"
}
querystring = {"q": str(food)}
response = requests.request("GET", url, headers=headers, params=querystring)
output = response.json()
# print(output['results'])
ingredients = []
names = []
recepies = []
for food in output['results']:
content = food['ingredients'].split(',')
names.append(food['title'])
recepies.append(food['href'])
ingredients = ingredients + content
# res = dict(zip(names, recepies))
# print(str(res))
food_recipe = {"names": names,
"recepies": recepies,
"ingredients": ingredients}
return food_recipe
def get_json(food):
#food_recipe = get_food_details(food)
#print(food_recipe)
nutrients = get_food_nutrients(food)
#print(nutrients)
output = {"prediction" : food, **nutrients}
#print(total_dict)
#output = json.dumps(total_dict)
return output
def send_data():
food = predict()
output = get_json(food)
db = firebase.database()
db.child("result/two").set(output)
return output
while(True):
db = firebase.database()
check_val = db.child("check").get().val()
if(check_val == 1):
output = send_data()
print(output)
db.child("check").set(0)