-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHelper.py
111 lines (87 loc) · 3.23 KB
/
Helper.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
import io
from google.cloud import vision
from nutritionix import Nutritionix
def get_labels_from_image(image):
"""
This function will allow the user to get the labels from an image_file.
input: file_name of the image
output: list of labels (.description or .score of individual label)
"""
vision_client = vision.Client()
image = vision_client.image(content=image)
labels = image.detect_labels()
return labels
def nutritionix_wrapper(label):
"""
This function will take a label and give out the weight to calories ratio
input: label (string)
output: weight / calories (double)
"""
nutritionix_id = nutritionix_ID(label)
nix = Nutritionix(app_id="8dfdbdb1", api_key="66ad2fcd0f25722ca73662505e9fd492")
nutritionix_info = nix.item(id=nutritionix_id).json()
weight_key = 'nf_serving_weight_grams'
calories_key = 'nf_calories'
weight = nutritionix_info[weight_key]
calories = nutritionix_info[calories_key]
if weight is not None and calories is not None:
print('ME GOOD')
ratio = calories / weight
return ratio
print('ME BAD')
return 0
def nutritionix_ID(label):
"""
This function will give the first product ID for a specific label
input: label (string)
output: ID (string)
"""
nix = Nutritionix(app_id="8dfdbdb1", api_key="66ad2fcd0f25722ca73662505e9fd492")
return nix.search(label, results="0:1").json()['hits'][0]['fields']['item_id']
def nutritionix_calories(nutritionix_id):
"""
This function will take an ID and return calories | Useful for the total count of calories per user
input: id (string)
output: calories
"""
nix = Nutritionix(app_id="8dfdbdb1", api_key="66ad2fcd0f25722ca73662505e9fd492")
calories_key = 'nf_calories'
nutritionix_info = nix.item(id=nutritionix_id).json()
calories = nutritionix_info[calories_key]
if calories is not None:
return calories
else:
return None
def health_score(**options):
"""
This function will return a health score based on multiple informations concerning the user
input: weigth, height, age, gender, total_calories_day (in DB)
output: health_score
"""
BMI = options['weight'] / options['height']
if BMI < 18.5:
BMI_weight = 1
elif BMI >= 18.5 and BMI < 25:
BMI_weight = 0
elif BMI >= 25 and BMI < 30:
BMI_weight = 1
elif BMI >= 30 and BMI < 40:
BMI_weight = 2
else:
BMI_weight = 3
if options['total_calories_day']:
calories = options['total_calories_day']
if options['gender'] == 'male':
BMR=66.47+ (13.75 * options['weight']) + (5.0 * options['height']) - (6.75 * options['age'])
elif options['gender'] == 'female':
BMR=65.09 + (9.56 * options['weight']) + (1.84 * options['height']) - (4.67 * options['age'])
ratio = BMR / calories
if ratio > 1:
return 80 - BMI_weight * 20 + 20 * (BMR / calories)
else:
return 60 - BMI_weight * 20 + 40 * (BMR / calories)
return 100 - BMI_weight * 20
if __name__ == '__main__':
file_name = 'img/Spaghetti.jpg'
labels = get_labels_from_image(file_name)
nutritionix_wrapper(labels[0].description)