赞
踩
车主或保险公司定损人员通过手机拍摄上传车辆损伤部位的外观图片,系统自动识别受损部件及损伤类型,快速在线定损,并可推荐引导至周边4S店/汽修店,显著提升小额案件的定损、理赔效率。本文主要介绍车辆外观损伤识别API的调用使用攻略。
一.平台接入
此步骤比较简单,不多阐述。可参照之前文档:
https://ai.baidu.com/forum/topic/show/943028
二.分析接口文档
https://ai.baidu.com/docs#/ImageClassify-API/043a3bd0
注意:邀测的接口,不能直接在控制台调用,可通过提交工单申请开通测试权限。
(1)接口描述
针对常见的小汽车车型,传入单帧图像,识别车辆外观受损部件及损伤类型,支持32种车辆部件、5大类外观损伤。同时可输出损伤的数值化结果(长宽、面积、部件占比),支持单图多种损伤的识别。
(2)请求说明
需要用到的信息有:
请求URL:https://aip.baidubce.com/rest/2.0/image-classify/v1/vehicle_damage
Header格式:Content-Type:application/x-www-form-urlencoded
(3)返回示例
{ "description":"Very good[车辆局部特写图][1.000000]", "damage_info":[ { "parts":前保险杠, "probability":89, "type":刮擦 }, { "parts":左前叶子板, "probability":74, "type":凹陷 } ] }
2.获取access_token
#client_id 为官网获取的AK, client_secret 为官网获取的SK
client_id =【百度云应用的AK】
client_secret =【百度云应用的SK】
#获取token
def get_token():
host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret
request = urllib.request.Request(host)
request.add_header('Content-Type', 'application/json; charset=UTF-8')
response = urllib.request.urlopen(request)
token_content = response.read()
if token_content:
token_info = json.loads(token_content.decode("utf-8"))
token_key = token_info['access_token']
return token_key
三.识别结果
返回结果:
{'log_id': 933845597687207145, 'result': {'damage_info': [{'numeric_info': [{'area': 10.720499992371, 'height': 6.8699998855591, 'ratio': 0.12322413921356, 'width': 2.0899999141693}], 'parts': '前保险杠', 'probability': 85, 'type': '刮擦'}, {'numeric_info': [{'area': 20.604499816895, 'height': 10.170000076294, 'ratio': 0.72043704986572, 'width': 3.0099999904633}], 'parts': '左前叶子板', 'probability': 65, 'type': '凹陷'}]}}
处理结果方面:可以看出,两处损伤识别很正确。
处理速度方面:处理时间4.30s,时间较长。
四.源码共享
# -*- coding: utf-8 -*- #!/usr/bin/env python import os import requests import base64 import json from pprint import pprint import time #client_id 为官网获取的AK, client_secret 为官网获取的SK api_key = '**********************' secret_key = '*****************************' class LandmarkRecognizer(object): def __init__(self, api_key, secret_key): self.access_token = self._get_access_token(api_key=api_key, secret_key=secret_key) self.API_URL = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/vehicle_damage' + '?access_token=' \ + self.access_token #获取token @staticmethod def _get_access_token(api_key, secret_key): api = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' \ '&client_id={}&client_secret={}'.format(api_key, secret_key) rp = requests.post(api) if rp.ok: rp_json = rp.json() # print(rp_json['access_token']) return rp_json['access_token'] else: print('=> Error in get access token!') def get_result(self, params): rp = requests.post(self.API_URL, data=params) if rp.ok: # print('=> Success! got result: ') rp_json = rp.json() pprint(rp_json) return rp_json else: print('=> Error! token invalid or network error!') print(rp.content) return None #车辆损伤识别 def detect(self, img_path): f = open(img_path, 'rb') strover = '识别结果:' img_str = base64.b64encode(f.read()) params = {'image': img_str} tic = time.clock() rp_json = self.get_result(params) toc = time.clock() print(strover) print('花费时长: '+'%.2f' %(toc - tic) +' s') if __name__ == '__main__': recognizer = LandmarkRecognizer(api_key, secret_key) img = 'F:\paddle\g2.jpg' recognizer.detect(img)
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。