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

编程入门 行业动态 更新时间:2024-10-28 12:30:42

【Java】【百度AI】抖音超火的 【情侣拼脸】 教程<a href=https://www.elefans.com/category/jswz/34/1767016.html style=来啦"/>

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

情侣拼脸效果图

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

注册百度AI

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

准备两张图片

图片是必不可少的。我们需要准备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

最后大功告成!

部分代码

/*** @author 小帅丶* @className FaceBeautySample* @Description 情侣拼脸-示例代码* @Date 2021年10月27日**/
public class CPFaceSample {/** 接口必要的AccessToken */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));}/*** @Author 小帅丶* @Description 情侣拼脸*              建议两张图片宽高一致、比例一致哦~* @Date  2021-10-27 13:42* @param leftPath - 左边的人脸照片本地路径* @param rightPath - 右边的人脸照片本地路径* @return java.awt.image.BufferedImage**/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;}/*** @Author 小帅丶* @Description 获取左脸* @Date  2021-10-27 9:32* @param imagePath - 人脸图片* @return java.lang.String**/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;}/*** @Author 小帅丶* @Description 获取右脸* @Date  2021-10-27 9:32* @param imagePath - 人脸图片* @return java.lang.String**/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;}/*** @Author 小帅丶* @Description 获取人脸检测数据* @Date  2021-10-27 10:49* @param imagePath - 人脸图片* @return cn.ydxiaoshuai.sample.aisample.baidu.model.FaceLandMark150Bean**/private static FaceLandMark150Bean getFaceMark(String imagePath) {byte [] image = FileUtil.readBytes(imagePath);String base64 = Base64.encode(image);//获取人脸信息HashMap<String,String> 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获取

情侣拼脸实现方法

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

微信扫一扫

 

更多推荐

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

本文发布于:2024-03-23 17:50:35,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1741034.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:来啦   情侣   教程   Java   AI

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!