Skip to content

使用方法

虹软人脸识别RK356x专用版支持在线激活,离线激活,支持图片人脸识别(可识别网络图片),活体检测,离线识别,相机预览旋转,相机人脸识别,批量注册(支持网络图片)等,识别率更高速度更快,支持保存用户的id和名称,插件支持uniapp和uniapp x

相关链接

插件使用注意事项

  • 如果您在使用插件的过程中有任何问题,可以联系作者,作者将全力协助您使用插件
  • 本文档同时提供了uniapp的用法示例和uniappx的用法示例,插件市场导入的示例项目仅为uniapp的用法示例,如果您需要uniappx的示例项目可以通过下方的链接下载示例

    https://pan.baidu.com/s/1Z0zKcefc1P9BdWc3VZfZIw?pwd=mkf4

  • 组件只能在nvue/uvue页面中使用,不支持vue页面

联系作者

关注微信公众号可联系作者

官方文档

https://ai.arcsoft.com.cn/manual/docs#/250

SDK版本

ArcFaceRk356x 5.0

插件地址

https://ext.dcloud.net.cn/plugin?id=21935

权限

  • android.permission.READ_EXTERNAL_STORAGE
  • android.permission.READ_PHONE_STATE
  • android.permission.WRITE_EXTERNAL_STORAGE
  • android.permission.CAMERA

API用法

仅提供了部分示例,完整示例请参考示例文件

用法

在需要使用插件的页面加载以下代码

js
import * as module from "@/uni_modules/leven-uts-arcFaceRk356x"
js
import * as module from "@/uni_modules/leven-uts-arcFaceRk356x"

示例代码

vue
<template>
  <view>
    <uni-card title="人脸管理">
      <button type="primary" @click="init">初始化</button>
      <button type="primary" @click="getImageFace">获取图片人脸信息</button>
      <button type="primary" @click="imageFaceRegister">图片注册人脸</button>
      <button type="primary" @click="clearFace">清空人脸库</button>
      <button type="primary" @click="deleteFace">删除人脸</button>
      <button type="primary" @click="getFace">获取人脸</button>
      <button type="primary" @click="getAllFace">获取所有人脸信息</button>
      <button type="primary" @click="batchRegister">批量注册</button>
      <button type="primary" @click="getFaceCount">获取注册的人脸数量</button>
      <button type="primary" @click="faceCompare">人脸对比</button>
    </uni-card>

    <!-- 注册进度弹窗 -->
    <process ref="refProcess" :processTitle="batchRegisterProcess.processTitle" :process="batchRegisterProcess.process"
      :successCount="batchRegisterProcess.successCount" :failedCount="batchRegisterProcess.failedCount"
      :totalCount="batchRegisterProcess.totalCount">
    </process>
  </view>
</template>

