栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

【Java】【百度AI】抖音超火的 【情侣拼脸】 教程来啦

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

【Java】【百度AI】抖音超火的 【情侣拼脸】 教程来啦

情侣拼脸效果图

 

今天作者就带来抖音超火的 【情侣拼脸】 教程。使用Java语言结合百度AI-人脸检测来完成!超简单哦~

注册百度AI

首先,就是注册百度AI账号,并创建人脸识别应用,获取AccessToken 备用。官方有图文教程哦。

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

准备两张图片

图片是必不可少的。我们需要准备2张图片,最好宽高、比例一致哦~ 如果不一致。那需要处理的步骤更多,大家可以自行研究一下哈~ 

Tips:可以优先对图片进行美化一下哦~

拼脸思路

1.图片依次调用百度AI-人脸检测服务。获取人脸关键点,拿到鼻梁的第一个坐标(nose_bridge_1)的X值。

2.左脸裁剪则xy给0,width为X,height为图片的原始高度。记为leftFace

3.右脸裁剪则y给0,x给X,width为原始宽度-X,height为图片的原始高度。记为rightFace

4.进行拼接,width为leftFace的width+rightFace的width,height为一致原始图高度。创建新的绘图对象SplitImage

5.先把leftFace画到SplitImage中,x,y 给0,从顶部开始绘制

6.再把rightFace回到SplitImage中,x为leftFace的宽度,y为0

最后大功告成!

部分代码
public class CPFaceSample {

    
    private static String access_token = "";
    
    private static int height;
    
    private static int width;


    public static void main(String[] args) throws Exception{
        
        String imagePathG = "F://testimg//cpface//girl02.jpg";
        
        String imagePathB = "F://testimg//cpface//boy02.jpg";
        
         String imagePath = "F:\testimg\cpface\";

        long startTime = System.currentTimeMillis();
        BufferedImage cpFace = getCPFace(imagePathB, imagePathG);
        //保存到本地
        ImageIO.write(cpFace,"jpg",new File(imagePath+System.currentTimeMillis()+".jpg"));
        long endTime = System.currentTimeMillis();
        System.out.println("耗时:"+(endTime-startTime));
    }
    
    public static BufferedImage getCPFace(String leftPath,String rightPath) throws Exception{
        //进行左右脸检测并分割成新的图片
        BufferedImage subImageLeft = getLeftFace(leftPath);
        BufferedImage subImageRight = getRightFace(rightPath);
        //计算高度,如果一致则height取其一即可
        if(subImageLeft.getHeight()>subImageRight.getHeight()){
            height = subImageLeft.getHeight();
        }else{
            height = subImageRight.getHeight();
        }
        //计算图片总宽度
        width = subImageLeft.getWidth() + subImageRight.getWidth();
        //创建一个BufferedImage对象
        BufferedImage bufferedImage = new BufferedImage(width,height,
                BufferedImage.TYPE_INT_RGB);
        //创建一个Graphics2D绘图对象
        Graphics2D graphics2D = bufferedImage.createGraphics();
        graphics2D.drawImage(subImageLeft, 0, 0, null);
        graphics2D.drawImage(subImageRight, subImageLeft.getWidth(), 0, null);
        graphics2D.dispose();
        return bufferedImage;
    }
    
    public static BufferedImage getLeftFace(String imagePath) throws Exception{
        FaceLandMark150Bean bean = getFaceMark(imagePath);
        //获取鼻梁第一个坐标
        double noseX = bean.getResult().getFace_list().get(0).getLandmark150().getNose_bridge_1().getX();
        //裁剪图片
        BufferedImage bufferedImage = DealDataUtil.base64ToBufferedImage(bean.getDeal_image());
        BufferedImage subImage = bufferedImage.getSubimage(0, 0,
                (int) noseX, bufferedImage.getHeight());
        return subImage;
    }
    
    public static BufferedImage getRightFace(String imagePath){
        FaceLandMark150Bean bean = getFaceMark(imagePath);
        //获取鼻梁第一个坐标
        double noseX = bean.getResult().getFace_list().get(0).getLandmark150().getNose_bridge_1().getX();
        //裁剪图片
        BufferedImage bufferedImage = DealDataUtil.base64ToBufferedImage(bean.getDeal_image());

        BufferedImage subImage = bufferedImage.getSubimage((int) noseX, 0,
                bufferedImage.getWidth()-(int) noseX, bufferedImage.getHeight());
        return subImage;
    }

    
    private static FaceLandMark150Bean getFaceMark(String imagePath) {
        byte [] image = FileUtil.readBytes(imagePath);
        String base64 = base64.encode(image);
        //获取人脸信息
        HashMap options = new HashMap<>();
        options.put("max_face_num", "10");
        options.put("face_field", "landmark150");
        options.put("image_type", "base64");
        options.put("image", base64);
        String result = HttpUtil.post(BaiDuConts.FACE_DETECT_URL + "?access_token=" + access_token,
                JSON.toJSonString(options));
        FaceLandMark150Bean bean = JSON.parseObject(result,
                FaceLandMark150Bean.class);
        bean.setDeal_image(base64);
        return bean;
    }

全部代码可以Gitee获取

情侣拼脸实现方法https://gitee.com/xshuai/worktools

不会编程?可以直达小程序体验哦~

微信扫一扫

 

 

 

 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/351585.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号