接口能力:
對於輸入的一張圖片(可正常解碼,且長寬比適宜),識別人體的輪廓範圍,與背景進行分離,適用於拍照背景替換、照片合成、身體特效等場景。輸入正常人像圖片,返回分割後的二值結果圖和分割類型(目前僅支持person)。
算法用於將圖片中的人像和背景進行分離,可以應用於APP背景替換,背景虛化等場景。
1.1 背景替換
利用分割結果進行背景的替換。比如圖片中的人像的背後背景可以更換背景色,例如藍底紅底證件照。
1.2 背景虛化
利用分割的結果將背景進行虛化得到更美的更有藝術效果的照片,例如濾鏡。
- 1)邀測的接口,不能直接在控制檯調用,可通過QQ羣(860337848、649285136)聯繫羣管、或提交工單申請開通測試權限。
- 2)邀測的接口,暫未封裝服務端SDK,只能通過API調用。
此接口返回的數據需要額外的處理。
需要對Java的BufferedImage、ImageIO、Graphics2D有一定的瞭解。對圖像處理灰度 二值化有了解。
示例代碼中使用到了第三方的json處理包fastjson
<!-- https://mvnrepository.com/artifact/com.alibaba/fastjson -->
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.49</version>
</dependency>
- Java調用示例代碼及返回數據處理
感謝百度相關RD提供的Java處理代碼哦
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import javax.imageio.ImageIO;
import sun.misc.BASE64Decoder;
import com.alibaba.fastjson.JSONObject;
//Base64Util FileUtil HttpUtil百度提供 自行下載即可 小帥整理的下載地址
//http://aixiaoshuai.mydoc.io/?t=234826
import com.xs.util.baidu.Base64Util;
import com.xs.util.baidu.FileUtil;
import com.xs.util.baidu.HttpUtil;
/**
* 人像分割Java-API示例代碼
* @author 小帥丶
*
*/
public class BodySegSample {
//人像分割接口地址
public static String BODYSEG_URL = "https://aip.baidubce.com/rest/2.0/image-classify/v1/body_seg";
public static void main(String[] args) throws Exception {
String accessToken = "自己應用獲取的AccessToken";
//返回字符串
String filePath = "G:/bodyseg.jpg";
String result = getBodySegResult(filePath, accessToken);
JSONObject jsonObject = JSONObject.parseObject(result);
//圖片轉BufferedImage對象
BufferedImage image = ImageIO.read(new File(filePath));
//對接口返回的labelmapbase64進行處理 並拿圖片的原始寬高
convert(jsonObject.getString("labelmap"),image.getWidth(),image.getHeight());
}
/**
* @param imagePath 圖片本地路徑
* @param accessToken 應用AccessToken
* @return String
* @throws Exception
*/
private static String getBodySegResult(String imagePath, String accessToken)
throws Exception {
byte[] imgData = FileUtil.readFileByBytes(imagePath);
String imgStr = Base64Util.encode(imgData);
String param = "image=" + URLEncoder.encode(imgStr, "UTF-8");
// 注意這裏僅爲了簡化編碼每一次請求都去獲取access_token,線上環境access_token有過期時間,
// 客戶端可自行緩存,過期後重新獲取。
String result = HttpUtil.post(BODYSEG_URL, accessToken, param);
return result;
}
/**
* 圖像轉換
* @param labelmapBase64 分割結果圖片,檢測出的二值圖像,base64編碼之後
* @param realWidth 圖片原始高度px
* @param realHeight 圖片原始寬度px
*/
public static void convert(String labelmapBase64, int realWidth, int realHeight) {
try {
BASE64Decoder base64Decoder = new BASE64Decoder();
byte[] bytes = base64Decoder.decodeBuffer(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("G:/gray001.jpg");
ImageIO.write(grayImage, "jpg", newFile);
} catch (IOException e) {
e.printStackTrace();
}
}
/**
* 重置圖片大小
* @param img 圖片數據
* @param newW 圖片寬度
* @param newH 圖片高度
* @return
*/
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;
}
}
- 使用到的原圖
- 處理後的圖片