<script>
  // 批量注册人脸的文件
  import {
    faceList
  } from "@/utils/face.js"
  //加载组件
  import process from "./process.nvue"
  import * as module from "@/uni_modules/leven-uts-arcFaceRk356x"
  export default {
    components: {
      process
    },
    data() {
      return {
        //批量注册进度
        batchRegisterProcess: {
          //进度描述
          processTitle: "0",
          //当前进度
          process: 0,
          // 成功数量
          successCount: 0,
          //失败数量
          failedCount: 0,
          //注册总数量
          totalCount: 0
        }
      }
    },
    methods: {
      // 初始化
      init() {
        module.init({
          complete: (res) => {
            uni.$lv.func.toast(JSON.stringify(res));
          }
        })
      },
      // 获取图片人脸信息
      getImageFace() {
        module.getImageFace({
          params: {
            //本地或网络url地址
            // url: "/sdcard/DCIM/Camera/IMG_20241013_150358.jpg",
            url: "http://www.yeyuboke.com/uniplugin/banner/IMG_20241013_150358.jpg",
          },
          complete: (res) => {
            uni.$lv.func.toast(JSON.stringify(res));
          }
        })
      },
      // 人脸对比
      faceCompare() {
        module.faceCompare({
          params: {
            //本地或网络url地址
            url: "http://www.yeyuboke.com/uniplugin/banner/IMG_20241013_150358.jpg",
            //相似度,大于或等于该相似度的人脸视为通过,默认:0.85
            similar: 0.85
          },
          complete: (res) => {
            uni.$lv.func.toast(JSON.stringify(res));
          }
        })
      },
      // 图片注册人脸
      imageFaceRegister() {
        module.imageFaceRegister({
          params: {
            //本地或网络url地址
            url: "http://www.yeyuboke.com/uniplugin/banner/IMG_20241013_150358.jpg",
            // url: "http://www.yeyuboke.com/svga/8.jpg",
            // 保存的id(可以不传该参数,默认时间戳)
            id: "123",
            //保存的姓名(可以不传该参数,默认时间戳)
            name: "leven",
            // 同一人是否可以多次注册,默认true
            registerMultiple: false
          },
          complete: (res) => {
            uni.$lv.func.toast(JSON.stringify(res));
          }
        })
      },
      // 清空人脸库
      clearFace() {
        module.clearFace({
          complete: (res) => {
            uni.$lv.func.toast(JSON.stringify(res));
          }
        })
      },
      // 删除人脸
      deleteFace() {
        module.deleteFace({
          params: {
            id: "123"
          },
          complete: (res) => {
            uni.$lv.func.toast(JSON.stringify(res));
          }
        })
      },
      // 获取人脸
      getFace() {
        module.getFace({
          params: {
            id: "123"
          },
          complete: (res) => {
            uni.$lv.func.toast(JSON.stringify(res));
          }
        })
      },
      // 批量注册
      batchRegister() {
        let list = [];
        faceList.map(item => {
          let obj = {
            id: item._id,
            name: item.name,
            url: item.pic
          }
          list.push(obj)
        })
        this.batchRegisterProcess.totalCount = list.length;
        module.batchRegister({
          params: {
            // 同一人是否可以多次注册,默认true
            registerMultiple: false,
            list: list
          },
          complete: (res) => {
            let data = res.data || {};
            if (res.code == -107) {
              this.showToast(res.message);
              return false;
            }
            if (this.$refs.refProcess) {
              this.$refs.refProcess.open();
            }
            if (res.code == 0) {
              let status = data.status;
              if (status == "registerComplete") {
                //注册完成
                if (this.$refs.refProcess) {
                  this.$refs.refProcess.close();
                }
                this.batchRegisterProcess.failedCount = 0;
                this.batchRegisterProcess.process = 0;
                this.batchRegisterProcess.processTitle = "0";
                this.batchRegisterProcess.successCount = 0;
                this.batchRegisterProcess.totalCount = 0;
                uni.$lv.func.toast("注册结束");
              } else if (status == "registerProgress") {
                //注册中
                this.setProcessBar(data);
              }
            } else {
              //注册失败
              this.setProcessBar(data);
            }
          }
        })
      },
      // 获取所有人脸信息
      getAllFace() {
        module.getAllFace({
          complete: (res) => {
            console.log(res);
          }
        })
      },
      // 获取注册的人脸数量
      getFaceCount() {
        module.getFaceCount({
          complete: (res) => {
            uni.$lv.func.toast(JSON.stringify(res));
          }
        })
      },
      showToast(content) {
        uni.showToast({
          icon: "none",
          title: content
        })
      },
      // 设置进度条
      setProcessBar(data) {
        // console.log(JSON.stringify(data))
        let successCount = data.success || this.batchRegisterProcess.successCount;
        let failedCount = data.failed || this.batchRegisterProcess.failedCount;
        let progress = this.batchRegisterProcess.process / 100;
        if (data.progress) {
          progress = parseFloat(data.progress);
        }
        let progressValue = progress.toFixed(4);
        //高精度乘法
        progressValue = uni.$lv.number.mul(progressValue, 100);
        this.batchRegisterProcess.failedCount = failedCount;
        this.batchRegisterProcess.process = Math.floor(progressValue);
        this.batchRegisterProcess.processTitle = progressValue + "";
        this.batchRegisterProcess.successCount = successCount;
      }
    }
  }
</script>

<style>

