当前位置:   article > 正文

快速接入百度大脑车辆外观损伤识别_车辆外观损伤识别模块代码

车辆外观损伤识别模块代码

车主或保险公司定损人员通过手机拍摄上传车辆损伤部位的外观图片,系统自动识别受损部件及损伤类型,快速在线定损,并可推荐引导至周边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)返回示例

   {

      "description":"Very good[车辆局部特写图][1.000000]",

      "damage_info":[

      {

        "parts":前保险杠,

        "probability":89,

        "type":刮擦

      },

      {

        "parts":左前叶子板,

        "probability":74,

        "type":凹陷

      }

      ]

}

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

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
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15

三.识别结果

在这里插入图片描述
返回结果:

 {'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': '凹陷'}]}}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

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

处理速度方面:处理时间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)
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/weixin_40725706/article/detail/374246
推荐阅读
相关标签
  

闽ICP备14008679号