反应,如何在初始化之前调用函数然后阻止它或以任何适当的方式?

问题描述

我已经为我的 react 组件构建了一个带有 face-api.js 的 javascript 函数,它将返回/控制台我人脸检测器框的宽度和高度。我在几个地方尝试了 console.log,它在模型(人脸识别模型)之前似乎工作正常。

但是当我为人脸检测器编写异步函数来检测人脸和控制台时。它给了我错误-

Unhandled rejection(Reference Error): Cannot access 'handledImage' before initialization  

这里也是屏幕截图。

我不知道如何解决?

有我的代码faceDetector.js

import * as faceapi from "face-api.js";

//passing image from my react compoenent
const faceDetector = (image) => {
    const imageRef = image;

    const loadModels = async () => {
       // models are present in public and they are getting loaded
        const MODEL_URL = process.env.PUBLIC_URL + "/models";
        Promise.all([
            faceapi.nets.tinyFaceDetector.loadFromUri(MODEL_URL),faceapi.nets.faceLandmark68Net.loadFromUri(MODEL_URL),faceapi.nets.faceRecognitionNet.loadFromUri(MODEL_URL),faceapi.nets.faceExpressionNet.loadFromUri(MODEL_URL)
        ])
             // this is rising the issue. I want to call this function after my models loaded so it can detect face
            .then(handleImage)
            .catch((e) => console.error(e));
    };
    loadModels();

    // this function should detect the face from imageRef and should console the size of detector
    const handleImage = async () => {
        const detections = await faceapi
            .detectSingleFace(imageRef,new faceapi.TinyFaceDetectorOptions())
        console.log(`Width ${detections.box._width} and Height ${detections.box._height}`);
    }


}



export {faceDetector}

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)