Day 04
作者:第五组 邓佳阳
打卡截图
学习笔记
车辆保险应用系统搭建
开通OSS
直观解释:云上存储
蛮考验后端写API的,按照Restful规格写就比较合理。
注意:无论是OSS还是API,尽量都放在一个地区
寻找需要的API
阿里云视觉智能开放平台闪亮登场,根据查询往期内容,会用到三个功能:
- 车辆部件识别
- 车辆损伤识别
- 车险图片分类
1.车辆部件识别
检测图片中车辆部件的位置以及名称。
2.车辆损伤识别
针对常见小汽车车型,识别车辆外观受损部件及损伤类型,可识别数十种车辆部件、五大类外观损伤。(刮擦、凹陷、开裂、褶皱、穿孔)
3.车险图片分类
对输入的车险图片进行分类。
实现
图片来源于 "鸭梨亚"
导入MAVEN
Apache MAVEN 解决第三方依赖
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.8.0</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-core</artifactId>
<version>4.4.8</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.52</version>
</dependency>
<dependency>
<groupId>com.aliyun</groupId>
<artifactId>aliyun-java-sdk-objectdet</artifactId>
<version>1.0.7</version>
</dependency>
类
uploadPic类:本地上传至OSS;
RecognizeVehicleParts类:车辆部件识别;
ecognizeVehicleDamage类:车辆损坏识别;
ClassifyVehicleInsurance类:车险图片分类。
jpackage com.example.demo;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import java.io.File;
import java.net.URL;
import java.security.SecureRandom;
import java.util.Date;
import java.lang.*;
import java.util.Scanner;
public class UploadPic {
public static String UploadPic(){
// Endpoint以杭州为例,其它Region请按实际情况填写。
String endpoint = "oss-cn-shanghai.aliyuncs.com";
// 阿里云主账号AccessKey。
String accessKeyId = "*************";
String accessKeySecret = "*************";
//本地文件名
System.out.println("请输入本地图片path:");
Scanner scanner = new Scanner(System.in);
String fileName = scanner.nextLine();
String bucketName = "auto-insurance-pic";
// 获取文件的后缀名
String suffixName = fileName.substring(fileName.lastIndexOf("."));
// 生成上传文件名
String objectName = System.currentTimeMillis() + "" + new SecureRandom().nextInt(0x0400) + suffixName;
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
// 如果需要上传时设置存储类型与访问权限,请参考以下示例代码。
// ObjectMetadata metadata = new ObjectMetadata();
// metadata.setHeader(OSSHeaders.OSS_STORAGE_CLASS, StorageClass.Standard.toString());
// metadata.setObjectAcl(CannedAccessControlList.Private);
// putObjectRequest.setMetadata(metadata);
// 上传文件。
ossClient.putObject(bucketName, objectName, new File(fileName));
// 设置URL过期时间为1小时。
Date expiration = new Date(System.currentTimeMillis() + 3600 * 1000);
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
// 关闭OSSClient。
ossClient.shutdown();
return url.toString();
}
}
package com.example.demo;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.example.demo.UploadPic;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.objectdet.model.v20191230.*;
public class RecognizeVehicleParts {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "accessKeyId", "accessKeySecret");
IAcsClient client = new DefaultAcsClient(profile);
RecognizeVehiclePartsRequest request = new RecognizeVehiclePartsRequest();
request.setRegionId("cn-shanghai");
request.setImageURL(UploadPic.UploadPic());
try {
RecognizeVehiclePartsResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
package com.example.demo;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.objectdet.model.v20191230.*;
public class RecognizeVehicleDamage {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "accessKeyId", "accessKeySecret");
IAcsClient client = new DefaultAcsClient(profile);
RecognizeVehicleDamageRequest request = new RecognizeVehicleDamageRequest();
request.setRegionId("cn-shanghai");
request.setImageURL(UploadPic.UploadPic());
try {
RecognizeVehicleDamageResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
package com.example.demo;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.exceptions.ServerException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;
import java.util.*;
import com.aliyuncs.objectdet.model.v20191230.*;
public class ClassifyVehicleInsurance {
public static void main(String[] args) {
DefaultProfile profile = DefaultProfile.getProfile("cn-shanghai", "accessKeyId", "accessKeySecret");
IAcsClient client = new DefaultAcsClient(profile);
ClassifyVehicleInsuranceRequest request = new ClassifyVehicleInsuranceRequest();
request.setRegionId("cn-shanghai");
request.setImageURL(UploadPic.UploadPic());
try {
ClassifyVehicleInsuranceResponse response = client.getAcsResponse(request);
System.out.println(new Gson().toJson(response));
} catch (ServerException e) {
e.printStackTrace();
} catch (ClientException e) {
System.out.println("ErrCode:" + e.getErrCode());
System.out.println("ErrMsg:" + e.getErrMsg());
System.out.println("RequestId:" + e.getRequestId());
}
}
}
结果
返回值
{"requestId":"87BEADED-F581-4C38-9F5F-E6F8DF0A1BA5","data":{"threshold":0.0,"labels":[{"score":0.0046,"name":"others"},{"score":0.0164,"name":"detail"},{"score":0.1934,"name":"component"},{"score":0.0,"name":"vin"},{"score":8.0E-4,"name":"people"},{"score":2.0E-4,"name":"motor"},{"score":0.1439,"name":"semi-car"},{"score":0.0027,"name":"panoramic"},{"score":3.0E-4,"name":"license"},{"score":0.0169,"name":"CT-scan"},{"score":5.0E-4,"name":"truck"},{"score":0.0144,"name":"disassembly"},{"score":0.6059,"name":"scene"}]}}
总结
牛人太多了,我太菜了。