【行业方案】【图像识别】健康饮食APP

1.需求及方案:

随着生活水平的提高,人们都希望能健康饮食,这就需要对食材有更加深入的了解。百度AI果蔬类食材识别,根据拍摄照片,识别图片中果蔬名称。本文结合百度AI果蔬类食材识别与百度百科,食物卡路里信息整合,为用户提供营养价值、搭配禁忌等方面信息,让大家可以更加健康的饮食。

2.平台接入

食材识别接入网址:https://console.bce.baidu.com/ai/#/ai/imagerecognition/overview/index

具体接入方式比较简单,可以参考我的另一个帖子,这里就不重复了:

3.食材识别调用攻略(Python3)

3.1首先认证授权:

在开始调用任何API之前需要先进行认证授权,具体的说明请参考:

#/Auth/top

具体Python3代码如下:

# -*- coding: utf-8 -*- #!/usr/bin/env python import urllib import base64 import json #client_id 为官网获取的AK, client_secret 为官网获取的SK client_id =【百度云应用的AK】 client_secret =【百度云应用的SK】 #获取token def get_token(): host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret request = urllib.request.Request(host) request.add_header('Content-Type', 'application/json; charset=UTF-8') response = urllib.request.urlopen(request) token_content = response.read() if token_content: token_info = json.loads(token_content) token_key = token_info['access_token'] return token_key

3.2 接口调用:
详细说明请参考:https://ai.baidu.com/docs#/ImageClassify-API/f0fe4219

说明的比较清晰,这里就不重复了。

大家需要注意的是:
API访问URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient
图片格式:Base64编码字符串,以图片文件形式请求时必填。(支持图片格式:jpg,bmp,png),图片大小不超过4M。最短边至少15px,最长边最大4096px。 注意:图片的base64编码是不包含图片头的

Python3调用代码如下:

#食材识别 #filename:图片名(本地存储包括路径) def landmark(filename): request_url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient" # 二进制方式打开图片文件 f = open(filename, 'rb') img = base64.b64encode(f.read()) params = dict() params['image'] = img params['show'] = 'true' params = urllib.parse.urlencode(params).encode("utf-8") #params = json.dumps(params).encode('utf-8') access_token = get_token() request_url = request_url + "?access_token=" + access_token request = urllib.request.Request(url=request_url, data=params) request.add_header('Content-Type', 'application/x-www-form-urlencoded') response = urllib.request.urlopen(request) content = response.read() landmark='' if content: #print(content) content=content.decode('utf-8') #print(content) data = json.loads(content) #print(data) words_result=data['result'] landmark=words_result['landmark'] print ('食材:',landmark) return landmark

4.应用方案:

4.1 整体方案:
首先调用食材识别API对食材图片进行识别。
然后通过百度百科对食材的具体情况进行搜索,并搜集食物卡路里信息,将结果展示给客户。整体方案如下图所示:

【行业方案】【图像识别】健康饮食APP

4.2 核心代码:

API调用:

# -*- coding: utf-8 -*- #!/usr/bin/env python import urllib import base64 import json #client_id 为官网获取的AK, client_secret 为官网获取的SK client_id =【百度云应用的AK】 client_secret =【百度云应用的SK】 #获取token def get_token(): host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret request = urllib.request.Request(host) request.add_header('Content-Type', 'application/json; charset=UTF-8') response = urllib.request.urlopen(request) token_content = response.read() if token_content: token_info = json.loads(token_content) token_key = token_info['access_token'] return token_key def ingredient(filename): request_url = " https://aip.baidubce.com/rest/2.0/image-classify/v1/classify/ingredient" # 二进制方式打开图片文件 f = open(filename, 'rb') img = base64.b64encode(f.read()) params = dict() params['image'] = img params['show'] = 'true' params = urllib.parse.urlencode(params).encode("utf-8") #params = json.dumps(params).encode('utf-8') access_token = get_token() request_url = request_url + "?access_token=" + access_token request = urllib.request.Request(url=request_url, data=params) request.add_header('Content-Type', 'application/x-www-form-urlencoded') response = urllib.request.urlopen(request) content = response.read() ingredient='' if content: #print(content) content=content.decode('utf-8') #print(content) data = json.loads(content) #print(data) words_result=data['result'][0] ingredient=words_result['name'] print (ingredient) return ingredient

您可能还会对下面的相关信息感兴趣: