在实现基本的 AI 功能之前,请确保:
本节介绍如何使用 ZegoEffects SDK 实现基本的图像处理功能,API 调用时序如下图:
传入 AI 资源或模型。
使用 Effects 的 AI 相关功能前,必须调用 setResources 接口导入 AI 资源或模型,详情请参考 快速开始 - 导入资源和模型。
// 传入人脸识别模型的绝对路径。人脸检测、大眼、瘦脸功能均须导入
ArrayList<String> aiModeInfos = new ArrayList<>();
aiModeInfos.add("sdcard/xxx/xxxxx/FaceDetectionModel.model");
aiModeInfos.add("sdcard/xxx/xxxxx/Segmentation.model");
// 传入模型路径列表,必须在 create 之前调用
ZegoEffects.setResources(aiModeInfos);
部署高级配置项
调用 setAdvancedConfig 接口,部署高级配置项,如配置设备性能等级,详情请参考 配置设备性能等级。
ZegoEffectsAdvancedConfig config = new ZegoEffectsAdvancedConfig();
// 可配置设备性能等级
ZegoEffects.setAdvancedConfig(config);
创建 Effects 对象。
将 1 前提条件 中申请到的 AppID、AppSign 直接传入 create 接口,由 SDK 内部鉴权之后,创建 Effects 对象并返回相关 错误码。
ZegoEffects mEffects = null;
long appid = *******;
String appSign = "*******";
mEffects = ZegoEffects.create(appid, appSign, applicationContex);
传入待处理的原始图像宽、高,调用 initEnv 接口初始化 Effects 对象。
// 初始化 Effects 对象,传入当前待处理的原始图像宽高
mEffects.initEnv(1280, 720);
调用 enableWhiten/enableBigEyes/setPortraitSegmentationBackgroundPath/enablePortraitSegmentation 接口开启各项 AI 功能。
// 1. 开启美白功能
// 2. 开启大眼功能
// 3. 开启 AI 人像分割功能,并传入分割背景图的绝对路
mEffects.enableWhiten(true)
.enableBigEyes(true)
.setPortraitSegmentationBackgroundPath("MY_BACKGROUND_PATH", ZegoEffectsScaleMode.ASPECT_FILL);
.enablePortraitSegmentation(true);
SDK 支持 RGB、YUV、Texture 等多种格式来处理图像,开发者可以参考如下表格:
视频帧类型 | 视频数据帧格式 | 处理数据接口 |
---|---|---|
Buff 类型 |
|
processImageBufferRGB |
Buff 类型 |
|
processImageBufferYUV |
Texture 类型 |
|
processTexture |
调用 processTexture 接口时,传入的纹理 ID 可以是 Texture2D 类型:
对于 Texture2D 类型纹理,可通过 processTexture 接口进行图像处理,具体实现方式,请参考如下代码:
ZegoEffectsVideoFrameParam zegoEffectsVideoFrameParam = new ZegoEffectsVideoFrameParam();
zegoEffectsVideoFrameParam.setFormat(ZegoEffectsVideoFrameFormat.RGBA32);
zegoEffectsVideoFrameParam.setWidth(width);
zegoEffectsVideoFrameParam.setHeight(height);
// 传入待处理的原始视频的 textureID,返回处理后的 textureID。
zegoTextureId = mEffects.processTexture(mTextureId, zegoEffectsVideoFrameParam);
1.4.11 及以后版本,使用 OES 纹理时,需要获取到 SurfaceTexture 的变换矩阵,并传入到 ZegoEffectsVideoFrameParam 参数的 tranMatrix 中。
通过如下方式从相机获取到的 SurfaceTexture 的 OES 纹理。
float[] tranMatrix = new float[16];
surfaceTexture.getTransformMatrix(tranMatrix);
确定 OES 纹理的宽和高。 大多数 Android 手机的 Camera 输出 OES 纹理是横屏的,其角度是 90 度或者 270 度。processTexture 接口内部会将其做空间旋转变换处理,因此提供给 processTexture 接口的宽高,可以通过以下代码进行适配调整:
//获取摄像机旋转角度
Camera.CameraInfo info = new Camera.CameraInfo();
Camera.getCameraInfo(cameraId, info);
boolean textureNeedRotation = (info.orientation % 180 == 90);
//如果已经获取到 OES 纹理的宽和高
//processTextureWidth 与 processTextureHeight,为提供给 ZegoEffectsVideoFrameParam 参数的长宽
processTextureWidth = textureNeedRotation? height:width;
processTextureHeight = textureNeedRotation? width:height;
ZegoEffectsVideoFrameParam zegoEffectsVideoFrameParam = new ZegoEffectsVideoFrameParam();
//对于 OES 纹理来说 SDK 内部使用的 ZegoEffectsVideoFrameFormat 可不设置,默认为 RGBA32
zegoEffectsVideoFrameParam.setFormat(ZegoEffectsVideoFrameFormat.RGBA32);
//指定纹理格式为 OES 类型
zegoEffectsVideoFrameParam.setTextureFormat(ZegoEffectsTextureFormat.TEXTURE_OES);
zegoEffectsVideoFrameParam.setWidth(processTextureWidth);
zegoEffectsVideoFrameParam.setHeight(processTextureHeight);
//传入 OES 纹理的变换矩阵
zegoEffectsVideoFrameParam.tranMatrix = tranMatrix;
// 传入待处理的原始视频的 textureID,返回处理后的 textureID。
zegoTextureId = mEffects.processTexture(mTextureId, zegoEffectsVideoFrameParam);
联系我们
文档反馈