</style>
vue
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <button type="primary" @click="init">初始化</button>
    <button type="primary" @click="getImageFace">获取图片人脸信息</button>
    <button type="primary" @click="imageFaceRegister">图片注册人脸</button>
    <button type="primary" @click="clearFace">清空人脸库</button>
    <button type="primary" @click="deleteFace">删除人脸</button>
    <button type="primary" @click="getFace">获取人脸</button>
    <button type="primary" @click="getAllFace">获取所有人脸信息</button>
    <button type="primary" @click="batchRegister">批量注册</button>
    <button type="primary" @click="getFaceCount">获取注册的人脸数量</button>
    <button type="primary" @click="faceCompare">人脸对比</button>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script setup>
  import { faceList } from "@/utils/arcFaceRk356x/face"
  import * as module from "@/uni_modules/leven-uts-arcFaceRk356x"

  function init() {
    module.init({
      complete: (res) => {
        console.log(JSON.stringify(res))
      }
    })
  }

  function getImageFace() {
    module.getImageFace({
      params: {
        //本地或网络url地址
        // url: "/sdcard/DCIM/Camera/IMG_20241013_150358.jpg",
        url: "http://www.yeyuboke.com/uniplugin/banner/IMG_20241013_150358.jpg",
      },
      complete: (res) => {
        console.log(JSON.stringify(res))
      }
    })
  }

  function imageFaceRegister() {
    module.imageFaceRegister({
      params: {
        //本地或网络url地址
        url: "http://www.yeyuboke.com/uniplugin/banner/IMG_20241013_150358.jpg",
        // url: "http://www.yeyuboke.com/svga/8.jpg",
        // 保存的id(可以不传该参数,默认时间戳)
        id: "123",
        //保存的姓名(可以不传该参数,默认时间戳)
        name: "leven",
        // 同一人是否可以多次注册,默认true
        registerMultiple: false
      },
      complete: (res) => {
        console.log(JSON.stringify(res))
      }
    })
  }

  function clearFace() {
    module.clearFace({
      complete: (res) => {
        console.log(JSON.stringify(res))
      }
    })
  }

  function deleteFace() {
    module.deleteFace({
      params: {
        id: "123"
      },
      complete: (res) => {
        console.log(JSON.stringify(res))
      }
    })
  }

  function getFace() {
    module.getFace({
      params: {
        id: "123"
      },
      complete: (res) => {
        console.log(JSON.stringify(res))
      }
    })
  }

  function getAllFace() {
    module.getAllFace({
      complete: (res) => {
        console.log(res);
      }
    })
  }

  function batchRegister() {
    let list : UTSJSONObject[] = [];
    faceList.map(item => {
      let obj = {
        id: item._id,
        name: item.name,
        url: item.pic
      }
      list.push(obj)
    })
    module.batchRegister({
      params: {
        // 同一人是否可以多次注册,默认true
        registerMultiple: false,
        list: list
      },
      complete: (res) => {
        console.log(JSON.stringify(res))
      }
    })
  }

  function getFaceCount() {
    module.getFaceCount({
      complete: (res) => {
        console.log(JSON.stringify(res))
      }
    })
  }

  function faceCompare() {
    module.faceCompare({
      params: {
        //本地或网络url地址
        url: "http://www.yeyuboke.com/uniplugin/banner/IMG_20241013_150358.jpg",
        //相似度,大于或等于该相似度的人脸视为通过,默认:0.85
        similar: 0.85
      },
      complete: (res) => {
        console.log(JSON.stringify(res))
      }
    })
  }
</script>

<style>

</style>

组件用法

用法

在需要使用插件的页面添加以下代码

vue
<leven-uts-arc-face-rk356x ref="refLevenArcFacePro" style="flex:1; height: 500px;" :config="config"
  @onError="onError" @onEvent="onEvent" @onEventMethod="onEventMethod">
</leven-uts-arc-face-rk356x>
vue
<leven-uts-arc-face-rk356x ref="refLevenArcFacePro" style="flex:1; height: 500px;" :configX="config"
  @onError="onError" @onEvent="onEvent" @onEventMethod="onEventMethod">
</leven-uts-arc-face-rk356x>

示例代码

vue
<template>
  <view>
    <uni-card title="人脸识别">
      <view style="flex:1; height: 500px; position: relative;">
        <leven-uts-arc-face-rk356x ref="refLevenArcFacePro" style="flex:1; height: 500px;" :config="config"
          @onError="onError" @onEvent="onEvent" @onEventMethod="onEventMethod">
        </leven-uts-arc-face-rk356x>
        <!-- 组件内部自定义内容 -->
        <cover-view v-if="imageBase64" style="position: absolute; right: 0; bottom: 0;">
          <image :src="imageBase64" style="width: 170px; height: 170px;" mode="scaleToFill"></image>
        </cover-view>
      </view>
      <view>
        <button type="primary" @click="register">注册人脸</button>
        <button type="primary" @click="switchCamera">切换相机</button>
        <button type="primary" @click="stop">关闭预览</button>
        <button type="primary" @click="start">开启预览</button>
        <button type="primary" @click="closeFace">关闭人脸检测</button>
        <button type="primary" @click="openFace">开启人脸检测</button>
        <button type="primary" @click="getCameraData">摄像头数据</button>
      </view>
    </uni-card>
    <uni-card title="人脸识别页面调用api">
      <button type="primary" @click="getFaceCount">获取注册的人脸数量</button>
    </uni-card>
  </view>
</template>

