You need to enable JavaScript to run this app.
导航

人体分割

最近更新时间2023.06.28 14:39:31

首次发布时间2022.02.25 17:16:51

简介

人体分割SDK提供从图像中实时提取人体轮廓的能力,支持多人同时分割,具备稳定性高、性能开销低、速度快等特点。

技术规格
支持平台Android、iOS、Windows、Mac
支持输入格式RGBA8888、BGRA8888、BGR888、RGB888
支持最小输入尺寸短边128
内存占用<4M (测试设备OppoR11)
速度<11ms(测试设备iPhone7)
C接口说明

详细接口说明查看头文件:bef_effect_ai_portrait_matting.h

1.创建人体分割句柄

BEF_SDK_API bef_effect_result_t  
bef_effect_ai_portrait_matting_create(bef_effect_handle_t *handle);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t创建的人体分割的检测句柄


返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

2.从文件初始化模型参数

BEF_SDK_API bef_effect_result_t  
bef_effect_ai_portrait_matting_init_model(
    bef_effect_handle_t handle,
    bef_ai_matting_model_type type,
    const char* param_path);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t人体分割的检测句柄
typebef_ai_matting_model_type模型类型,目前分大小模型
param_pathconst char*模型文件的路径

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

3.设置SDK参数

BEF_SDK_API bef_effect_result_t
bef_effect_ai_portrait_matting_set_param(
    bef_effect_handle_t handle,
    bef_ai_matting_param_type type,
    int value);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t人体分割的检测句柄
typebef_ai_matting_param_type算法参数,用来设置边界的模式、强制预测、短边长度
valueint默认为1

备注
bef_ai_matting_param_type 详细见bef_effect_ai_portrait_matting.h

/*
 * @brief SDK参数
 * edge_mode:
 *    算法参数,用来设置边界的模式
 *    - 0: 不加边界
 *    - 1: 加边界
 *    - 2: 加边界, 其中, 2 和 3 策略不太一样,但效果上差别不大,可随意取一个
 * fresh_every:
 *    算法参数,设置调用多少次强制做预测,目前设置 15 即可
 * MP_OutputMinSideLen:
 *    返回短边的长度, 默认值为128, 需要为16的倍数;
 * MP_OutputWidth 不设置,只做GetParam 兼容之前的调用
 * MP_OutputHeight 不设置,只做GetParam 兼容之前的接口;
 **/
enum bef_ai_matting_param_type {
    BEF_MP_EdgeMode = 0,
    BEF_MP_FrashEvery = 1,
    BEF_MP_OutputMinSideLen = 2,
    BEF_MP_OutputWidth = 3,
    BEF_MP_OutputHeight = 4,
};

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

4.获取输出mask的尺寸

BEF_SDK_API bef_effect_result_t
bef_effect_ai_portrait_matting_get_output_shape(
    bef_effect_handle_t handle,
    int width,
    int height,
    int *output_width,
    int *output_height);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t人体分割的检测句柄
widthint输入图的宽度
heightint输入图的高度
output_widthint*输出mask的宽度
output_heightint*输出mask的高度

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

5.进行抠图操作

BEF_SDK_API bef_effect_result_t
bef_effect_ai_portrait_matting_do_detect(
    bef_effect_handle_t handle,
    const unsigned char* src_image_data,
    bef_ai_pixel_format pixel_format,
    int width,
    int height,
    int image_stride,
    bef_ai_rotate_type orient,
    bool need_flip_alpha,
    bef_ai_matting_ret *ret);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t人体分割的检测句柄
src_image_dataconst unsigned char*为传入图像的大小
pixel_formatbef_ai_pixel_format传入图像的类型
widthint传入图像的宽
heightint传入图像的高
image_strideint传入图像的步长
orientbef_ai_rotate_type传入图像旋转角
need_flip_alphabool传出图像是否需要翻转
retbef_ai_matting_ret*传出图像

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

6.释放句柄

BEF_SDK_API bef_effect_result_t
bef_effect_ai_portrait_matting_destroy(bef_effect_handle_t handle);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t人体分割的检测句柄

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

6.人体分割授权

BEF_SDK_API bef_effect_result_t 
bef_effect_ai_portrait_matting_check_license(
    JNIEnv* env, 
    jobject context, 
    bef_effect_handle_t handle, 
    const char *licensePath);

