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

JAVA代码实现人物照片的人像分割 | 百度AI

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

JAVA代码实现人物照片的人像分割 | 百度AI

效果展示

原图

 

处理后

 

 

 实现方法

第一步 

先去百度云上注册账号,创建 ai人像分割应用

人像分割技术_人像分割算法_人像分割-百度AI开放平台

 第二部

代码实现

pom文件引入依赖

		
		
			com.baidu.aip
			java-sdk
			4.16.3
		

单类测试代码


import com.baidu.aip.bodyanalysis.AipBodyAnalysis;
import org.json.JSONObject;
import sun.misc.base64Decoder;

import javax.imageio.ImageIO;
import java.awt.*;
import java.awt.image.BufferedImage;
import java.io.*;
import java.util.base64;
import java.util.HashMap;

public class Sample {
    //设置APPID/AK/SK
    public static final String APP_ID = "你创建应用的 APP_ID ";
    public static final String API_KEY = "你创建应用的 API_KEY ";
    public static final String SECRET_KEY = "你创建应用的 SECRET_KEY ";

    public static void main(String[] args) {
        // 初始化一个AipBodyAnalysis
        AipBodyAnalysis client = new AipBodyAnalysis(APP_ID, API_KEY, SECRET_KEY);
        // 可选:设置网络连接参数
        client.setConnectionTimeoutInMillis(2000);
        client.setSocketTimeoutInMillis(60000);
        sample(client);
    }

    public static void sample(AipBodyAnalysis client){
        // 传入可选参数调用接口
        HashMap options = new HashMap();
        options.put("type", "foreground");
        // 参数为本地路径
        String imgPath = "C:\Users\liuya\Desktop\img\demo.jpg";
        FileInputStream fileInputStream = null;
        try {
            fileInputStream = new FileInputStream(imgPath);
            BufferedImage image = ImageIO.read(fileInputStream);
            JSonObject res = client.bodySeg(imgPath, options);
            System.out.println(res.get("foreground").toString());
            convert(res.get("foreground").toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }


    public static BufferedImage resize(BufferedImage img, int newW, int newH) {
        Image tmp = img.getScaledInstance(newW, newH, Image.SCALE_SMOOTH);
        BufferedImage dimg = new BufferedImage(newW, newH, BufferedImage.TYPE_INT_ARGB);
        Graphics2D g2d = dimg.createGraphics();
        g2d.drawImage(tmp, 0, 0, null);
        g2d.dispose();

        return dimg;
    }

    public static void convert(String labelmapbase64) {
        try {
            base64Decoder decoder = new base64Decoder();
            byte[] bytes = decoder.decodeBuffer(labelmapbase64);
            InputStream is = new ByteArrayInputStream(bytes);
            BufferedImage image = ImageIO.read(is);
            File newFile = new File("C:\Users\liuya\Desktop\people.png");
            ImageIO.write(image, "png", newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void convert(String labelmapbase64, int realWidth, int realHeight) {
        try {
            byte[] bytes = base64.getDecoder().decode(labelmapbase64);
            InputStream is = new ByteArrayInputStream(bytes);
            BufferedImage image = ImageIO.read(is);
            BufferedImage newImage = resize(image, realWidth, realHeight);
            BufferedImage grayImage = new BufferedImage(realWidth, realHeight, BufferedImage.TYPE_BYTE_GRAY);
            for(int i= 0 ; i < realWidth ; i++){
                for(int j = 0 ; j < realHeight; j++){
                    int rgb = newImage.getRGB(i, j);
                    grayImage.setRGB(i, j, rgb * 255);  //将像素存入缓冲区
                }
            }
            File newFile = new File("C:\Users\liuya\Desktop\gray.jpg");
            ImageIO.write(grayImage, "jpg", newFile);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

复制粘贴 上面代码到你的ide编辑器里,运行主方法即可,记得修改代码中的图片路径,和生成分割图形的路径。

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

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

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