feat: fix faceId loop error caused by async (#3651)

This commit is contained in:
DacongDA 2025-03-11 21:03:04 +08:00 committed by GitHub
parent 9610ce5b8c
commit 30789138e2
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -289,20 +289,20 @@ const FaceRecognitionModal = (props) => {
<p>{i18next.t("general:Click to Upload")}</p> <p>{i18next.t("general:Click to Upload")}</p>
</Dragger > </Dragger >
{ {
modelsLoaded ? <Button style={{width: "100%"}} onClick={() => { modelsLoaded ? <Button style={{width: "100%"}} onClick={async() => {
let maxScore = 0; let maxScore = 0;
files.forEach((file, fileIndex) => { for (const file of files) {
const fileIndex = files.indexOf(file);
const img = new Image(); const img = new Image();
img.src = file.base64; img.src = file.base64;
faceapi.detectAllFaces(img, new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks().withFaceDescriptors().then(res2 => { const faceIds = await faceapi.detectAllFaces(img, new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks().withFaceDescriptors();
if (res2[0]?.detection.score > 0.9 && res2[0]?.detection.score > maxScore) { if (faceIds[0]?.detection.score > 0.9 && faceIds[0]?.detection.score > maxScore) {
maxScore = res2[0]?.detection.score; maxScore = faceIds[0]?.detection.score;
setCurrentFaceId(res2[0]); setCurrentFaceId(faceIds[0]);
setCurrentFaceIndex(fileIndex); setCurrentFaceIndex(fileIndex);
} }
}); }
}); if (maxScore < 0.9) {
if (currentFaceId?.length === 0) {
message.error(i18next.t("login:Face recognition failed")); message.error(i18next.t("login:Face recognition failed"));
} }
}}> {i18next.t("application:Generate faceId")}</Button> : null }}> {i18next.t("application:Generate faceId")}</Button> : null