JAVA代码实现人物照片的人像分割 | 百度AI
Posted 洛阳泰山
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAVA代码实现人物照片的人像分割 | 百度AI相关的知识,希望对你有一定的参考价值。
效果展示
原图
处理后
实现方法
第一步
先去百度云上注册账号,创建 ai人像分割应用
第二部
代码实现
pom文件引入依赖
<!-- https://mvnrepository.com/artifact/com.baidu.aip/java-sdk -->
<dependency>
<groupId>com.baidu.aip</groupId>
<artifactId>java-sdk</artifactId>
<version>4.16.3</version>
</dependency>
单类测试代码
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<String, String> options = new HashMap<String, String>();
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编辑器里,运行主方法即可,记得修改代码中的图片路径,和生成分割图形的路径。
以上是关于JAVA代码实现人物照片的人像分割 | 百度AI的主要内容,如果未能解决你的问题,请参考以下文章