欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

python用百度云接口实现车辆识别

程序员文章站 2022-05-22 10:19:02
...
""" 读取图片 """
def get_file_content(filePath):
    with open(filePath, 'rb') as fp:
        return fp.read()

image = get_file_content('example.jpg')

""" 调用车辆识别 """
client.carDetect(image);

""" 如果有可选参数 """
options = {}
options["top_num"] = 3
options["baike_num"] = 5

""" 带参数调用车辆识别 """
client.carDetect(image, options)

返回实例

{
  "log_id": 4086212218842203806,
  "location_result": {
    "width": 447,
    "top": 226,
    "height": 209,
    "left": 188
  },
  "result": [{
    "baike_info": {
      "baike_url": "http://baike.baidu.com/item/%E5%B8%83%E5%8A%A0%E8%BF%AAChiron/20419512",
      "description": "布加迪Chiron是法国跑车品牌布加迪出品的豪华超跑车。配置四涡轮增压发动机,420 公里每小时,有23种颜色的选择,售价高达260万美元。"
    },
    "score": 0.98793351650238,
    "name": "布加迪Chiron",
    "year": "无年份信息"
  },
  {
    "score": 0.0021970034576952,
    "name": "奥迪RS5",
    "year": "2011-2017"
  },
  {
    "score": 0.0021096928976476,
    "name": "奥迪RS4",
    "year": "无年份信息"
  },
  {
    "score": 0.0015581247862428,
    "name": "奥迪RS7",
    "year": "2014-2016"
  },
  {
    "score": 0.00082337751518935,
    "name": "布加迪威航",
    "year": "2004-2015"
  }],
  "color_result": "颜色无法识别"
}
代码

```python
from aip import AipImageClassify

# 定义常量
APP_ID = '22601565' #你百度帐号上的APP_ID
API_KEY = 'jXkY3Cv7VCIQGPvtdnY7X6aC' #你百度帐号上的API_KEY
SECRET_KEY = 'Cpk2bGGrp96UKYue0qNni4EiaGD7MwLj'#你百度帐号上的SECRET_KEY
# 初始化AipFace对象
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('C:/Users/lenovo/Pictures/paoche.jpg')
#将左侧括号内hua.jpg替换为待识别的图片路径

print(type(image))

result=client.carDetect(image);
for i in result['result']:
    print(i)

python用百度云接口实现车辆识别

{‘score’: 0.478742241859436, ‘name’: ‘兰博基尼盖拉多’, ‘year’: ‘2013-2014’}
{‘score’: 0.1481235921382904, ‘name’: ‘兰博基尼Aventador’, ‘year’: ‘2017’}
{‘score’: 0.117242768406868, ‘name’: ‘兰博基尼Veneno’, ‘year’: ‘2013-2014’}
{‘score’: 0.06717473268508911, ‘name’: ‘兰博基尼Centenario’, ‘year’: ‘2017’}
{‘score’: 0.0423521101474762, ‘name’: ‘兰博基尼Huracan’, ‘year’: ‘2016-2017’}

可加参数来改变输出信息的条数

options["top_num"] = 2
result=client.carDetect(image,options);