当前位置:   article > 正文

python 百度ai json解析_百度AI识别调用的简单例子

python 百度ai json解析_百度AI识别调用的简单例子

一、车辆识别

1、安装百度ai的python SDK

使用到python调用百度ai的接口,官网下载相对应的SDK:https://ai.baidu.com/sdk#vis

解压,python setup.py install

[cc lang=”python”]

from aip import AipImageClassify

import json

APP_ID = ‘xxxxx’

API_KEY = ‘xxxxx’

SECRET_KEY = ‘xxxxx’

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(‘mycar.jpg’)

dict=client.carDetect(image)

print json.dumps(dict, ensure_ascii=False)

[/cc]

找一张汽车的图片,命名为mycar.jpg,例如下图:

百度ai接口返回的json如下,结果还算是准的,根据置信度有多个选择:

{

“log_id”: 7957146212839390462,

“location_result”: {

“width”: 823,

“top”: 105,

“left”: 112,

“height”: 379

},

“result”: [{

“score”: 0.99940371513367,

“name”: “本田雅阁”,

“year”: “2013-2017”

}, {

“score”: 1.8783097402775e-05,

“name”: “本田飞度”,

“year”: “2004-2016”

}, {

“score”: 1.5786132280482e-05,

“name”: “本田歌诗图”,

“year”: “2010-2016”

}, {

“score”: 1.4914214261808e-05,

“name”: “本田凌派”,

“year”: “2013-2017”

}, {

“score”: 1.0179050150327e-05,

“name”: “本田思铂睿”,

“year”: “2009-2017”

}],

“color_result”: “黑色”

}

3、“车辆识别”相关请求和反馈参数如下:

车辆识别 请求参数详情

参数名称

是否必选

类型

默认值

说明

image

string

图像数据,base64编码,要求base64编码后大小不超过4M,最短边至少15px,最长边最大4096px,支持jpg/png/bmp格式

top_num

string

返回预测得分top结果数,默认为5

baike_num

string

0

返回百科信息的结果数,默认不返回

车辆识别 返回数据参数详情

字段

是否必选

类型

说明

log_id

uint64

唯一的log id,用于问题定位

color_result

string

颜色

result

car-result()

车型识别结果数组

+name

string

车型名称,示例:宝马x6

+score

double

置信度,示例:0.5321

+year

string

年份

+baike_info

object

对应识别结果的百科词条名称

++baike_url

string

对应识别结果百度百科页面链接

++image_url

string

对应识别结果百科图片链接

++description

string

对应识别结果百科内容描述

location_result

string

车在图片中的位置信息

二、身份证识别

使用bash方式,通过api key、secret key获取access_token,用于调用百度api

curl -i -k ‘https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=XXXXX&client_secret=XXXXX‘

获取token信息如下,留意access_token那行,下一步会用到:

{

“refresh_token”: “”,

“expires_in”: 2592000,

“session_key”: “”,

“access_token”: “XXXXX”,

“scope”: “public vis-ocr_ocr vis-classify_dishes brain_ocr_scope vis-classify_car brain_ocr_general brain_ocr_general_basic vis-ocr_business_license brain_ocr_webimage brain_all_scope brain_ocr_idcard brain_ocr_driving_license brain_ocr_vehicle_license vis-ocr_plate_number vis-classify_animal vis-classify_plant brain_solution brain_ocr_plate_number brain_ocr_accurate brain_ocr_accurate_basic brain_ocr_receipt brain_ocr_business_license brain_object_detect brain_realtime_logo brain_dish_detect brain_car_detect brain_animal_classify brain_plant_classify brain_solution_iocr brain_ingredient brain_ocr_handwriting brain_ocr_passport brain_ocr_vat_invoice brain_advanced_general_classify brain_custom_dish brain_numbers brain_ocr_train_ticket brain_ocr_taxi_receipt brain_poi_recognize vis-ocr_\u8f66\u8f86vin\u7801\u8bc6\u522b vis-ocr_\u5b9a\u989d\u53d1\u7968\u8bc6\u522b brain_ocr_vin brain_ocr_quota_invoice wise_adapt lebo_resource_base lightservice_public hetu_basic lightcms_map_poi kaidian_kaidian ApsMisTest_Test\u6743\u9650 vis-classify_flower lpq_\u5f00\u653e cop_helloScope ApsMis_fangdi_permission smartapp_snsapi_base iop_autocar oauth_tp_app smartapp_smart_game_openapi oauth_sessionkey smartapp_swanid_verify smartapp_opensource_openapi smartapp_opensource_recapi”,

“session_secret”: “”

}

找一张身份证图片,命名为sfz1.jpg,读取身份证图片,填入上一步生成的access token,调用百度api:

[cc lang=”python”]

# coding:utf-8

import base64

import urllib, urllib2, sysaccess_token = ‘XXXXX‘

url = ‘https://aip.baidubce.com/rest/2.0/ocr/v1/idcard?access_token=’ + access_token

f = open(r’sfz1.jpg’, ‘rb’)

img = base64.b64encode(f.read())

params = {“image”: img, “id_card_side”: “front”}

params = urllib.urlencode(params)

request = urllib2.Request(url, params)

request.add_header(‘Content-Type’, ‘application/x-www-form-urlencoded’)

response = urllib2.urlopen(request)

content = response.read()

if (content):

print(content)

[/cc]

返回的json信息如下:

{

“log_id”: 112233,

“words_result_num”: 6,

“direction”: 0,

“image_status”: “normal”,

“words_result”: {

“住址”: {

“location”: {

“width”: 472,

“top”: 516,

“height”: 101,

“left”: 292

},

“words”: “广东省广州市广州塔顶楼”

},

“出生”: {

“location”: {

“width”: 314,

“top”: 421,

“height”: 40,

“left”: 291

},

“words”: “19990909”

},

“姓名”: {

“location”: {

“width”: 127,

“top”: 235,

“height”: 50,

“left”: 292

},

“words”: “张三”

},

“公民身份号码”: {

“location”: {

“width”: 613,

“top”: 744,

“height”: 42,

“left”: 475

},

“words”: “123456789000000000”

},

“性别”: {

“location”: {

“width”: 36,

“top”: 333,

“height”: 41,

“left”: 290

},

“words”: “男”

},

“民族”: {

“location”: {

“width”: 32,

“top”: 336,

“height”: 37,

“left”: 520

},

“words”: “汉”

}

}

}

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/菜鸟追梦旅行/article/detail/708901
推荐阅读
相关标签
  

闽ICP备14008679号