【攻略】百度车辆外观损伤识别API接口搞定在线定损功能

摘要

车主或保险公司定损人员通过手机拍摄上传车辆损伤部位的外观图片,系统自动识别受损部件及损伤类型,快速在线定损,并可推荐引导至周边4S店/汽修店,显著提升小额案件的定损、理赔效率......

作者 wangwei8638

车主或保险公司定损人员通过手机拍摄上传车辆损伤部位的外观图片,系统自动识别受损部件及损伤类型,快速在线定损,并可推荐引导至周边4S店/汽修店,显著提升小额案件的定损、理赔效率。本文主要介绍车辆外观损伤识别API的调用使用攻略。

一.平台接入

此步骤比较简单,不多阐述。可参照之前文档:

https://ai.baidu.com/forum/topic/show/943028

二.分析接口文档

1. 接口API

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)返回示例

  1.  {
  2.       "description":"Very good[车辆局部特写图][1.000000]", 
  3.  
  4.       "damage_info":[ 
  5.  
  6.       { 
  7.  
  8.         "parts":前保险杠, 
  9.  
  10.         "probability":89, 
  11.  
  12.         "type":刮擦 
  13.  
  14.       }, 
  15.  
  16.       { 
  17.  
  18.         "parts":左前叶子板, 
  19.  
  20.         "probability":74, 
  21.  
  22.         "type":凹陷 
  23.  
  24.       } 
  25.  
  26.       ] 
  27.  

2.获取access_token

  1. #client_id 为官网获取的AK, client_secret 为官网获取的SK 
  2. client_id =【百度云应用的AK】 
  3. client_secret =【百度云应用的SK】 
  4.  
  5. #获取token 
  6. def get_token(): 
  7. host = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=' + client_id + '&client_secret=' + client_secret 
  8. request = urllib.request.Request(host) 
  9. request.add_header('Content-Type', 'application/json; charset=UTF-8') 
  10. response = urllib.request.urlopen(request) 
  11. token_content = response.read() 
  12. if token_content: 
  13. token_info = json.loads(token_content.decode("utf-8")) 
  14. token_key = token_info['access_token'] 
  15. return token_key 

.识别结果

返回结果:

  1. {'log_id': 933845597687207145, 
  2. result': {'damage_info': [{'numeric_info': [{'area': 10.720499992371, 
  3.                                              'height': 6.8699998855591, 
  4.                                              'ratio': 0.12322413921356, 
  5.                                              'width': 2.0899999141693}], 
  6.                            'parts': '前保险杠', 
  7.                            'probability': 85, 
  8.                            'type': '刮擦'}, 
  9.                           {'numeric_info': [{'area': 20.604499816895, 
  10.                                              'height': 10.170000076294, 
  11.                                              'ratio': 0.72043704986572, 
  12.                                              'width': 3.0099999904633}], 
  13.                            'parts': '左前叶子板', 
  14.                            'probability': 65, 
  15.                            'type': '凹陷'}]}} 

处理结果方面:可以看出,两处损伤识别很正确。

处理速度方面:处理时间4.30s,时间较长。

四.源码共享

  1.  # -*- coding: utf-8 -*- 
  2. #!/usr/bin/env python 
  3.  
  4.  
  5.  
  6. import os 
  7.  
  8. import requests 
  9.  
  10. import base64 
  11.  
  12. import json 
  13.  
  14. from pprint import pprint 
  15.  
  16. import time 
  17.  
  18. #client_id 为官网获取的AK, client_secret 为官网获取的SK 
  19.  
  20. api_key = '**********************' 
  21.  
  22. secret_key = '*****************************' 
  23.  
  24.  
  25.  
  26. class LandmarkRecognizer(object): 
  27.  
  28.     def __init__(self, api_key, secret_key): 
  29.  
  30.         selfself.access_token = self._get_access_token(api_keyapi_key=api_key, secret_keysecret_key=secret_key) 
  31.  
  32.         self.API_URL = 'https://aip.baidubce.com/rest/2.0/image-classify/v1/vehicle_damage' + '?access_token=' \ 
  33.  
  34.                       + self.access_token 
  35.  
  36.     #获取token 
  37.  
  38.     @staticmethod 
  39.  
  40.     def _get_access_token(api_key, secret_key): 
  41.  
  42.         api = 'https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials' \ 
  43.  
  44.             '&client_id={}&client_secret={}'.format(api_key, secret_key) 
  45.  
  46.         rp = requests.post(api) 
  47.  
  48.         if rp.ok: 
  49.  
  50.             rprp_json = rp.json() 
  51.  
  52. #            print(rp_json['access_token']) 
  53.  
  54.             return rp_json['access_token'] 
  55.  
  56.         else: 
  57.  
  58.             print('=> Error in get access token!') 
  59.  
  60.     def get_result(self, params): 
  61.  
  62.         rp = requests.post(self.API_URL, data=params) 
  63.  
  64.         if rp.ok: 
  65.  
  66. #            print('=> Success! got result: ') 
  67.  
  68.             rprp_json = rp.json() 
  69.  
  70.             pprint(rp_json) 
  71.  
  72.             return rp_json 
  73.  
  74.         else: 
  75.  
  76.             print('=> Error! token invalid or network error!') 
  77.  
  78.             print(rp.content) 
  79.  
  80.             return None 
  81.  
  82.     #车辆损伤识别 
  83.  
  84.     def detect(self, img_path): 
  85.  
  86.         f = open(img_path, 'rb') 
  87.  
  88.         strover = '识别结果:' 
  89.  
  90.         img_str = base64.b64encode(f.read()) 
  91.  
  92.         params = {'image': img_str} 
  93.  
  94.         tic = time.clock() 
  95.  
  96.         rp_json = self.get_result(params) 
  97.  
  98.         toc = time.clock() 
  99.  
  100.         print(strover) 
  101.  
  102.         print('花费时长: '+'%.2f'  %(toc - tic) +' s') 
  103.  
  104.  
  105.  
  106. if __name__ == '__main__': 
  107.  
  108.     recognizer = LandmarkRecognizer(api_key, secret_key) 
  109.  
  110.     img = 'F:\paddle\g2.jpg' 
  111.  
  112.     recognizer.detect(img) 

最新文章

极客公园

用极客视角,追踪你不可错过的科技圈.

极客之选

新鲜、有趣的硬件产品,第一时间为你呈现。

张鹏科技商业观察

聊科技,谈商业。