Skip to content

高质量去除背景

Ryu Xin edited this page Feb 28, 2024 · 3 revisions

支持版本

从1.0.6版本开始,本客户端支持高质量背景去除API。

<dependency>
  <groupId>com.hiforce.pixel.open</groupId>
  <artifactId>pixel-force-open-client</artifactId>
  <version>1.0.6</version>
</dependency>

API介绍

public class PixelForceClient {
    ...
    public InvokeResult removeBackground(RemoveBgClientRequest request)
}

去除背景的入参RemoveBgClientRequest定义如下:

public class RemoveBgClientRequest extends ClientReqDTO {

    private static final long serialVersionUID = 756954615638167890L;

    @Getter
    @Setter
    private String inputImagePath;

    @Getter
    @Setter
    private boolean transparent = true;

    @Getter
    @Setter
    private boolean colorMode = true;

    @Getter
    @Setter
    private String colorHex = "#FFFFFF";

    @Getter
    @Setter
    private String bgImagePath;

    @Getter
    @Setter
    private boolean needMask;
}
  • inputImagePath: 需要移除背景的原始图片路径
  • transparent: 布尔值,移除背景后是否是透明底
  • colorHex: 指定颜色HEX值,当transparent为false以及colorMode为true时生效
  • colorMode: 是否是颜色替换模式,默认为true
  • bgImagePath: 指定图片背景路径,当colorMode为false是生效
  • needMask: 是否同步返回去除背景后,对应的蒙版图片,默认为false

样例代码

public class RemoveBgSample01 extends BaseRemoteSample {

    public static void main(String[] args) {
        RemoveBgSample01 changeBgSample = new RemoveBgSample01();
        changeBgSample.run();
    }

    @Override
    public String getAccessKey() {
        return "YOUR_ACCESS_KEY";
    }

    @Override
    public String getAccessSecret() {
        return "YOUR_ACCESS_SECRET";
    }

    @Override
    public void execute() throws Exception {
        ResourceResult result = getFirstAvailableResource();
        String resourceId = result.getResources().get(0).getId();

        UploadResult uploadResult = uploadAttachment(resourceId, "/remove_bg/01.png");

        RemoveBgClientRequest request = new RemoveBgClientRequest();
        request.setInputImagePath(uploadResult.getPath());
        request.setTransparent(true);
        request.setNeedMask(true);

        InvokeResult invokeResult = PixelForceClient.getInstance().removeBackground(request);
        if (!invokeResult.getStatus().equals(ApiStatusEnum.SUCCESS.getCode())) {
            throw new RuntimeException("Failed to invoke Remove BG");
        }

        System.out.println("Remove Bg InvokeResult:" + JSON.toJSONString(invokeResult));
        waitAndQueryTaskExecuteResult(invokeResult, resourceId);

    }
}

我们需要将样例代码中的API访问秘钥替换成有效的秘钥,然后执行 RemoveBgSample01.java 程序,对下面图片进行背景移除:

image

运行日志:

image

移除背景后的效果如下:

image

同步输出的蒙版如下:

image

Home

Quick Start

Integrating with PixelForce


首页

快速开始

调用PixelForce

HiFORCE容器

Clone this wiki locally