视觉学习第四天

汽车检测

开通服务

先要开通汽车检测服务

导入依赖

<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>objectdet</artifactId>
    <version>0.0.5</version>
</dependency>

<!--    图片识别    -->
<dependency>
    <groupId>com.aliyun</groupId>
    <artifactId>ocr</artifactId>
    <version>1.0.3</version>
</dependency>

<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.1.37</version>
</dependency>

实现

service

@Service
public class CarService {

    @Value("${car.accessKeyId}")
    private String accessKeyId;

    @Value("${car.accessKeySecret}")
    private String accessKeySecret;

    private Client objectClient;

    private RuntimeObject runtimeObject;


    @PostConstruct
    public void initClient() throws Exception {
        Config objectConfig = new Config();
        objectConfig.type = "access_key";
        objectConfig.regionId = "cn-shanghai";
        objectConfig.accessKeyId = accessKeyId;
        objectConfig.accessKeySecret = accessKeySecret;

        objectConfig.endpoint = "objectdet.cn-shanghai.aliyuncs.com";
        objectClient = new Client(objectConfig);
        runtimeObject = new RuntimeObject();
    }
    //识别分类图片还未完成
    public String classifyVehicle(String filePath) throws Exception {
        ClassifyVehicleInsuranceAdvanceRequest request = new ClassifyVehicleInsuranceAdvanceRequest();
        request.imageURLObject = new FileInputStream(filePath);
        ClassifyVehicleInsuranceResponse response = objectClient.classifyVehicleInsuranceAdvance(request, runtimeObject);

        String result = null;

        for (ClassifyVehicleInsuranceResponse.ClassifyVehicleInsuranceResponseDataLabels item : response.data.labels){
            System.out.println(item.name + item.score);
        }

        return null;
    }


    public String myDetectVehicle(String filePath)throws Exception{
        DetectVehicleAdvanceRequest request = new DetectVehicleAdvanceRequest();
        request.imageURLObject = new FileInputStream(filePath);
        DetectVehicleResponse response = objectClient.detectVehicleAdvance(request, runtimeObject);

        String result = null;

        for (DetectVehicleResponse.DetectVehicleResponseDataDetectObjectInfoList item : response.data.detectObjectInfoList){
            System.out.println(item.type);
            System.out.println(item.score);

            if ("vehicle".equals(item.type)){
                System.out.println("检测成功!是机动车");
            }else {
                System.out.println("该图片不是机动车");
            }
           result =  item.type;
        }

        return result;
    }


    //车辆损伤识别
    public String myRecognizeVehicle(String filePath) throws Exception{
        //本地上传图片
        RecognizeVehicleDamageAdvanceRequest request = new RecognizeVehicleDamageAdvanceRequest();
        request.imageURLObject = new FileInputStream(filePath);

        //识别车辆损伤
        RecognizeVehicleDamageResponse response = objectClient.recognizeVehicleDamageAdvance(request, runtimeObject);

        return getHurtResult(response.data.elements);
    }

    public String getHurtResult(RecognizeVehicleDamageResponse.RecognizeVehicleDamageResponseDataElements[] items){

        StringBuffer type = new StringBuffer("检测到的车辆损伤为:");
        for (RecognizeVehicleDamageResponse.RecognizeVehicleDamageResponseDataElements item: items){

            switch (item.type) {
                case "1": type.append("轻微刮擦   ");
                    break;
                case "2": type.append("重度刮擦   ");
                    break;
                case "3": type.append("轻度变形   ");
                    break;
                case "4": type.append("中度变形   ");
                    break;
                case "5": type.append("重度变形   ");
                    break;
                case "6": type.append("crack破损孔洞   ");
                    break;
                case "7": type.append("翼子板和大灯缝隙   ");
                    break;
                case "8": type.append("翼子板保险杠缝隙   ");
                    break;
                case "9": type.append("大灯轻微刮擦   ");
                    break;
                case "10": type.append("大灯重度刮擦   ");
                    break;
                case "11": type.append("大灯破损   ");
                    break;
                case "12": type.append("后视镜轻微刮擦   ");
                    break;
                case "13": type.append("后视镜玻璃破损   ");
                    break;
                case "14": type.append("后视镜脱落   ");
                    break;
                case "15": type.append("挡风玻璃破损   ");
                    break;
            }
        }
        return type.toString();
    }


    public String fileUpload(MultipartFile file){
        //获取文件名
        String uploadFileName = file.getOriginalFilename();

        //上传路径保存设置 UUID
        String path = "src/main/resources/static/upload";

        //如果路径不存在,创建一个
        File realPath = new File(path);
        if (!realPath.exists()){
            realPath.mkdir();
        }

        //上传文件,并保存
        InputStream is = null;
        OutputStream os = null;

        try {
            is = file.getInputStream();
            os = new FileOutputStream(new File(realPath,uploadFileName));
            int len = 0;
            byte[] buffer = new byte[1024];
            while ((len = is.read(buffer)) != -1){
                os.write(buffer,0,len);
                os.flush();
            }
        } catch (IOException e) {
            e.printStackTrace();
        }finally {
            try {
                os.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

            try {
                is.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return path +"\\"+uploadFileName;
    }
}

controller

@Controller
public class CarController {

    @Autowired
    private CarService carService;

    @RequestMapping("/upload")
    public String fileUpload(@RequestParam("carfile")MultipartFile carfile,
    Model model)throws Exception{

        System.out.println("22222");

        //上传受损车辆
        String carPath = carService.fileUpload(carfile);

        //调用Service层分析图片
        String carStr = null;
        String carStr2 = null;

        //机动车检测
        carStr = carService.myDetectVehicle(carPath);
        //车辆损伤识别
        carStr2 = carService.myRecognizeVehicle(carPath);

        //保存数据到数据库中


        //将图片路径放入Model中
        String carFileName = carfile.getOriginalFilename();

        model.addAttribute("carfilename","/upload/"+carFileName);

        if ("vehicle".equals(carStr)){
            model.addAttribute("carStr","检测成功!是机动车");
        }else {
            model.addAttribute("carStr","您上传的图片不是机动车,请重新上传!!");
        }

        model.addAttribute("carStr2",carStr2);

        return "result";
    }


    @RequestMapping(value = "/test",method = RequestMethod.GET)
    public String toResult(){

        return "test";
    }
}

前端

test

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form th:action="@{/upload}" enctype="multipart/form-data" method="post">

    受损车辆:<input type="file" name="carfile">
    <input type="submit" value="upload">
</form>
</body>
</html>

result

<!DOCTYPE html>
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
    <img th:src="${carfilename}">
    <p th:text="${carStr}"></p>
    <p th:text="${carStr2}"></p>
</body>
</html>
上一篇:Spring Boot 2.0(五):Docker Compose + Spring Boot + Nginx + Mysql 实践


下一篇:视觉学习第三天