反应,如何在初始化之前调用然后块中的函数或任何正确的方法?
react, how to call a function inside then block before initializing it or any proper way?
我已经为我的 React 组件构建了一个带有 face-api.js 的 javascript 函数,它将 return/console 我的面部检测器框的宽度和高度。我在几个地方尝试了 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}
您需要更改函数声明的顺序。您不能在声明变量之前调用 const
变量。
//passing image from my react component
const faceDetector = (image) => {
const imageRef = image;
// 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}`);
}
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();
}
我已经为我的 React 组件构建了一个带有 face-api.js 的 javascript 函数,它将 return/console 我的面部检测器框的宽度和高度。我在几个地方尝试了 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}
您需要更改函数声明的顺序。您不能在声明变量之前调用 const
变量。
//passing image from my react component
const faceDetector = (image) => {
const imageRef = image;
// 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}`);
}
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();
}