实施前准备工作
一、本地图片上传为OSS
1.开通oss
2.创建Bucket
二、开通目标检测服务
三、查看所需API
这里我们用到阿里云视觉智能开放平台提供的三个功能:
- 车辆部件识别
- 车辆损伤识别
- 车险图片分类
1.车辆部件识别
检测图片中车辆部件的位置以及名称。
2.车辆损伤识别
针对常见小汽车车型,识别车辆外观受损部件及损伤类型,可识别数十种车辆部件、五大类外观损伤。(刮擦、凹陷、开裂、褶皱、穿孔)
3.车险图片分类
对输入的车险图片进行分类。
具体实施
一、本地图片上传至OSS的upload.py
1.示例代码
# -*- coding: utf-8 -*-
import os
import random
import string
import oss2
# 以下代码展示了文件上传的高级用法,如断点续传、分片上传等。
# 基本的文件上传如上传普通文件、追加文件,请参见object_basic.py
# 首先初始化AccessKeyId、AccessKeySecret、Endpoint等信息。
# 通过环境变量获取,或者把诸如“<你的AccessKeyId>”替换成真实的AccessKeyId等。
#
# 以杭州区域为例,Endpoint可以是:
# http://oss-cn-hangzhou.aliyuncs.com
# https://oss-cn-hangzhou.aliyuncs.com
# 分别以HTTP、HTTPS协议访问。
access_key_id = os.getenv('OSS_TEST_ACCESS_KEY_ID', '<你的AccessKeyId>')
access_key_secret = os.getenv('OSS_TEST_ACCESS_KEY_SECRET', '<你的AccessKeySecret>')
bucket_name = os.getenv('OSS_TEST_BUCKET', '<你的Bucket>')
endpoint = os.getenv('OSS_TEST_ENDPOINT', '<你的访问域名>')
# 确认上面的参数都填写正确了
for param in (access_key_id, access_key_secret, bucket_name, endpoint):
assert '<' not in param, '请设置参数:' + param
# 创建Bucket对象,所有Object相关的接口都可以通过Bucket对象来进行
bucket = oss2.Bucket(oss2.Auth(access_key_id, access_key_secret), endpoint, bucket_name)
def random_string(n):
return ''.join(random.choice(string.ascii_lowercase) for i in range(n))
# 生成一个本地文件用于测试。文件内容是bytes类型。
filename = random_string(32) + '.txt'
content = oss2.to_bytes(random_string(1024 * 1024))
with open(filename, 'wb') as fileobj:
fileobj.write(content)
"""
断点续传上传
"""
# 断点续传一:因为文件比较小(小于oss2.defaults.multipart_threshold),
# 所以实际上用的是oss2.Bucket.put_object
oss2.resumable_upload(bucket, 'remote-normal.txt', filename)
# 断点续传二:为了展示的需要,我们指定multipart_threshold可选参数,确保使用分片上传
oss2.resumable_upload(bucket, 'remote-multipart.txt', filename, multipart_threshold=100 * 1024)
"""
分片上传
"""
# 也可以直接调用分片上传接口。
# 首先可以用帮助函数设定分片大小,设我们期望的分片大小为128KB
total_size = os.path.getsize(filename)
part_size = oss2.determine_part_size(total_size, preferred_size=128 * 1024)
# 初始化分片上传,得到Upload ID。接下来的接口都要用到这个Upload ID。
key = 'remote-multipart2.txt'
upload_id = bucket.init_multipart_upload(key).upload_id
# 逐个上传分片
# 其中oss2.SizedFileAdapter()把fileobj转换为一个新的文件对象,新的文件对象可读的长度等于size_to_upload
with open(filename, 'rb') as fileobj:
parts = []
part_number = 1
offset = 0
while offset < total_size:
size_to_upload = min(part_size, total_size - offset)
result = bucket.upload_part(key, upload_id, part_number,
oss2.SizedFileAdapter(fileobj, size_to_upload))
parts.append(oss2.models.PartInfo(part_number, result.etag, size = size_to_upload, part_crc = result.crc))
offset += size_to_upload
part_number += 1
# 完成分片上传
bucket.complete_multipart_upload(key, upload_id, parts)
# 验证一下
with open(filename, 'rb') as fileobj:
assert bucket.get_object(key).read() == fileobj.read()
os.remove(filename)
二、车辆部件识别
1.请求参数
ImageURL:图片URL
2.示例代码
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkobjectdet.request.v20191230.RecognizeVehiclePartsRequest import RecognizeVehiclePartsRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-shanghai')
request = RecognizeVehiclePartsRequest()
request.set_accept_format('json')
request.set_ImageURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/RecognizeVehicleParts.jpg")
response = client.do_action_with_exception(request)
# python2: print(response)
print(str(response, encoding='utf-8'))
三、车辆损坏识别类
1.请求参数
ImageURL:图片URL
2.示例代码
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkobjectdet.request.v20191230.RecognizeVehicleDamageRequest import RecognizeVehicleDamageRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-shanghai')
request = RecognizeVehicleDamageRequest()
request.set_accept_format('json')
request.set_ImageURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/sanjiye-meizi/%E7%9B%AE%E6%A0%87%E6%A3%80%E6%B5%8B/%E8%BD%A6%E8%BE%86%E6%8D%9F%E4%BC%A4%E8%AF%86%E5%88%AB%20.jpg")
response = client.do_action_with_exception(request)
# python2: print(response)
print(str(response, encoding='utf-8'))
四、车险图片分类
1.请求参数
ImageURL:图片URL
2.示例代码
#!/usr/bin/env python
#coding=utf-8
from aliyunsdkcore.client import AcsClient
from aliyunsdkcore.acs_exception.exceptions import ClientException
from aliyunsdkcore.acs_exception.exceptions import ServerException
from aliyunsdkobjectdet.request.v20191230.ClassifyVehicleInsuranceRequest import ClassifyVehicleInsuranceRequest
client = AcsClient('<accessKeyId>', '<accessSecret>', 'cn-shanghai')
request = ClassifyVehicleInsuranceRequest()
request.set_accept_format('json')
request.set_ImageURL("http://viapi-test.oss-cn-shanghai.aliyuncs.com/sanjiye-meizi/%E7%9B%AE%E6%A0%87%E6%A3%80%E6%B5%8B/%E8%BD%A6%E9%99%A9%E5%9B%BE%E7%89%87.jpg")
response = client.do_action_with_exception(request)
# python2: print(response)
print(str(response, encoding='utf-8'))