# FaceHarmonyDemo **Repository Path**: ios_7/face-harmony-demo ## Basic Information - **Project Name**: FaceHarmonyDemo - **Description**: No description available - **Primary Language**: Unknown - **License**: Not specified - **Default Branch**: main - **Homepage**: None - **GVP Project**: No ## Statistics - **Stars**: 0 - **Forks**: 0 - **Created**: 2025-12-27 - **Last Updated**: 2026-01-01 ## Categories & Tags **Categories**: Uncategorized **Tags**: None ## README # 鸿蒙 人脸识别、活体识别、离线识别 主要功能: - 本地离线识别,无需网络 - 活体识别 - 相机录入人脸、相机识别人脸 - 本地图片/远程图片录入 - 人脸库管理 - 从人脸库里搜索某个图片 - 同时识别多人 - 判断两张图片是否是同一人 - 实时识别人脸信息(包含人脸数、人脸位置、年龄、性别、人种、眼睛张开、表情、摇头等等) ### 定制或商务请联系QQ252797991 ### 集成步骤 1. 安装依赖 ``` ohpm i @wrs/module_face ``` 2. 配置权限 ``` "requestPermissions": [ { "name": "ohos.permission.CAMERA", "reason": '$string:camera_permission_reason', "usedScene": {"abilities": ["EntryAbility"]} }, { "name": "ohos.permission.INTERNET" }, { "name" : "ohos.permission.STORE_PERSISTENT_DATA", "usedScene": { "abilities": [ "EntryAbility" ], "when":"always" } }, { "name": "ohos.permission.APP_TRACKING_CONSENT", "reason": "$string:permission_app_tracking_content", "usedScene": { "abilities": [ "EntryAbility" ], "when": "inuse" } } ] ``` ### 接口 引入变量 ``` import * as FaceEngine from "@wrs/module_face" ``` - 激活引擎 ``` // demo里演示调试的激活码仅用来开发测试使用,一周左右会失效,请联系QQ252797991获取正式激活码 let activeCode = "xxxxx" let config = {} as FaceEngine.HFFeatureHubConfiguration config.searchThreshold = 0.48 // 搜索相似度大于searchThreshold就认为是同一个人 let result = await FaceEngine.activeEngine(this.getUIContext().getHostContext()!, activeCode, config) if (result == FaceEngine.code_ok) { this.getUIContext().getPromptAction().showToast({ message: "激活成功", duration: 2000, bottom: 80 }) } else { let str = "激活码错误" switch (result) { case FaceEngine.code_pkg_error: str = "包名错误" break case FaceEngine.code_expiretime_error: str = "激活码过期" break case FaceEngine.code_devicefinger_error: str = "设备指纹不匹配" break case FaceEngine.code_platform_error: str = "平台类型不对" break } this.getUIContext().getPromptAction().showToast({ message: "激活失败:" + result + " " + str , duration: 2000, bottom: 80 }) } ``` - 获取设备指纹 ``` let deviceFinger = await FaceEngine.getDeviceFinger(this.getUIContext().getHostContext()!) ``` - 注册图片 1. image.PixelMap注册 ``` FaceEngine.registerImageWithPixelMap(pixelMap).then((id) => { this.getUIContext().getPromptAction().showToast({ message: "注册成功:" + id, duration: 2000, bottom: 80 }) }).catch((error: BusinessError) => { hilog.error(0, this.tag, "注册失败:" + JSON.stringify(error)) this.getUIContext().getPromptAction().showToast({ message: "注册失败:" + JSON.stringify(error), duration: 2000, bottom: 80 }) }) }).catch((error: BusinessError) => { hilog.error(0, this.tag, "注册失败:" + JSON.stringify(error)) this.getUIContext().getPromptAction().showToast({ message: "注册失败:" + JSON.stringify(error), duration: 2000, bottom: 80 }) }) ``` 2. url注册,支持本地图片和云端图片 ``` let url = "/xxx/xxx/xxx.jpg" // 本地图片,如:/xxxx/xx/xxx.jpg 网络图片,如:https://sss/sss.jpg FaceEngine.registerImageWithUrl(url).then((id) => { this.getUIContext().getPromptAction().showToast({ message: "注册成功:" + id, duration: 2000, bottom: 80 }) }).catch((error: BusinessError) => { hilog.error(0, this.tag, "注册失败:" + JSON.stringify(error)) this.getUIContext().getPromptAction().showToast({ message: "注册失败:" + JSON.stringify(error), duration: 2000, bottom: 80 }) }) }).catch((error: BusinessError) => { hilog.error(0, this.tag, "注册失败:" + JSON.stringify(error)) this.getUIContext().getPromptAction().showToast({ message: "注册失败:" + JSON.stringify(error), duration: 2000, bottom: 80 }) }) ``` - 根据图片搜索人脸库的用户 1. image.PixelMap注册 ``` FaceEngine.searchImageWithPixelMap(pixelMap).then((result) => { if (result) { if (result.length > 0) { this.getUIContext().getPromptAction().showToast({ message: "搜索成功,用户ID:" + result[0].id + " 相似度:" + result[0].confidence, duration: 2000, bottom: 80 }) } else { this.getUIContext().getPromptAction().showToast({ message: "搜索成功,没有搜索到这个用户", duration: 2000, bottom: 80 }) } } else { this.getUIContext().getPromptAction().showToast({ message: "搜索失败:图片有问题", duration: 2000, bottom: 80 }) } }).catch((error: BusinessError) => { hilog.error(0, this.tag, "搜索失败:" + JSON.stringify(error)) this.getUIContext().getPromptAction().showToast({ message: "搜索失败:" + JSON.stringify(error), duration: 2000, bottom: 80 }) }) ``` 2. url搜索,支持本地图片和云端图片 ``` let filePath: string = context.resourceDir + '/test.jpg'; FaceEngine.searchImageWithUrl(filePath).then((result) => { if (result) { if (result.length > 0) { this.getUIContext().getPromptAction().showToast({ message: "搜索成功,用户ID:" + result[0].id + " 相似度:" + result[0].confidence, duration: 2000, bottom: 80 }) } else { this.getUIContext().getPromptAction().showToast({ message: "搜索成功,没有搜索到这个用户", duration: 2000, bottom: 80 }) } } else { this.getUIContext().getPromptAction().showToast({ message: "搜索失败:图片有问题", duration: 2000, bottom: 80 }) } }).catch((error: BusinessError) => { hilog.error(0, this.tag, "搜索失败:" + JSON.stringify(error)) this.getUIContext().getPromptAction().showToast({ message: "搜索失败:" + JSON.stringify(error), duration: 2000, bottom: 80 }) }) ``` - 对比两张图片是否是同一个人 1. PixelMap对比 ``` FaceEngine.compareImageWithPixelMap(this.image1 as PixelMap, this.image2 as PixelMap).then((result) => { let title = "是同一个人" if (result.similarity > 0.48) { title = "是同一个人" } else { title = "是同一个人" } this.getUIContext().getPromptAction().showToast({ message: title + ",相似度:" + result.similarity, duration: 2000, bottom: 80 }) }).catch((error: BusinessError) => { this.getUIContext().getPromptAction().showToast({ message: "对比失败:" + JSON.stringify(error), duration: 2000, bottom: 80 }) }) ``` 2. url对比 ``` FaceEngine.compareImageWithUrl("/xxx/xxx.jpg", "/xx/xxxxxxxx.jpg").then((result) => { let title = "是同一个人" if (result.similarity > 0.48) { title = "是同一个人" } else { title = "是同一个人" } this.getUIContext().getPromptAction().showToast({ message: title + ",相似度:" + result.similarity, duration: 2000, bottom: 80 }) }).catch((error: BusinessError) => { this.getUIContext().getPromptAction().showToast({ message: "对比失败:" + JSON.stringify(error), duration: 2000, bottom: 80 }) }) ``` - 获取人脸库里所有用户 ``` FaceEngine.FeatureHubGetExistingIds().then((array)=>{ this.dataArray = array }) ``` - 删除用户 ``` FaceEngine.FeatureFaceRemove(id).then((result)=>{ if (result > 0) { // 删除成功 } else { // 删除失败 } }).catch((error: BusinessError)=>{ }) ``` - 获取图片人脸的位置、年龄、性别、睁眼、摇头等数据 ``` FaceEngine.getFaceInfoWithPixelMap(pixelMap).then((result) => { if (result) { hilog.error(0, this.tag, "获取成功:" + JSON.stringify(result)) this.getUIContext().getPromptAction().showToast({ message: "获取成功:" + JSON.stringify(result), duration: 2000, bottom: 80 }) } else { this.getUIContext().getPromptAction().showToast({ message: "识别失败:图片有问题", duration: 2000, bottom: 80 }) } }).catch((error: BusinessError) => { this.getUIContext().getPromptAction().showToast({ message: "识别失败:" + JSON.stringify(error), duration: 2000, bottom: 80 }) }) ``` - 相机组件 ``` @State readyRegister: boolean = false // 是否用当前视频帧图片注册到数据库 @State readyCompare: boolean = false // 是否搜索数据库 @State rgbLiveness: boolean = false // 是否进行rgb活体识别 @State quality: boolean = false // 是否进行图片质量检测 @State faceMask: boolean = false // 是否进行识别口罩 @State faceInteractionState: boolean = false // 是否识别交互状态 @State faceInteractionsActions: boolean = false // 是否识别动作 @State faceAttribute: boolean = false // 是否识别人脸属性 @State faceEmotion: boolean = false // 是否识别人脸表情 private controller: CameraViewController = new CameraViewController() CameraView({ readyRegister: this.readyRegister, readyCompare: this.readyCompare, rgbLiveness: this.rgbLiveness, quality: this.quality, faceMask: this.faceMask, faceInteractionState: this.faceInteractionState, faceInteractionsActions: this.faceInteractionsActions, faceAttribute: this.faceAttribute, faceEmotion: this.faceAttribute, controller: this.controller, cameraPosition: camera.CameraPosition.CAMERA_POSITION_FRONT, pixelMapCallback: (image) => { // this.src = image }, videoCallback: (result, viewWidth, viewHeight) => { // 识别搜索到用户回调 this.proceVideoCallback(result) } }) ```