<script>
  import * as module from "@/uni_modules/leven-uts-arcFaceRk356x"
  export default {
    data() {
      return {
        //组件配置
        config: {
          //相机属性,所有的参数都可以不传,不传则按默认的
          camera: {
            // 相机预览旋转角度
            rotation: 270,
            //相机模式,1.前置,0.后置(默认)
            facing: 0,
            // 摄像机预览圆角,默认:0
            radius: 50,
            //预览分辨率,默认:[1280,720]
            size: [1280, 720],
            //是否锁定屏幕启动方向,默认:true
            screenLocked: true,
            //是否回调预览数据,默认:false
            isPreviewFrame: false,
            //预览数据回调时间,单位:毫秒,为0不做限制,有数据就回调
            previewTime: 0
          },
          // 视频检测配置,所有参数都可以不传,不传则按默认的
          video: {
            // 视频检测角度,可接收参数,0,90,180,270,360(默认)
            orient: 360,
            // 人脸框是否处于X反向状态,如果未设置该参数人脸框和人脸处于反向请将该参数设置为true
            isContraryX: false,
            // 人脸框是否处于Y反向状态,如果未设置该参数人脸框和人脸处于反向请将该参数设置为true
            isContraryY: false,
            // 识别阈值(默认:0.8)
            similar: 0.9,
            // 是否进行活体检测(默认为true)
            liveness: true,
            //识别是否展示面部信息(默认为true)
            showFaceInfo: false,
            //活体检测阈值设置
            livenessParams: {
              //可见光活体检测阈值,默认:0.5
              rgb: 0.5,
              //红外活体检测阈值,默认:0.7
              ir: 0.7
            },
            //是否显示人脸上方识别状态提示,默认:true
            showFaceResultNotice: true,
            //人脸识别尺寸,超过该尺寸才识别,否则不识别,可根据识别成功后返回的人脸尺寸进行调整,默认:0,不做人脸尺寸识别
            faceSize: 300,
          },
        },
        //注册的人脸图片
        imageBase64: ""
      }
    },
    methods: {
      //**************************人脸识别页面api调试*********************
      // 获取注册的人脸数量
      getFaceCount() {
        module.getFaceCount(res => {
          // this.showToast(JSON.stringify(res))
          uni.$lv.func.toast(JSON.stringify(res));
        })
      },
      //**************************人脸识别页面api调试*********************
      // 注册人脸
      register() {
        if (this.$refs.refLevenArcFacePro) {
          this.$refs.refLevenArcFacePro.register({
            // 注册后保存的id(可以不传该参数,默认时间戳)
            id: "123",
            //注册后保存的名字(可以不传该参数,默认时间戳)
            name: "leven",
            //同一人脸是否可以多次注册
            registerMultiple: false,
          }, res => {
            if (res.code == 0) {
              uni.$lv.func.toast("注册成功");
            } else {
              uni.$lv.func.toast(res.message);
            }
          });
        }
      },
      // 切换相机
      switchCamera() {
        if (this.$refs.refLevenArcFacePro) {
          this.$refs.refLevenArcFacePro.switchCamera(res => {
            uni.$lv.func.toast(JSON.stringify(res));
          });
        }
      },
      // 摄像头数据
      getCameraData() {
        if (this.$refs.refLevenArcFacePro) {
          this.$refs.refLevenArcFacePro.getCameraData(res => {
            console.log(res)
            uni.$lv.func.toast(JSON.stringify(res));
          });
        }
      },
      // 关闭预览
      stop() {
        if (this.$refs.refLevenArcFacePro) {
          this.$refs.refLevenArcFacePro.stop(res => {
            uni.$lv.func.toast(JSON.stringify(res));
          });
        }
      },
      // 开启预览
      start() {
        if (this.$refs.refLevenArcFacePro) {
          this.$refs.refLevenArcFacePro.start(res => {
            uni.$lv.func.toast(JSON.stringify(res));
          });
        }
      },
      // 关闭人脸检测
      closeFace() {
        if (this.$refs.refLevenArcFacePro) {
          this.$refs.refLevenArcFacePro.closeFace(res => {
            uni.$lv.func.toast(JSON.stringify(res));
          });
        }
      },
      // 开启人脸检测
      openFace() {
        if (this.$refs.refLevenArcFacePro) {
          this.$refs.refLevenArcFacePro.openFace(res => {
            uni.$lv.func.toast(JSON.stringify(res));
          });
        }
      },
      // 错误事件
      onError(e) {
        let detail = e.detail || {};
        uni.$lv.func.toast("错误事件:" + JSON.stringify(detail));
      },
      //其他事件
      onEvent(e) {
        let detail = e.detail || {};
        console.log(detail)
      },
      //方法事件
      onEventMethod(e) {
        let detail = e.detail || {};
        let data = detail.data || {}
        let type = detail.type || ""
        if (type == "onFaceResult") {
          if (data.compareImageBase64) {
            this.imageBase64 = "data:image/jpeg;base64," + detail.compareImageBase64
          }
        }
        console.log(detail)
      }
    }
  }
