【Java】人脸口罩检测体验
756665228 发布于2020-03-17 17:52 浏览:1057 回复:5
2
收藏

人脸口罩检测全面开放  https://ai.baidu.com/forum/topic/show/958031

AI接入指南 https://ai.baidu.com/ai-doc/REFERENCE/Ck3dwjgn3 一定要看哦从注册到创建应用都有图文教程

本文Java语言示例、使用maven搭建。需要引用的jar(fastjson、lombok、hutool)

1.请求人脸检测参数封装对象

import lombok.Data;
/**
 * @author 小帅丶
 * @className FaceRequest
 * @Description 人脸检测基础参数对象封装
 * @Date 2020/3/17-10:13
 **/
@Data
public class FaceRequest {
    private String image_type;//图片类型 
    private String image;//图片信息
    private String face_field;//检测属性
    private Integer max_face_num=10;//最多处理人脸的数目
}

2.窗口显示处理后的图片方法

import javax.swing.*;
import java.awt.*;
/**
 * @author 小帅丶
 * @className JFrameSample
 * @Description 窗口显示对象
 * @Date 2020/3/9-9:46
 **/
public class JFrameSample extends JFrame{
    /**
     * @Author 小帅丶
     * @Description 显示图片
     * @Date  2020/3/9 10:21
     * @param filePath 图片路径
     * @return
     **/
    public JFrameSample(String filePath){
        setTitle("处理后的图片");
        ImageIcon imageIcon = new ImageIcon(filePath);
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setLocationRelativeTo(null);
        setSize(imageIcon.getIconWidth(),imageIcon.getIconHeight());
        setResizable(false);
        getContentPane().setLayout(null);
        JPanel panel = new ImagePanel(filePath,imageIcon);
        panel.setBounds(0,0,imageIcon.getIconWidth(),imageIcon.getIconHeight());
        getContentPane().add(panel);
        setVisible(true);
    }
    class ImagePanel extends JPanel{
        String filePathNew = null;
        ImageIcon imageIconNew = null;
        public ImagePanel(String filePath,ImageIcon imageIcon) {
            filePathNew = filePath;
            imageIconNew = imageIcon;
        }
        @Override
        public void paint(Graphics g) {
            super.paint(g);
            g.drawImage(imageIconNew.getImage(), 0, 0, imageIconNew.getIconWidth(),imageIconNew.getIconHeight(),this);
        }
    }
}

3.请求接口代码

import cn.hutool.http.HttpUtil;
import cn.xsshome.taip.util.FileUtil;
import com.alibaba.fastjson.JSON;
import com.baidu.aip.util.Base64Util;
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 FaceMaskSample
 * @Description 人脸口罩检测
 * @Date 2020/3/17-10:11
 **/
@NoArgsConstructor
@Data
public class FaceMaskSample {
    //人脸检测口罩接口地址
    public static String REFUSE_URL = "https://aip.baidubce.com/rest/2.0/face/v3/detect";
    //人脸图片请求方式
    private static String IMAGEBASE64 = "BASE64";

    public static void main(String[] args) throws Exception{
        //接口必要参数AccessToken
        String accessToken = "";
        //要检测的图片
        String filePath = "F://testimg//demo-card-2.jpg";
        //图片转base64字符串处理
        String base64Img = Base64Util.encode(FileUtil.readFileByBytes(filePath));
        //参数对象转JSON字符串
        FaceRequest bean = new FaceRequest();
        bean.setImage_type(IMAGEBASE64);
        bean.setImage(base64Img);
        //查询的属性 age,beauty,expression,face_shape,gender,glasses,landmark,landmark150,race,
        // quality,eye_status,emotion,face_type,mask信息
        // 逗号分隔. 默认只返回face_token、人脸框、概率和旋转角度
        bean.setFace_field("age,beauty,mask");
        String param = JSON.toJSONString(bean);
        //发送请求并获取结果
        String result = HttpUtil.post(REFUSE_URL+"?access_token="+accessToken, param);
        //打印检测结果
        System.out.println(result);
        //给图片画框
        getReact(filePath, JSON.parseObject(result, FaceMaskSample.class));
    }
    /**
     * @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,FaceMaskSample bean) throws Exception{
        BufferedImage image = ImageIO.read(new File(filePath));
        Graphics gold = image.getGraphics();
        Graphics2D g = (Graphics2D) gold;
        //口罩置信度
        String maskName = "";
        //画框
        if(bean.getResult().getFace_list().size()>0){
            for (int i = 0; i < bean.getResult().getFace_list().size(); i++) {
                //年龄
                int age = bean.getResult().getFace_list().get(i).getAge();
                //颜值
                double beauty = bean.getResult().getFace_list().get(i).getBeauty();
                g.setColor(Color.BLUE);
                if(bean.getResult().getFace_list().get(i).getMask().getType()==1){
                    maskName = "佩戴口罩,置信度:"+bean.getResult().getFace_list().get(i).getMask().getProbability();
                }else{
                    maskName = "未佩戴口罩,置信度:"+bean.getResult().getFace_list().get(i).getMask().getProbability();
                }
                g.drawString(maskName, (int)bean.getResult().getFace_list().get(i).getLocation().getLeft(), (int)bean.getResult().getFace_list().get(i).getLocation().getTop());
                g.drawRect((int)bean.getResult().getFace_list().get(i).getLocation().getLeft(), (int)bean.getResult().getFace_list().get(i).getLocation().getTop(), (int)bean.getResult().getFace_list().get(i).getLocation().getWidth(), (int)bean.getResult().getFace_list().get(i).getLocation().getHeight());
            }
        }
        FileOutputStream out = new FileOutputStream("F://testimg//demo-card-2317.jpg");//输出图片的本地路径
        ImageIO.write(image, "jpeg", out);
        new JFrameSample("F://testimg//demo-card-2317.jpg");
    }
    /**********************接口返回的Bean BEGIN**********************/
    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 int face_probability;
            private AngleBean angle;
            private int age;
            private double beauty;
            private MaskBean mask;

            @NoArgsConstructor
            @Data
            public static class LocationBean {
                private double left;
                private double top;
                private int width;
                private int height;
                private int rotation;
            }

            @NoArgsConstructor
            @Data
            public static class AngleBean {
                private double yaw;
                private double pitch;
                private double roll;
            }

            @NoArgsConstructor
            @Data
            public static class MaskBean {
                private int type;
                private int probability;
            }
        }
    }
    /**********************接口返回的Bean END**********************/
}

4.部分工具类代码

FileUtil   https://ai.baidu.com/file/658A35ABAB2D404FBF903F64D47C1F72
Base64Util https://ai.baidu.com/file/C8D81F3301E24D2892968F09AE1AD6E2

5.原图

6.处理后图片

7.运行时截图

个人主页:https://ai.baidu.com/forum/user/center/2312713

个人邮箱:youngxiaoshuai#163.com (#替换为@)

收藏
点赞
2
个赞
共5条回复 最后由756665228回复于2020-03-30 09:48
#6756665228回复于2020-03-30 09:48:20
#4 ZengZhuoqi回复
可以实现视频流吗

这个提交工单咨询一下百度吧。

云服务的接口也可以。只不过是需要接口QPS提高。

1
#5ZengZhuoqi回复于2020-03-30 07:44:48

这样感觉场景会多很多哦

0
#4ZengZhuoqi回复于2020-03-30 07:44:23

可以实现视频流吗

0
#3TZLGKE回复于2020-03-21 11:34:17

赞!

人脸口罩检测demo已上线,欢迎大家直接传图体验~ https://ai.baidu.com/tech/face/detect

0
TOP
切换版块