参数说明

参数名参数类型参数说明
handlebef_effect_handle_t人体分割的检测句柄
licensePathconst char *授权文件字符串

返回值
成功返回 BEF_RESULT_SUC, 失败返回相应错误码, 具体请参考 bef_effect_ai_public_define.h

Java 接口说明

接口说明
详细接口说明查看文件:com.bytedance.labcv.effectsdk.PortraitMatting.java

1.初始化人体分割句柄

public int init(
      Context context, 
      String modelpath, 
      BytedEffectConstants.PortraitMatting modelType, 
      String licensePath)

参数说明

参数名参数类型参数说明
contextContext应用上下文
modelpathString模型文件绝对路径
modelTypeBytedEffectConstants.PortraitMatting使用模型类型
licensePathString授权文件绝对路径

备注
{@link BytedEffectConstants}
BEF_PORTAITMATTING_LARGE_MODEL(0), 大模型,耗时比小模型稍长;
BEF_PORTAITMATTING_SMALL_MODEL(1); 小模型,较快;

返回值
成功返回BEF_RESULT_SUC,否则返回对应的错误码

2.设置SDK参数

public int setParam(
      BytedEffectConstants.PorraitMattingParamType paramType , 
      int paramValue)

参数说明

参数名参数类型参数说明
paramTypeBytedEffectConstants.PorraitMattingParamTypesdk设置参数类型
paramValueintsdk设置参数值

备注
详见BytedEffectConstants.PorraitMattingParamType

public enum PorraitMattingParamType
    {
        /**
         *  算法参数,用来设置边界的模式
         *       - 0: 不加边界
         *       - 1: 加边界
         *       - 2: 加边界, 其中, 2 和 3 策略不太一样,但效果上差别不大,可随意取一个
         */
        BEF_MP_EdgeMode(0),
        /**
         * 算法参数,设置调用多少次强制做预测,目前设置 15 即可
         */
        BEF_MP_FrashEvery(1),
        /**
         * 返回短边的长度, 默认值为128, 需要为16的倍数;
         */
        BEF_MP_OutputMinSideLen(2);

        private int value;

        PorraitMattingParamType(int v){this.value = v;}

        public int getValue(){return value;}
    }

返回值
成功返回BEF_RESULT_SUC,否则返回对应的错误码

3.人体分割

public MattingMask detectMatting(
      ByteBuffer imgdata,
      BytedEffectConstants.PixlFormat pixel_format,
      int width, 
      int height, 
      int stride,
      BytedEffectConstants.Rotation orientation,
      boolean needflipalpha)

参数说明

参数名参数类型参数说明
imgdataByteBuffer输入图像数据
pixel_formatBytedEffectConstants.PixlFormat输入数据格式
widthint输入图像宽度
heightint输入图像高度
strideint输入图像步长
orientationBytedEffectConstants.Rotation输入图像旋转角
needFlipAlphaboolean输出图像是否需要翻转


备注
类HairMask详细情况可以参考com.bytedance.labcv.effectsdk.PortraitMatting.java

/**
   * 人体分割结果, 单通道灰度图
   */
  public class MattingMask
  {
    private int width;
    private int height;
    private byte[] buffer;

    public byte[] getBuffer() {
      return buffer;
    }

    public int getWidth() {
      return width;
    }

    public int getHeight()
    {
      return height;
    }

    @Override
    public String toString() {
      return String.format("l: %d w:%d, h:%d", buffer.length, width, height);
    }
  }

返回值
成功返回 MattingMask-人体分割结果,单通道灰度图

4.释放人体分割句柄

public void release()

FAQ

1.如果出现任何异常,请查看是否有以下类型日志

答:建议首先查看以下tag输出Error类型的日志:

bef_effect_ai 和SMASH_E_LOG 以及 EffectSDK

2.人体分割设置后完全没有效果

答:建议按以下步骤排查下

  • 请确认人体分割句柄初始化是否成功
  • 请确认人体分割句柄已经授权成功
  • 如果初始化成功,请检查设置的输入图像数据完整且类型支持

3.人体分割SDK输出的mask的数据定义?

答:我们SDK 输出的byte[]中的值在0-255范围中,255表示人体部分,0表示背景部分,(0,255)中间的值表示的是人体的边缘部分。

错误码

错误码请参考错误码表