</script>

<style>

</style>
vue
<template>
  <!-- #ifdef APP -->
  <scroll-view style="flex:1">
  <!-- #endif -->
    <leven-uts-arc-face-rk356x ref="refLevenArcFacePro" style="flex:1; height: 500px;" :configX="config"
      @onError="onError" @onEvent="onEvent" @onEventMethod="onEventMethod">
    </leven-uts-arc-face-rk356x>

    <button type="primary" @click="register">注册人脸</button>
    <button type="primary" @click="switchCamera">切换相机</button>
    <button type="primary" @click="stop">关闭预览</button>
    <button type="primary" @click="start">开启预览</button>
    <button type="primary" @click="closeFace">关闭人脸检测</button>
    <button type="primary" @click="openFace">开启人脸检测</button>
    <button type="primary" @click="getCameraData">摄像头数据</button>
  <!-- #ifdef APP -->
  </scroll-view>
  <!-- #endif -->
</template>

<script lang="uts" setup>
  import JSONObject from 'com.alibaba.fastjson.JSONObject'
  import * as module from "@/uni_modules/leven-uts-arcFaceRk356x"

  //组件的引用
  const refLevenArcFacePro = ref<LevenUtsArcFaceRk356xElement | null>(null)

  const config = ref({
    //相机属性,所有的参数都可以不传,不传则按默认的
    camera: {
      // 相机预览旋转角度
      rotation: 270,
      //相机模式,1.前置,0.后置(默认)
      facing: 0,
      // 摄像机预览圆角,默认:0
      radius: 50,
      //预览分辨率,默认:[1280,720]
      size: [1280, 720],
      //是否锁定屏幕启动方向,默认:true
      screenLocked: true,
      //是否回调预览数据,默认:false
      isPreviewFrame: false,
      //预览数据回调时间,单位:毫秒,为0不做限制,有数据就回调
      previewTime: 0
    },
    // 视频检测配置,所有参数都可以不传,不传则按默认的
    video: {
      // 视频检测角度,可接收参数,0,90,180,270,360(默认)
      orient: 360,
      // 人脸框是否处于X反向状态,如果未设置该参数人脸框和人脸处于反向请将该参数设置为true
      isContraryX: false,
      // 人脸框是否处于Y反向状态,如果未设置该参数人脸框和人脸处于反向请将该参数设置为true
      isContraryY: false,
      // 识别阈值(默认:0.8)
      similar: 0.9,
      // 是否进行活体检测(默认为true)
      liveness: true,
      //识别是否展示面部信息(默认为true)
      showFaceInfo: false,
      //活体检测阈值设置
      livenessParams: {
        //可见光活体检测阈值,默认:0.5
        rgb: 0.5,
        //红外活体检测阈值,默认:0.7
        ir: 0.7
      },
      //是否显示人脸上方识别状态提示,默认:true
      showFaceResultNotice: true,
      //人脸识别尺寸,超过该尺寸才识别,否则不识别,可根据识别成功后返回的人脸尺寸进行调整,默认:0,不做人脸尺寸识别
      faceSize: 300,
      //是否返回失败的人脸图片的base64数据,默认:false
      isCompareImageBase64: true
    }
  })

  function register() {
    let options = {
      // 注册后保存的id(可以不传该参数,默认时间戳)
      id: "123",
      //注册后保存的名字(可以不传该参数,默认时间戳)
      name: "leven",
      //同一人脸是否可以多次注册
      registerMultiple: false,
    };
    let params : JSONObject = JSONObject.parse(JSON.stringify(options)) as JSONObject
    refLevenArcFacePro.value?.register(params);
  }

  function switchCamera() {
    refLevenArcFacePro.value?.switchCamera();
  }

  function stop() {
    refLevenArcFacePro.value?.stop();
  }

  function start() {
    refLevenArcFacePro.value?.start();
  }

  function closeFace() {
    refLevenArcFacePro.value?.closeFace();
  }

  function openFace() {
    refLevenArcFacePro.value?.openFace();
  }

  function getCameraData() {
    refLevenArcFacePro.value?.getCameraData();
  }

  function onError(e : JSONObject) {
    console.log(e)
  }

  function onEvent(e : JSONObject) {
    console.log(e)
  }

  function onEventMethod(e : JSONObject) {
    console.log(e)
  }
</script>

<style>

</style>