阅读(4953) (7)

百度智能小程序 检测图片的具体车型

2020-08-13 15:59:37 更新

swan.ai.carClassify

解释 :用于检测一张车辆图片的具体车型,即对于输入的一张图片(可正常解码,且长宽比适宜),输出图片的车辆品牌及型号、颜色及年份、位置信息。

方法参数

Object object

object 参数说明

属性名 类型 必填 默认值 说明

image

String

-

图像资源地址

top_num

Number

5

返回结果 top n,默认 5 。

success

Function

接口调用成功的回调函数

fail

Function

接口调用失败的回调函数

complete

Function

接口调用结束的回调函数(调用成功、失败都会执行)

success 返回参数说明

参数 类型 说明

log_id

Number

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

color_result

string

颜色

result

Array

车型识别结果数组

location_result

Object

车在图片中的位置信息

result 返回值说明

参数名 参数类型 说明

name

String

车型名称,示例:宝马 x6 。

score

Number

置信度,示例:0.5321 。

year

String

年份

location_result 返回值说明

参数名 参数类型 说明

left

Number

左起像素位置

top

Number

上起像素位置

width

Number

像素宽

height

Number

像素高

示例 

在开发者工具中打开

代码示例

<view class="wrap">
    <view class="card-area" style="height:2.1rem">
        <image mode="aspectFill" class="display-area-image" src="{{image}}"></image>
    </view>
    <view class="card-area" style="margin-bottom: 1.2rem;">
        <view s-if="!loading" class="top-description border-bottom">识别结果</view>
        <view s-if="result.length>0">
            <view s-if="!loading">
                <view s-for="item in result" class="list-area border-bottom">
                    <view class="reasult-keyword">{{item.name}}</view>
                    <view class="reasult-score">
                        <view class="score-title">置信度</view>
                        <view style="display:inline-block">
                            <view class="score-info">{{item.score}}<text class="percent">%</text></view>
                            <view class="score-percent">
                                <view class="percent-front {{item.score===100?'percent-100':''}}" style="width:{{item.score}}%;"></view>
                            </view>
                        </view>        
                    </view>     
                    <view class="score-title">年份</view>
                    <view class="root-info">{{item.year}}</view>     
                </view>
            </view>
        </view>
        <view s-else>
            <view s-if="!loading">
                <view class="result-area">
                    <view class="result-area-fail">未检测到车辆</view>
                    <view class="result-area-tips">请上传包含车辆的清晰图片</view>
                </view>
            </view>
        </view>
    </view>
     <view class="swan-security-padding-bottom flex-button">
        <button type="primary" hover-class="button-hover" disabled="{{loading}}" bindtap="carClassify">上传车辆图片</button>
    </view>
</view>
Page({
    data: {
        loading: true,
        isLoad: false,
        image: 'https://www.programbbs.com/pic/baiduapp/carClassify.png',
        result: [
            {score: '10.67', name: '日产BladeGlider', year: '无年份信息'},
            {score: '9.05', name: '标致VisionGranTurismo', year: '无年份信息'},
            {score: '7.54', name: '摩根EV3', year: '2015'},
            {score: '7.18', name: 'LM55', year: '无年份信息'},
            {score: '6.96', name: '奔驰Vision', year: '无年份信息'}
        ]
    },
    onShow() {
        if (!this.data.isLoad) {
            swan.showToast({
                title: '识别中...',
                icon: 'loading',
                duration: 1000
            });
            this.setData({
                loading: true
            });

            setTimeout(() => {
                this.setData({
                    loading: false
                });
                this.data.isLoad = true;
            }, 1000);
        }
    },
    carClassify() {
        if (this.getData('loading')) {
            return;
        }
        swan.showActionSheet({
            itemList: ['拍照', '从手机相册选择'],
            success: res => {
                const sourceType = res.tapIndex === 0 ? 'camera' : 'album';
                swan.chooseImage({
                    count: 1,
                    sizeType: ['original', 'compressed'],
                    sourceType: [sourceType],
                    success: res => {
                        const image = res.tempFilePaths[0];
                        this.setData({image, loading: true});
                        swan.showToast({
                            title: '识别中...',
                            icon: 'loading',
                            duration: 20000
                        });
                        swan.ai.carClassify({
                            image,
                            top_num: 5,
                            success: res => {
                                console.log('carClassify success', res);
                                this.setImageData(res.result);
                            },
                            fail: err => {
                                console.log('carClassify fail', err);
                            },
                            complete: () => {
                                swan.hideToast();
                                this.setData('loading', false);
                            }
                        });
                    },
                    fail: err => {
                        console.log('chooseImage fail', err);
                    }
                });
            }
        });
    },
    setImageData(data) {
        const len = data.length - 1;
        for (let i = len; i >= 0; i--) {
            if (data[i].name === '非车类') {
                data.splice(i, 1);
            } 
            else {
                if (data[i].score && !isNaN(data[i].score)) {
                    data[i].score = (data[i].score * 100).toFixed(2);
                    if (data[i].score > 100) {
                        data[i].score = 100;
                    }
                }
                else {
                    data[i].score = 0;
                }
            }
        }
        this.setData('result', data);
    }
});

返回值示例

{
    "log_id": $log_id,
    "location_result": {
        "width": 434,
        "top": 119,
        "height": 246,
        "left": 110
    },
    "result": [
        {
            "score": 0.99993008375168,
            "name": "宝马X3",
            "year": "2016"
        },
        {
            "score": 0.00005255633732304,
            "name": "宝马X4",
            "year": "2015-2016"
        },
        {
            "score": 0.0000031432850846613,
            "name": "SWM斯威X7",
            "year": "2016-2017"
        },
        {
            "score": 0.0000012879694395451,
            "name": "宝马X5",
            "year": "2016-2017"
        },
        {
            "score": 5.6126202707674e-7,
            "name": "丰田汉兰达",
            "year": "2015-2017"
        },
        {
            "score": 4.4069730620322e-7,
            "name": "斯柯达速尊",
            "year": "2014-2016"
        },
        {
            "score": 4.0618823504701e-7,
            "name": "宝马X1",
            "year": "2017"
        }
    ],
    "color_result": "白色"
}