【EasyDL物体检测】Java示例代码包含框选
756665228 发布于2020-02-27 13:47 浏览:2701 回复:1
2
收藏

本帖直接以代码为主

 

import cn.hutool.http.HttpUtil;
import cn.xsshome.taip.util.FileUtil;
import com.alibaba.fastjson.JSON;
import com.baidu.aip.util.Base64Util;
//需要idea安装lombok插件
import lombok.Data;
import lombok.NoArgsConstructor;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileOutputStream;
import java.util.List;

/**
 * @author 小帅丶
 * @className EasyDLSampleSecond
 * @Description 调用EasyDL示例代码
 * @Date 2020/2/27-12:41
 **/
public class EasyDLSampleSecond {
    //垃圾分类物体检测EasyDL自训练接口地址
    public static String REFUSE_URL = "https://aip.baidubce.com/rpc/2.0/ai_custom/v1/detection/refuse_classify";

    public static void main(String[] args) throws Exception{
        //接口必要参数AccessToken
        String accessToken = "24.xxx";
        //要检测的图片
        String filePath = "F://testimg//body//2.jpg";
        //图片转base64字符串处理
        String base64Img = Base64Util.encode(FileUtil.readFileByBytes(filePath));
        //参数对象组装
        RequestBean bean = new RequestBean();
        bean.setImage(base64Img);
        //参数对象转JSON字符串
        String param = JSON.toJSONString(bean);
        //发送请求并获取结果
        String result = HttpUtil.post(REFUSE_URL+"?access_token="+accessToken, param);
        //打印检测结果
        System.out.println(result);
        //给图片加框
        getReact(filePath, JSON.parseObject(result, ResponseBean.class));
    }
    /**
     * @Author 小帅丶
     * @Description 接口请求所需对象
     * @Date  2020/2/27 12:50
     **/
    @Data
    public static class RequestBean{
        private String image;
        private String threshold;
    }
    /**
     * @Author 小帅丶
     * @Description 接口返回的对象
     * @Date  2020/2/27 12:50
     **/
    @Data
    public static class ResponseBean{
        private long log_id;
        private List results;
        @NoArgsConstructor
        @Data
        public static class ResultsBean {
            private LocationBean location;
            private String name;
            private double score;
            @NoArgsConstructor
            @Data
            public static class LocationBean {
                private int height;
                private int left;
                private int top;
                private int width;
            }
        }
    }
    /**
     * @Author 小帅丶
     * @Description 给图片增加框
     * @Date  2020/2/27 13:11
     * @param filePath 图片路径
     * @param bean 包含坐标的对象 x-left y-top width-width height-height
     * @return void
     **/
    public static void getReact(String filePath,ResponseBean bean) throws Exception{
        BufferedImage image = ImageIO.read(new File(filePath));
        Graphics g = image.getGraphics();
        List results = bean.getResults();
        Font font = new Font("微软雅黑", Font.BOLD, 16);
        for (int i = 0; i < results.size(); i++) {
            g.setColor(Color.BLUE);
            g.setFont(font);
            g.drawString(results.get(i).getName(), results.get(i).getLocation().getLeft(), results.get(i).getLocation().getTop());
            g.drawRect(results.get(i).getLocation().getLeft(), results.get(i).getLocation().getTop(), results.get(i).getLocation().getWidth(), results.get(i).getLocation().getHeight());
        }
        FileOutputStream out = new FileOutputStream("F://testimg//body//22.jpg");//输出图片的地
        ImageIO.write(image, "jpeg", out);
    }
}

 

原图

处理后的图

收藏
点赞
2
个赞
共1条回复 最后由付洋洋carrie回复于2020-02-27 15:06
#2付洋洋carrie回复于2020-02-27 15:06:14

手动赞

1
TOP
切换版块