用python实现菜品的图片识别

用Python实现对菜品的图片识别

最近做项目需要对菜品进行图片识别功能,于是便自己动手做了一个小test

项目需要的东西(需要自己去申请自己的 APPID , AK , SK

APP_ID = 'xxxx'
API_KEY = 'xxxxxx'
SECRET_KEY = 'xxxxxxx'

读取图片代码:

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('这里填自己图片地址')

调用菜品识别:

client.dishDetect(image)

如果有可选参数的话, 带参数调用菜品识别:

""" 如果有可选参数 """
options = {}
options["top_num"] = 10
options["filter_threshold"] = "0.7"
options["baike_num"] = 1
""" 带参数调用菜品识别 """
res = client.dishDetect(image, options)
for k in res:
    print("%s->%s"%(k,res[k]))

完整代码:

from aip import AipImageClassify

""" 你的 APPID AK SK """
APP_ID = '你的APPID'
API_KEY = '你的AK'
SECRET_KEY = '你的SK'

client = AipImageClassify(APP_ID, API_KEY, SECRET_KEY)

""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('你的图片地址')


""" 调用菜品识别 """
client.dishDetect(image)

""" 如果有可选参数 """
options = {}
options["top_num"] = 10
options["filter_threshold"] = "0.7"
options["baike_num"] = 1
""" 带参数调用菜品识别 """
res = client.dishDetect(image, options)
for k in res:
    print("%s->%s"%(k,res[k]))

实例图片:
测试图片
测试结果:
测试结果

PS:如果你申请的百度智能云API,记得一定要先免费领取测试额度!
操作链接:点此处加载

  • 4
    点赞
  • 13
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要使用百度云API实现菜品识别,您需要遵循以下步骤: 1. 首先,您需要在百度云平台上注册并创建一个新的应用程序,以获取您的API密钥和密钥。在创建应用程序时,请确保启用了“菜品识别”API。 2. 安装必要的Python库,如requests和json等。您可以使用以下命令来安装这些库: ``` pip install requests pip install json ``` 3. 接下来,您需要编写Python代码来调用API并进行菜品识别。以下是一个基本的示例代码: ```python import requests import json # Enter your API Key and Secret Key here API_KEY = 'your_api_key' SECRET_KEY = 'your_secret_key' # Upload your image to Baidu Cloud and get the image URL image_url = 'http://your_image_url' # Define the request URL url = 'https://aip.baidubce.com/rest/2.0/image-classify/v2/dish' # Define the request headers headers = { 'Content-Type': 'application/x-www-form-urlencoded' } # Define the request parameters params = { 'access_token': API_KEY, 'image': image_url, 'top_num': 5 } # Send the request to the API response = requests.post(url, data=params, headers=headers) # Parse the response JSON data data = json.loads(response.text) # Print the top 5 dishes for item in data['result']: print(item['name'], item['probability']) ``` 在上面的代码中,您需要将您的API密钥和密钥替换为“your_api_key”和“your_secret_key”,并将您要识别菜品图像的URL替换为“your_image_url”。 4. 运行代码并查看输出结果。输出将包括前5个最可能的菜品及其概率。 请注意,该API对菜品识别准确性取决于您提供的图像质量和菜品的种类。如果您得到不准确的结果,请尝试使用更高质量的图像或更具有代表性的菜品图像。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值