利用百度云API接口上传照片进行植物识别

第一步还是引库,确定自己安装过这些库了,没有安装的需要自行安装import requestsimport base64import os#os库是下面多张图片进行识别的,如果一次识别一张图片就不需要用os库接下来看代码def A_Token(access_url): '''获取access_token''' pa = {"grant_type": "client_credentials", # 固定值 "client_id": "*******.
摘要由CSDN通过智能技术生成

第一步还是引库,确定自己安装过这些库了,没有安装的需要自行安装

import requests
import base64
import os

#os库是下面多张图片进行识别的,如果一次识别一张图片就不需要用os库

 接下来看代码

def A_Token(access_url):
    '''获取access_token'''
    pa = {"grant_type": "client_credentials",  # 固定值
          "client_id": "*******************",  # 找到自己的API Key
          "client_secret": "***************"}  # 找到自己的Secret Key
    re = requests.get(access_url, params=pa).json()
    # if response:
    # print(response.json())
    atoken = re.get("access_token")  # 提取access_token的值
    return atoken

def getHtml(url, img):
    '''请求植物识别API接口'''
    hd = {'content-type': 'application/x-www-form-urlencoded'}
    par = {"image": img}
    response = requests.post(url, data=par, headers=hd)
    if response:
        response.json()
    plant = response.json()
    print(plant)
    print("植物的可能性从大到小为:", plant["result"][0]["name"],end=",")
    print(plant["result"][1]["name"],end=",")
    print(plant["result"][2]["name"])

access_url = "https://aip.baidubce.com/oauth/2.0/token"
url = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant"
accesstoken = A_Token(access_url)
'''获取access_token'''的步骤

获取access_token:首先需要自己去百度云创建一个账号,点开右上角的管理控制台,进去之后把鼠标放在产品服务上
  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
利用百度云 API 进行手写文字图片识别,可以按照以下步骤进行操作: 1. 首先,需要到百度云控制台注册账号并创建一个应用,获取到 API Key 和 Secret Key。 2. 安装 `baidu-aip` 库,这个库是百度云提供的官方 Python SDK,可以使用以下命令安装: ``` pip install baidu-aip ``` 3. 在 Python 代码中导入 `AipOcr` 类,创建一个实例,并设置 API Key 和 Secret Key: ```python from aip import AipOcr APP_ID = 'your app id' API_KEY = 'your api key' SECRET_KEY = 'your secret key' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) ``` 4. 加载要识别的图片: ```python with open('image.jpg', 'rb') as f: image = f.read() ``` 5. 调用 `client.basicAccurate` 方法进行文字识别: ```python result = client.basicAccurate(image) ``` 6. 解析返回的结果,可以得到识别出来的文字: ```python words = [] for item in result['words_result']: words.append(item['words']) print(words) ``` 完整的代码示例: ```python from aip import AipOcr APP_ID = 'your app id' API_KEY = 'your api key' SECRET_KEY = 'your secret key' client = AipOcr(APP_ID, API_KEY, SECRET_KEY) with open('image.jpg', 'rb') as f: image = f.read() result = client.basicAccurate(image) words = [] for item in result['words_result']: words.append(item['words']) print(words) ``` 需要注意的是,百度云 API 的免费额度是有限的,如果需要进行大量的手写文字图片识别,需要付费购买更多的调用次数。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值