本文介绍了一种使用百度智能云图像识别接口的方法,该接口具有多种功能,包括识别菜品、Logo、果蔬等。如果你对如何使用百度接口感兴趣,可以参考我在其他文章中的详细介绍。
```python
import requests import base64 import pprint
def getaccesstoken(): host = 'https://aip.baidubce.com/oauth/2.0/token?granttype=clientcredentials&clientid=【官网获取的AK】&clientsecret=【官网获取的SK】' response = requests.get(host) if response: return response.json()['access_token']
def advancedgeneral(imgpath): requesturl = "https://aip.baidubce.com/rest/2.0/image-classify/v2/advancedgeneral" with open(imgpath, 'rb') as f: img = base64.b64encode(f.read()) params = {"image": img} accesstoken = getaccesstoken() requesturl += "?accesstoken=" + accesstoken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(requesturl, data=params, headers=headers) if response: pprint.pprint(response.json())
def objectdetection(imgpath): requesturl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/objectdetect" with open(imgpath, 'rb') as f: img = base64.b64encode(f.read()) params = {"image": img, "withface": 1} accesstoken = getaccesstoken() requesturl += "?accesstoken=" + accesstoken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=params, headers=headers) if response: print(response.json())
def animalrecognition(imgpath): requesturl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/animal" with open(imgpath, 'rb') as f: img = base64.b64encode(f.read()) params = {"image": img} accesstoken = getaccesstoken() requesturl += "?accesstoken=" + accesstoken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=params, headers=headers) if response: print(response.json())
def plantrecognition(imgpath): requesturl = "https://aip.baidubce.com/rest/2.0/image-classify/v1/plant" with open(imgpath, 'rb') as f: img = base64.b64encode(f.read()) params = {"image": img} accesstoken = getaccesstoken() requesturl += "?accesstoken=" + accesstoken headers = {'content-type': 'application/x-www-form-urlencoded'} response = requests.post(request_url, data=params, headers=headers) if response: print(response.json())
if name == 'main': imgpath = 'u=2580636683,3144235846&fm=26&gp=0.jpg' # 1. 通用物体和场景识别 # advancedgeneral('111.jpg') # 2. 图像主体检测 # objectdetection(imgpath) # 3. 动物识别 # animalrecognition(imgpath) # 4. 植物识别 plantrecognition(imgpath) ```
运行结果:
json
{
"log_id": 470629798833798022,
"result": [
{
"keyword": "北京天安门",
"root": "建筑-传统建筑",
"score": 0.943181
},
{
"keyword": "城楼",
"root": "建筑-传统建筑",
"score": 0.75112
},
{
"keyword": "都市夜景",
"root": "建筑-建筑夜景",
"score": 0.414575
},
{
"keyword": "街道",
"root": "建筑-其他",
"score": 0.219592
},
{
"keyword": "轿车",
"root": "交通工具-汽车",
"score": 0.011774
}
],
"result_num": 5
}
运行结果:
json
{
"log_id": 6487470709418459622,
"result": {
"width": 775,
"top": 17,
"left": 3,
"height": 278
}
}
运行结果:
json
{
"log_id": 1653180371969692999,
"result": [
{
"name": "松鼠",
"score": "0.98465"
},
{
"name": "岩松鼠",
"score": "0.00457777"
},
{
"name": "灰鼠",
"score": "0.00147658"
},
{
"name": "金花鼠",
"score": "0.000845074"
},
{
"name": "花栗鼠",
"score": "0.000815643"
},
{
"name": "赤腹松鼠",
"score": "0.000759463"
}
]
}
json
{
"log_id": 5028727426341370887,
"result": [
{
"name": "非洲象",
"score": "0.962111"
},
{
"name": "亚洲象",
"score": "0.0314206"
},
{
"name": "野象",
"score": "0.00213204"
},
{
"name": "猛犸象",
"score": "6.82637e-05"
},
{
"name": "水牛",
"score": "2.94292e-05"
},
{
"name": "狮子",
"score": "2.70095e-05"
}
]
}
运行结果:
json
{
"log_id": 6306910600402241223,
"result": [
{
"name": "荷花玉兰",
"score": 0.7407493591308594
},
{
"name": "玉兰",
"score": 0.11518511176109314
},
{
"name": "山玉兰",
"score": 0.031238427385687828
}
]
}
希望以上内容对你有所帮助。