AI 美颜
  • iOS
  • Android : Java
  • macOS
  • Windows
  • 产品简介
    • 概述
    • 发布日志
    • 计费说明
  • 下载
  • 体验 App
  • 快速开始
    • 跑通示例源码
    • 集成 SDK
    • 导入资源和模型
    • 在线鉴权
    • 实现图像处理
  • 基础功能
  • 最佳实践
  • 客户端 API
  • 常见错误码
  • 常见问题
  • 文档中心
  • AI 美颜
  • 快速开始
  • 导入资源和模型

导入资源和模型

更新时间:2024-01-12 15:33

1 概览

在使用 ZegoEffects SDK 提供的 AI 功能前,请在 下载 页面,获取最新版本的 AI 资源或模型:

  • 如果需要使用美颜,美型,美白,白牙,红润,挂件贴纸等功能,请先导入对应的资源。
  • 如果需要使用大眼、瘦脸、人像分割等功能,请先导入对应的模型。

2 使用步骤

  1. 指定 AI 资源和模型的绝对路径。请在 下载 页面,获取最新版本的 AI 资源或模型(bundle/model 文件)。

    // 传入模型和资源绝对路径。美颜,美型,美白,白牙,红润,挂件贴纸功能均须导入资源。大眼、瘦脸、人像分割功能须导入模型。
    ArrayList<String> aiResourcesInfos = new ArrayList<>();
    aiResourcesInfos.add("sdcard/xxx/xxxxx/CommonResources.bundle");
    aiResourcesInfos.add("sdcard/xxx/xxxxx/FaceWhiteningResources.bundle");
    aiResourcesInfos.add("sdcard/xxx/xxxxx/PendantResources.bundle");
    aiResourcesInfos.add("sdcard/xxx/xxxxx/RosyResources.bundle");
    aiResourcesInfos.add("sdcard/xxx/xxxxx/TeethWhiteningResources.bundle");
    aiResourcesInfos.add("sdcard/xxx/xxxxx/FaceDetectionModel.model");
    aiResourcesInfos.add("sdcard/xxx/xxxxx/Segmentation.model");
  2. 拷贝资源到手机目录下。

    /**
     * 从 assets 目录中复制整个文件夹内容,拷贝到 /data/data/包名/files/目录中
     *
     * @param activity activity 使用 CopyFiles 类的 Activity
     * @param filePath String  文件路径,如:/assets/aa
     */
    public static void copyAssetsDir2Phone(Context activity, String filePath, String destPath) {
        try {
            String[] fileList = activity.getAssets().list(filePath);
            if (fileList.length > 0) {
                File file = new File(activity.getFilesDir().getAbsolutePath() + File.separator + destPath + File.separator + filePath);
                if (file.exists()) {
                    //TODO: 这里做删除操作,起保护作用。
                }
                file.mkdirs();//如果文件夹不存在,则递归
                for (String fileName : fileList) {
                    if(!filePath.isEmpty()) { ;
                        copyAssetsDir2Phone(activity, filePath + File.separator + fileName, destPath);
                    }else{
                        copyAssetsDir2Phone(activity, fileName, destPath);
                    }
                }
            } else {
                InputStream inputStream = activity.getAssets().open(filePath);
                File file = new File(activity.getFilesDir().getAbsolutePath() + File.separator + destPath + File.separator + filePath);
                if (file.exists()) {
                    boolean delete = file.delete();
                }
                if (!file.exists() || file.length() == 0) {
                    FileOutputStream fos = new FileOutputStream(file);
                    int len = -1;
                    byte[] buffer = new byte[1024];
                    while ((len = inputStream.read(buffer)) != -1) {
                        fos.write(buffer, 0, len);
                    }
                    fos.flush();
                    inputStream.close();
                    fos.close();
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    开发者可以将资源和模型放在 assets 文件夹下,如果开发过程中对资源包的大小有要求、也可以将获取到的资源和模型放在自己的服务器上,并在初始化 SDK 前从服务器上下载下来。从 assets 或服务器下载的资源和模型都需要复制到 Android/data/"当前应用包名"/files/ 目录下,并将资源和模型的“绝对路径”传递给 SDK 即可。

  1. 在调用 create 接口创建对象之前,先调用 setResources 接口传入资源和模型路径列表,加载资源,模型。

    // 传入资源和模型路径列表,必须在 create 之前调用
    ZegoEffects.setResources(aiResourcesInfos);

3 支持的模型和资源

当前 ZegoEffects SDK 支持的模型和资源,请参考下表:

模型/资源 描述 支持功能
CommonResources 美颜美型通用资源 美颜、美型
FaceWhiteningResources 美白颜色查找表资源 美白
PendantResources 挂件贴纸资源 挂件
RosyResources 红润颜色查找表资源 红润
TeethWhiteningResources 白牙颜色查找表资源 白牙
FaceDetectionModel 人脸检测模型 人脸检测、大眼、瘦脸
SegmentationModel 人像分割模型 人像分割
本篇目录