【乘风新基建】【百度大脑新品体验】黑眼圈眼袋检测
756665228 发布于2020-08-28 浏览:2361 回复:11
3
收藏

本期小帅带来了黑眼圈眼袋检测接口体验(Java语言)

如何注册创建应用呢?请移步百度AI官方文档接入指南。这里不过多陈述了哈

https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjgn3

此接口暂未正式上线。需要的开发者们请加百度特效能力交流QQ群:  583486416

Step1:准备好AccessToken备用

参考官方的接入指南。获取access-token可以获取到access-token
https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjhhu#获取access-token

Step2:编写调用的代码

接口文档:https://ai.baidu.com/ai-doc/FACE/Ykcvn0pko 
一定要看接口文档。可以得到请求参数和返回的参数说明哦

工具类使用Hutool
以及百度官方提供的
FileUtil  https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
Base64Util https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2
    /**
     * @Author 小帅丶
     * @Description 黑眼圈眼袋检测接口
     * @Date  2020年8月26日17:34:10
     * @param filePath 文件路径
     * @return java.lang.String
     **/
    public static String getEyesAttr(String filePath) throws Exception{
        String imageBase64 = Base64Util.encode(FileUtil.readFileByBytes(filePath));
        String image = imageBase64;

        MedicalBeautyRequest request = new MedicalBeautyRequest();
        request.setAppid(appid);
        request.setImage(image);
        request.setImage_type(image_type);

        String result = HttpUtil.post(MedicalBeautyConts.FACE_EYESATTR_URL+"?access_token="+accessToken, JSON.toJSONString(request));
        return result;
    }

需要用到的MedicalBeautyRequest对象

需要安装lombok插件

@Data
@JsonInclude(JsonInclude.Include.NON_NULL)
public class MedicalBeautyRequest {
    private String appid;
    private String image;
    private String image_type;
    private Integer max_face_num;
}

Step3:找一张人脸图片测试一下

    public static void main(String[] args) throws Exception {

        String filePath = "F://testimg//10111700.jpg";
        //黑眼圈眼袋检测接口
        String result = getEyesAttr(filePath);
        System.out.println(result);
    }

原图

接口检测返回的内容

{"error_code":0,"error_msg":"SUCCESS","log_id":4584057535001,"timestamp":1598585521,"cached":0,"result":{"face_num":1,"face_list":[{"face_token":"6c42142795e325fa72f980e23a17f96c","location":{"left":184.76,"top":201.72,"width":174,"height":176,"degree":-2},"eyesattr":{"dark_circle_left":[[{"x":327,"y":239},{"x":326,"y":240},{"x":324,"y":240},{"x":323,"y":241},{"x":322,"y":241},{"x":321,"y":242},{"x":298,"y":242},{"x":298,"y":247},{"x":299,"y":247},{"x":302,"y":250},{"x":304,"y":250},{"x":306,"y":252},{"x":308,"y":252},{"x":309,"y":253},{"x":320,"y":253},{"x":321,"y":252},{"x":324,"y":252},{"x":325,"y":251},{"x":326,"y":251},{"x":332,"y":245},{"x":332,"y":244},{"x":333,"y":243},{"x":333,"y":240},{"x":332,"y":240},{"x":331,"y":239}]],"dark_circle_right":[[{"x":209,"y":241},{"x":208,"y":242},{"x":207,"y":242},{"x":207,"y":246},{"x":210,"y":249},{"x":210,"y":250},{"x":211,"y":251},{"x":211,"y":253},{"x":212,"y":253},{"x":216,"y":257},{"x":218,"y":257},{"x":219,"y":258},{"x":231,"y":258},{"x":232,"y":257},{"x":233,"y":257},{"x":234,"y":256},{"x":235,"y":256},{"x":238,"y":253},{"x":239,"y":253},{"x":239,"y":252},{"x":241,"y":250},{"x":241,"y":246},{"x":240,"y":245},{"x":221,"y":245},{"x":220,"y":244},{"x":218,"y":244},{"x":216,"y":242},{"x":214,"y":242},{"x":213,"y":241}]],"eye_bags_left":[],"eye_bags_right":[]}}]}}

