HarmonyOS 鸿蒙Next 人脸活体检测接口获取不到人脸数据
HarmonyOS 鸿蒙Next 人脸活体检测接口获取不到人脸数据
参考官方链接 https://developer.huawei.com/consumer/cn/doc/harmonyos-references-V5/vision-interactive-liveness-V5#section887319119114
伙伴您好,
在官网Demo基础之上,做一些修改,修改点如下:
第18行增加如下代码
@State result :interactiveLiveness.InteractiveLivenessResult|undefined = undefined
第140行增加如下代码
Image(this.result?.mPixelMap)
.width(260)
.height(260)
.align(Alignment.Center)
.margin({ bottom: 260 })
第236行增加如下代码
this.result = result
---------------------分隔线-----------------------------
全量代码如下
import { common, abilityAccessCtrl, Permissions } from ‘@kit.AbilityKit’;
import { interactiveLiveness } from ‘@kit.VisionKit’;
import { BusinessError } from ‘@kit.BasicServicesKit’;
import { image } from ‘@kit.ImageKit’;
import { hilog } from ‘@kit.PerformanceAnalysisKit’;
@Entry
@Component
struct LivenessIndex {
private context: common.UIAbilityContext = getContext(this) as common.UIAbilityContext;
private array: Array<Permissions> = [“ohos.permission.CAMERA”];
@State actionsNum: number = 0;
@State isSilentMode: string = ‘INTERACTIVE_MODE’;
@State routeMode: string = ‘replace’;
@State resultInfo: interactiveLiveness.InteractiveLivenessResult = {
livenessType: 0
};
@State result :interactiveLiveness.InteractiveLivenessResult|undefined = undefined
@State failResult: Record<string, number | string> = {
‘code’: 1008302000,
‘message’: ‘’
};
build() {
Stack({
alignContent: Alignment.Top
}) {
Column() {
Row() {
Text(‘Select liveness detection mode:’)
.fontSize(18)
.width(‘100%’)
}
Row() {
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
Row() {
Radio({ value: ‘INTERACTIVE_MODE’, group: ‘isSilentMode’ }).checked(true)
.height(24)
.width(24)
.onChange((isChecked: boolean) => {
this.isSilentMode = ‘INTERACTIVE_MODE’
})
Text(‘Interactive’)
.fontSize(16)
}
.width(‘50%’)
.margin({ right: 15 })
Row() {
Radio({ value: <span class="hljs-string">'SILENT_MODE'</span>, group: <span class="hljs-string">'isSilentMode'</span> }).checked(<span class="hljs-literal">false</span>)
.height(<span class="hljs-number">24</span>)
.width(<span class="hljs-number">24</span>)
.onChange((isChecked: boolean) => {
<span class="hljs-keyword">this</span>.isSilentMode = <span class="hljs-string">'SILENT_MODE'</span>;
})
Text(<span class="hljs-string">'Silent'</span>)
.fontSize(<span class="hljs-number">16</span>)
}
.width(<span class="hljs-string">'50%'</span>)
}
.width(<span class="hljs-string">'100%'</span>)
}
}
.margin({ bottom: <span class="hljs-number">30</span> })
Row() {
Text(<span class="hljs-string">'Redirection mode after detection:'</span>)
.fontSize(<span class="hljs-number">18</span>)
.width(<span class="hljs-string">'100%'</span>)
}
Row() {
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
Row() {
Radio({ value: <span class="hljs-string">'replace'</span>, group: <span class="hljs-string">'routeMode'</span> }).checked(<span class="hljs-literal">true</span>)
.height(<span class="hljs-number">24</span>)
.width(<span class="hljs-number">24</span>)
.onChange((isChecked: boolean) => {
<span class="hljs-keyword">this</span>.routeMode = <span class="hljs-string">'replace'</span>
})
Text(<span class="hljs-string">'Replace'</span>)
.fontSize(<span class="hljs-number">16</span>)
}
.width(<span class="hljs-string">'50%'</span>)
.margin({ right: <span class="hljs-number">15</span> })
Row() {
Radio({ value: <span class="hljs-string">'back'</span>, group: <span class="hljs-string">'routeMode'</span> }).checked(<span class="hljs-literal">false</span>)
.height(<span class="hljs-number">24</span>)
.width(<span class="hljs-number">24</span>)
.onChange((isChecked: boolean) => {
<span class="hljs-keyword">this</span>.routeMode = <span class="hljs-string">'back'</span>;
})
Text(<span class="hljs-string">'Back'</span>)
.fontSize(<span class="hljs-number">16</span>)
}
.width(<span class="hljs-string">'50%'</span>)
}
.width(<span class="hljs-string">'100%'</span>)
}
}
.margin({ bottom: <span class="hljs-number">30</span> })
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.isSilentMode == <span class="hljs-string">'INTERACTIVE_MODE'</span>) {
Row() {
Flex({ direction: FlexDirection.Row, justifyContent: FlexAlign.Start, alignItems: ItemAlign.Center }) {
Text(<span class="hljs-string">'Actions:'</span>)
.fontSize(<span class="hljs-number">18</span>)
.width(<span class="hljs-string">'25%'</span>)
TextInput({
placeholder: <span class="hljs-keyword">this</span>.actionsNum != <span class="hljs-number">0</span> ? <span class="hljs-keyword">this</span>.actionsNum.toString() : <span class="hljs-string">"Maximum number: 4"</span>
})
.type(InputType.Number)
.placeholderFont({
size: <span class="hljs-number">18</span>,
weight: FontWeight.Normal,
family: <span class="hljs-string">"HarmonyHeiTi"</span>,
style: FontStyle.Normal
})
.fontSize(<span class="hljs-number">18</span>)
.fontWeight(FontWeight.Bold)
.fontFamily(<span class="hljs-string">"HarmonyHeiTi"</span>)
.fontStyle(FontStyle.Normal)
.width(<span class="hljs-string">'65%'</span>)
.onChange((value: string) => {
<span class="hljs-keyword">this</span>.actionsNum = <span class="hljs-built_in">Number</span>(value) as interactiveLiveness.ActionsNumber;
})
}
}
}
}
.margin({ left: <span class="hljs-number">24</span>, top: <span class="hljs-number">80</span> })
.zIndex(<span class="hljs-number">1</span>)
Stack({
alignContent: Alignment.Bottom
}) {
Image(<span class="hljs-keyword">this</span>.result?.mPixelMap)
.width(<span class="hljs-number">260</span>)
.height(<span class="hljs-number">260</span>)
.align(Alignment.Center)
.margin({ bottom: <span class="hljs-number">260</span> })
<span class="hljs-keyword">if</span> (<span class="hljs-keyword">this</span>.resultInfo?.mPixelMap) {
Image(<span class="hljs-keyword">this</span>.resultInfo?.mPixelMap)
.width(<span class="hljs-number">260</span>)
.height(<span class="hljs-number">260</span>)
.align(Alignment.Center)
.margin({ bottom: <span class="hljs-number">260</span> })
Circle()
.width(<span class="hljs-number">300</span>)
.height(<span class="hljs-number">300</span>)
.fillOpacity(<span class="hljs-number">0</span>)
.strokeWidth(<span class="hljs-number">60</span>)
.stroke(Color.White)
.margin({ bottom: <span class="hljs-number">250</span>, left: <span class="hljs-number">0</span> })
}
Text(<span class="hljs-keyword">this</span>.resultInfo.mPixelMap ?
<span class="hljs-string">'Detection succeeded'</span> :
<span class="hljs-keyword">this</span>.failResult.code != <span class="hljs-number">1008302000</span> ?
<span class="hljs-string">'Detection failed'</span> :
<span class="hljs-string">''</span>)
.width(<span class="hljs-string">'100%'</span>)
.height(<span class="hljs-number">26</span>)
.fontSize(<span class="hljs-number">20</span>)
.fontColor(<span class="hljs-string">'#000000'</span>)
.fontFamily(<span class="hljs-string">'HarmonyHeiTi'</span>)
.margin({ top: <span class="hljs-number">50</span> })
.textAlign(TextAlign.Center)
.fontWeight(<span class="hljs-string">'Medium'</span>)
.margin({ bottom: <span class="hljs-number">240</span> })
<span class="hljs-keyword">if</span>(<span class="hljs-keyword">this</span>.failResult.code != <span class="hljs-number">1008302000</span>) {
Text(<span class="hljs-keyword">this</span>.failResult.message as string)
.width(<span class="hljs-string">'100%'</span>)
.height(<span class="hljs-number">26</span>)
.fontSize(<span class="hljs-number">16</span>)
.fontColor(Color.Gray)
.textAlign(TextAlign.Center)
.fontFamily(<span class="hljs-string">'HarmonyHeiTi'</span>)
.fontWeight(<span class="hljs-string">'Medium'</span>)
.margin({ bottom: <span class="hljs-number">200</span> })
}
Button(<span class="hljs-string">"Start detection"</span>, { type: ButtonType.Normal, stateEffect: <span class="hljs-literal">true</span> })
.width(<span class="hljs-number">192</span>)
.height(<span class="hljs-number">40</span>)
.fontSize(<span class="hljs-number">16</span>)
.backgroundColor(<span class="hljs-number">0x317aff</span>)
.borderRadius(<span class="hljs-number">20</span>)
.margin({
bottom: <span class="hljs-number">56</span>
})
.onClick(() => {
<span class="hljs-keyword">this</span>.privateStartDetection();
})
}
.height(<span class="hljs-string">'100%'</span>)
}
}
onPageShow() {
this.resultRelease();
// this.getDetectionResultInfo();
}
// Route to the face liveness detection control.
private privateRouterLibrary() {
// let routerOptions: interactiveLiveness.InteractiveLivenessConfig = {
// isSilentMode: this.isSilentMode as interactiveLiveness.DetectionMode,
// routeMode: this.routeMode as interactiveLiveness.RouteRedirectionMode,
// actionsNum: this.actionsNum
// }
<span class="hljs-keyword">let</span> routerOptions: interactiveLiveness.InteractiveLivenessConfig= {
isSilentMode: <span class="hljs-string">"INTERACTIVE_MODE"</span> as interactiveLiveness.DetectionMode,
routeMode: <span class="hljs-string">"back"</span> as interactiveLiveness.RouteRedirectionMode,
actionsNum: <span class="hljs-number">3</span> as interactiveLiveness.ActionsNumber
};
<span class="hljs-comment">// interactiveLiveness.startLivenessDetection(routerOptions).then((DetectState: boolean) => {</span>
<span class="hljs-comment">// console.info('LivenessCollectionIndex', `Succeeded in jumping.`);</span>
<span class="hljs-comment">// }).catch((err: BusinessError) => {</span>
<span class="hljs-comment">// console.error('LivenessCollectionIndex', `Failed to jump. Code:${err.code},message:${err.message}`);</span>
<span class="hljs-comment">// })</span>
interactiveLiveness.startLivenessDetection(routerOptions, (err: BusinessError, result: interactiveLiveness.InteractiveLivenessResult | <span class="hljs-literal">undefined</span>) => {
<span class="hljs-keyword">if</span>(err.code !== <span class="hljs-number">0</span> && !result) {
hilog.error(<span class="hljs-number">0x0001</span>, <span class="hljs-string">"LivenessCollectionIndex"</span>, `Failed to detect. Code:${err.code},message:${err.message}`);
<span class="hljs-keyword">return</span>;
}
hilog.info(<span class="hljs-number">0x0001</span>, <span class="hljs-string">'LivenessCollectionIndex'</span>, `Succeeded <span class="hljs-keyword">in</span> detecting result:${result}`);
<span class="hljs-keyword">this</span>.result = result
})
}
// Verify the camera permission.
private privateStartDetection() {
abilityAccessCtrl.createAtManager().requestPermissionsFromUser(this.context, this.array).then((res) => {
for (let i = 0; i < res.permissions.length; i++) {
if (res.permissions[i] === “ohos.permission.CAMERA” && res.authResults[i] === 0) {
this.privateRouterLibrary();
}
}
})
}
// Obtain the detection result.
private getDetectionResultInfo() {
// Resources are released after the getInteractiveLivenessResult API calling is complete.
let resultInfo = interactiveLiveness.getInteractiveLivenessResult();
resultInfo.then(data => {
this.resultInfo = data;
}).catch((err: BusinessError) => {
this.failResult = {
‘code’: err.code,
‘message’: err.message
}
})
}
// Result release.
private resultRelease() {
this.resultInfo = {
livenessType: 0
}
this.failResult = {
‘code’: 1008302000,
‘message’: ‘’
}
}
}
针对HarmonyOS鸿蒙Next人脸活体检测接口获取不到人脸数据的问题,以下是一些可能的解决方案:
- 检查权限配置:确保你的应用在
module.json5
文件中已经正确配置了CAMERA
权限,并且用户已经授予了该权限。这是进行人脸活体检测的前提。 - 检查接口调用:确保你正确地调用了人脸活体检测接口,并且传入了正确的参数。例如,检查
InteractiveLivenessConfig
配置是否正确,包括检测模式、跳转模式等。 - 检查设备环境:人脸活体检测对设备环境有一定的要求,如摄像头像素、光线环境等。尝试在不同的设备或光线环境下进行检测,看是否能成功获取人脸数据。
- 查看日志和错误信息:检查应用的日志文件,看是否有关于人脸活体检测的错误信息。这些信息可以帮助你定位问题所在。
如果以上方法都无法解决问题,可能是系统或接口本身的问题。此时,建议联系官网客服进行进一步的咨询和排查。官网地址是:https://www.itying.com/category-93-b0.html。