体重保持是现代人需要生活关注的一项事情。我们用Python实现体重管理软件,并根据你吸收和消耗的卡路里的余值进行餐食推荐。
一天损失的卡路里净值的计算公式为:
损失卡路里 = 自身基础消耗 + 运动锻炼消耗 - (早餐吸收卡路里 + 午餐吸收卡路里 + 晚餐吸收卡路里)。
一周卡路里为每天卡路里乘以7,于是一周内你能减重的公式为
一周损失卡路里/3600*0.45359237
然后我们根据减重目标和这些公式反推你每天的饮食吸收近来的卡路里上限为:
每天三餐的吸收卡路里上限 = 自身基础消耗bmr + 运动锻炼消耗 - ((每周减重目标kg *3600/0.45359237)/ 7天)
我们根据算出来的三餐卡路里上限给出饮食建议。
这里调用了https://spoonacular.com/food-api/console#Dashboard 的API,需要你先注册个账号。然后这里有一个APP key,先记下来
通过connect进行用户登记。这个API的目的是绑定用户到你注册的appid里。记得在请求头里面加上你的app key。
headers={"x-api-key":""}
然后记住这个hash,以后的api都使用hash识别为是这个用户的请求。可以针对这个用户进行客制化饮食推荐。
接着我们调用mealplanner接口获取三餐推荐。这里需要用到三餐总卡路里和请求头app id使用get请求方法获得。
于是API给我们返回的三餐推荐是:
这里我们使用腾讯云的大模型图像创作引擎->文生图模型来生成红烧肉以及配料图片。
大模型文生图的API入口在这里:https://console.cloud.tencent.com/api/explorer?Product=aiart&Version=2022-12-29&Action=TextToImage
这里使用官方提供的Python API指引。然后在这里申请https://console.cloud.tencent.com/cam/capi,得到APP key和密钥。
import requests
class WeightLoss:
def __init__(self, exercise, bmr):
self.exercise=exercise
self.bmr=bmr
def calories_deficit(self):
deficit=self.bmr+self.exercise-(self.breakfast_calories+self.lunch_calories+dinner_calories)
return deficit
def targe_meals_calories(self, target_weight_loss):
meals_calories = self.bmr + self.exercise - ((target_weight_loss *3600/0.45359237)/7)
return meals_calories
#exercise=int(input("how many calories did you burn exercising?"))
#bmr=int(input("What is your basic metabolic rate"))
exercise = 500 #calories
bmr = 1000 #calories
target_weight_loss=0.5 #kg
fitness=WeightLoss(exercise, bmr)
meals_calories=int(fitness.targe_meals_calories(target_weight_loss))
if meals_calories > 0:
print(f"You meal calories limit is {meals_calories} calories per day")
##登记用户
#user_data="""
#{
# "username": "mariolu",
# "firstName": "mario",
# "lastName": "lu",
# "email": "lumanyu@gmail.com"
#}
#"""
#headers={"x-api-key":""}
#connect_url="https://api.spoonacular.com/users/connect"
#response = requests.post(url, headers=headers, json=json.loads(user_data))
#response_json = response.json()
#hash = response_json['hash']
##三餐推荐
#url=f"https://api.spoonacular.com/mealplanner/generate?timeFrame=day&targetCalories={target_calories}"
#response = requests.get(url, headers=headers)
#response_json = response.json()
# 这里为了保证在线运行结果,我把我拉取的食谱直接放在这个快照链接里
url = "https://raw.githubusercontent.com/lumanyu/ai_app/main/data/mealplanner/mealpanner.json"
# A GET request to the API
response = requests.get(url)
# 获取到食谱的组成成份
response_json = response.json()
print(f"早餐: {response_json['meals'][0]['servings']} {response_json['meals'][0]['title']}")
print(f"午餐: {response_json['meals'][1]['servings']} {response_json['meals'][1]['title']}")
print(f"晚餐: {response_json['meals'][2]['servings']} {response_json['meals'][2]['title']}")
import sys
import json
import matplotlib
import matplotlib.pyplot as plt
import base64
import matplotlib.image as mpimg
import io
from tencentcloud.common import credential
from tencentcloud.common.profile.client_profile import ClientProfile
from tencentcloud.common.profile.http_profile import HttpProfile
from tencentcloud.common.exception.tencent_cloud_sdk_exception import TencentCloudSDKException
from tencentcloud.aiart.v20221229 import aiart_client, models
import urllib
matplotlib.use('Agg')
"""
try:
# 实例化一个认证对象,入参需要传入腾讯云账户 SecretId 和 SecretKey,此处还需注意密钥对的保密
# 代码泄露可能会导致 SecretId 和 SecretKey 泄露,并威胁账号下所有资源的安全性。以下代码示例仅供参考,建议采用更安全的方式来使用密钥,请参见:https://cloud.tencent.com/document/product/1278/85305
# 密钥可前往官网控制台 https://console.cloud.tencent.com/cam/capi 进行获取
cred = credential.Credential("", "")
# 实例化一个http选项,可选的,没有特殊需求可以跳过
httpProfile = HttpProfile()
httpProfile.endpoint = "aiart.tencentcloudapi.com"
# 实例化一个client选项,可选的,没有特殊需求可以跳过
clientProfile = ClientProfile()
clientProfile.httpProfile = httpProfile
# 实例化要请求产品的client对象,clientProfile是可选的
client = aiart_client.AiartClient(cred, "ap-guangzhou", clientProfile)
# 实例化一个请求对象,每个接口都会对应一个request对象
req = models.TextToImageRequest()
params = {
"Prompt": "Watermelon, Kiwi, Apple and Frozen Banana Smoothie"
}
req.from_json_string(json.dumps(params))
# 返回的resp是一个TextToImageResponse的实例,与请求对象对应
resp = client.TextToImage(req)
# 输出json格式的字符串回包
image_data = json.loads(resp.to_json_string())
global img_data
img_data=image_data['ResultImage']
fig = plt.figure()
fp = io.BytesIO(base64.b64decode(str(img_data)))
with fp:
img = mpimg.imread(fp, format='jpeg')
plt.imshow(img)
fig.savefig('plot.png', dpi=80)
except TencentCloudSDKException as err:
print(err)
"""
#因为这里生成早餐图片要填上secret id和key,所以需要你自己注册,代码是可用的。这里我直接拉取之前拉取过的图片做展示
url="https://raw.githubusercontent.com/lumanyu/ai_app/main/data/mealplanner/breakfast.mini.png"
response = requests.get(url)
fp = io.BytesIO(response.content)
fig = plt.figure()
with fp:
img = mpimg.imread(fp, format='')
plt.imshow(img)
fig.savefig('plot2.png', dpi=80)
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。
原创声明:本文系作者授权腾讯云开发者社区发表,未经许可,不得转载。
如有侵权,请联系 cloudcommunity@tencent.com 删除。