会返回左、右眼黑眼圈以及左、右眼眼袋的坐标位置信息。很赞!!!。

Step4:把返回的数据处理一下,渲染到原图上

 /**
     * @Author 小帅丶
     * @Description 给图片增加框-黑眼圈眼袋
     * @Date  2020年8月26日17:49:05
     * @param filePath 图片路径
     * @param bean 包含坐标的对象 x-left y-top
     * @return void
     **/
    private static void getReactEyesAttr(String filePath, MedicalBeautyEyesAttrResponseBean bean) throws Exception{
        long startTime = System.currentTimeMillis();
        BufferedImage image = ImageIO.read(new File(filePath));
        Graphics2D g = (Graphics2D) image.getGraphics();
        Font font = new Font("微软雅黑", Font.ITALIC, 16);
        g.setStroke(new BasicStroke(2.5f));
        g.setColor(Color.RED);
        g.setFont(font);
        //左右黑眼圈对象
        List> dark_circle_left = bean.getResult().getFace_list().get(0).getEyesattr().getDark_circle_left();
        List> dark_circle_right = bean.getResult().getFace_list().get(0).getEyesattr().getDark_circle_right();
        //左右眼袋对象
        List> eye_bags_left = bean.getResult().getFace_list().get(0).getEyesattr().getEye_bags_left();
        List> eye_bags_right = bean.getResult().getFace_list().get(0).getEyesattr().getEye_bags_right();
        //左黑眼圈
        for (int i = 0; i < dark_circle_left.size(); i++) {
            for (int j = 0; j  darkCircleLeftBeans = dark_circle_left.get(i);
                int j2 = j+1;
                if(j2 darkCircleRightBeans = dark_circle_right.get(i);
                int j2 = j+1;
                if(j2 eyeBagsLeftBeans = eye_bags_left.get(i);
                int j2 = j+1;
                if(j2 eyeBagsRightBeans = eye_bags_right.get(i);
                int j2 = j+1;
                if(j2

处理后的原图

需要用到的MedicalBeautyEyesAttrResponseBean

import lombok.Data;
import lombok.NoArgsConstructor;
import java.util.List;
/**
 * @author 小帅丶
 * @className MedicalBeautyEyesAttrResponseBean
 * @Description 黑眼圈接口返回的对象
 * @Date 2020/8/26-17:38
 **/
@NoArgsConstructor
@Data
public class MedicalBeautyEyesAttrResponseBean {
    private int error_code;
    private String error_msg;
    private long log_id;
    private int timestamp;
    private int cached;
    private ResultBean result;

    @NoArgsConstructor
    @Data
    public static class ResultBean {
        private int face_num; //图片中的人脸数量
        private List face_list;//人脸信息列表

        @NoArgsConstructor
        @Data
        public static class FaceListBean {
            private String face_token;//人脸标志
            private LocationBean location;//人脸在图片中的位置
            private EyesattrBean eyesattr;//眼睛属性信息

            @NoArgsConstructor
            @Data
            public static class LocationBean {
                private double left;//人脸区域离左边界的距离
                private double top;//人脸区域离上边界的距离
                private int width;//人脸区域的宽度
                private int height;//人脸区域的高度
                private int degree;//人脸框相对于竖直方向的顺时针旋转角,[-180,180]
            }

            @NoArgsConstructor
            @Data
            public static class EyesattrBean {
                private List> dark_circle_left;//左眼黑眼圈
                private List> dark_circle_right;//右眼黑眼圈
                private List> eye_bags_left;//左眼眼袋
                private List> eye_bags_right;//右眼眼袋

                @NoArgsConstructor
                @Data
                public static class DarkCircleLeftBean {
                    private int x;//黑眼圈离左边界的距离
                    private int y;//黑眼圈离上边界的距离
                }

                @NoArgsConstructor
                @Data
                public static class DarkCircleRightBean {
                    private int x;//黑眼圈离左边界的距离
                    private int y;//黑眼圈离上边界的距离
                }
                @NoArgsConstructor
                @Data
                public static class EyeBagsLeftBean {
                    private int x;//眼袋离左边界的距离
                    private int y;//眼袋离上边界的距离
                }
                @NoArgsConstructor
                @Data
                public static class EyeBagsRightBean {
                    private int x;//眼袋离左边界的距离
                    private int y;//眼袋离上边界的距离
                }
            }
        }
    }
}

小程序演示

小程序代码后续在评论中给出

-----------------------------------------------------------

建议:

可以给出黑眼圈的颜色类型(茶色、黑色、青色)

眼袋没有图片检测出数据

后续是否迭代接口直接返回渲染后的图片呢?

应用场景

互动娱乐
应用于影视视频编辑及短视频制作场景,利用黑眼圈与眼袋检测能力,给视频带来更多有趣的互动玩法

医美智能分析
上传或配合采集工具实时拍摄照片,高精度检测分析黑眼圈眼袋问题,配合美颜模拟美容效果,推荐相应护肤产品
上手难易程度

使用接口简单。返回数据标注到图片上稍微复杂一些

收藏
点赞
3
个赞
共11条回复 最后由七年期限回复于2020-11-20
#12七年期限回复于2020-11-20
#11 汽车199101回复
可以在来一个python版的,哈哈

6啊

0
#11汽车199101回复于2020-11-20
#5 756665228回复
社区上传不了过多代码。那就百度云提供了。 链接: https://pan.baidu.com/s/13jLvaVyrkykXhMUhJ_ErAQ 提取码: ujy3 复制这段内容后打开百度网盘手机App,操作更方便哦
展开

可以在来一个python版的,哈哈

0
#10七年期限回复于2020-10-22
#5 756665228回复
社区上传不了过多代码。那就百度云提供了。 链接: https://pan.baidu.com/s/13jLvaVyrkykXhMUhJ_ErAQ 提取码: ujy3 复制这段内容后打开百度网盘手机App,操作更方便哦
展开

666

0
#5756665228回复于2020-09-27
#4 756665228回复
代码不全。重新贴一下 [代码]

社区上传不了过多代码。那就百度云提供了。

链接: https://pan.baidu.com/s/13jLvaVyrkykXhMUhJ_ErAQ 提取码: ujy3 复制这段内容后打开百度网盘手机App,操作更方便哦

1
#4756665228回复于2020-09-27

代码不全。重新贴一下

   public static String getReactEyesAttr(byte[] imageBytes, MedicalBeautyEyesAttrResponseBean bean) throws Exception{
        long startTime = System.currentTimeMillis();
        ByteArrayInputStream bais = new ByteArrayInputStream(imageBytes);
        BufferedImage image = ImageIO.read(bais);
        Graphics2D g = (Graphics2D) image.getGraphics();
        Font font = new Font("微软雅黑", Font.ITALIC, 16);
        g.setStroke(new BasicStroke(2.5f));
        g.setColor(Color.RED);
        g.setFont(font);
        //左右黑眼圈对象
        List> dark_circle_left = bean.getResult().getFace_list().get(0).getEyesattr().getDark_circle_left();
        List> dark_circle_right = bean.getResult().getFace_list().get(0).getEyesattr().getDark_circle_right();
        //左右眼袋对象
        List> eye_bags_left = bean.getResult().getFace_list().get(0).getEyesattr().getEye_bags_left();
        List> eye_bags_right = bean.getResult().getFace_list().get(0).getEyesattr().getEye_bags_right();
        //左黑眼圈
        for (int i = 0; i < dark_circle_left.size(); i++) {
            for (int j = 0; j  darkCircleLeftBeans = dark_circle_left.get(i);
                int j2 = j+1;
                if(j2 darkCircleRightBeans = dark_circle_right.get(i);
                int j2 = j+1;
                if(j2 eyeBagsLeftBeans = eye_bags_left.get(i);
                int j2 = j+1;
                if(j2 eyeBagsRightBeans = eye_bags_right.get(i);
                int j2 = j+1;
                if(j2
0
#3七年期限回复于2020-09-01

0
#2羊驼001回复于2020-08-28

upup

0
TOP
切换版块