【问题标题】:ReactJS - Faceapi.js Error: toNetInput - expected media type issueReactJS - Faceapi.js 错误:toNetInput - 预期的媒体类型问题
【发布时间】:2022-02-20 18:52:25
【问题描述】:

我是 reactjs 的新手,我是构建一个页面/组件来上传图像并通过 faceapi.js 运行它以显示面部标志点。我正在尝试将这些点放在一个数组中,以便稍后用于其他过程。

网络摄像头版本有效,然后我尝试制作基于照片的版本;但是,一直在努力解决以下问题。

我得到的当前错误:

toNetInput.ts:39 
    
   Uncaught (in promise) Error: toNetInput - expected media to be of type HTMLImageElement | HTMLVideoElement | HTMLCanvasElement | tf.Tensor3D, or to be an element id

我也尝试从 Dom 中的图像元素定义它,但没有奏效。我觉得我错过了一些非常简单但又太盲目的东西......

import React, { useRef , useEffect, useState} from "react";    
import * as faceapi from 'face-api.js'; 

//this one was for previous tests with img html element
import * as inputImage from '../assets/images/sah.png';



function CompName() { 
    loadModels();
    const [file, setFile] = useState([])
    const [canvas, setcanvas] = useState([])

    function fileChangeHandler(event){
      
        setFile(event);
            }
    
    useEffect(() => {
        
       setcanvas(faceapi.createCanvas(file))
        console.log('file 1 ', file.size);
        document.body.append(canvas) 
        if(file.size>0){
        detectF();
        console.log('file ', file);
    }
      }, [file])





     async function loadModels(){
        Promise.all([ 
        await faceapi.nets.tinyFaceDetector.loadFromUri('/models') ,
        await faceapi.nets.ssdMobilenetv1.loadFromUri('/models'),
        await faceapi.nets.faceLandmark68Net.loadFromUri('/models'),
        await  faceapi.nets.faceRecognitionNet.loadFromUri('/models'),
        await  faceapi.nets.faceExpressionNet.loadFromUri('/models') 
        ]).then((values) => {

            console.log('models loaded');
        });
      } 





    const displaySize = {
        width: file.width,
        height: file.height
      }


    faceapi.matchDimensions(canvas, displaySize)   
 
    async function detectF(){
        const detections = await faceapi.detectAllFaces(file, new faceapi.TinyFaceDetectorOptions()).withFaceLandmarks()
        const resizedDetections = faceapi.resizeResults(detections, displaySize)
      
      // faceapi.draw.drawDetections(canvas, resizedDetections)
      faceapi.draw.drawFaceLandmarks(canvas, resizedDetections)
      // faceapi.draw.drawFaceExpressions(canvas, resizedDetections)
      const landmarkPositions = resizedDetections[0].landmarks.positions;
 
    console.log({detections});

    }





return (  
    <> 
<div >
<input type="file" onChange={(e) => fileChangeHandler(e.target.files[0])} />
{/* <img src={inputImage} onLoad={handleImageLoad} /> */} 
</>
    );
}

export default CompName;

任何帮助都是有价值的,在此先感谢。

以下是我浏览过的可能会有所帮助的资源。 Trying detect faces on image using face-api.js.Getting error: Unhandled Rejection (Error): createCanvasFromMedia -media has not finished loading yet

face-api.js load image file from disk

Trying detect faces on image using face-api.js.Getting error: Unhandled Rejection (Error): createCanvasFromMedia -media has not finished loading yet

https://github.com/k3nnet/image_recognition/blob/master/src/app/image-component/image-component.component.ts

https://github.com/justadudewhohacks/face-api.js

https://medium.com/@kkmphele/image-facial-recognition-in-javascript-using-convolutional-neural-networks-with-face-api-js-7ce8a8c7493c

【问题讨论】:

    标签: reactjs p5.js face-api tfjs-node


    【解决方案1】:

    const img = await faceapi.bufferToImage(file);

    首先,你必须使用faceapi缓冲函数来传递一张图片

    删除承诺 .all 并设置一个状态,让我们知道何时加载模块。

    加载模块后,我们可以让用户选择上传图片。

    这里是沙盒链接:https://codesandbox.io/s/inspiring-germain-vlyhc0?file=/src/App.js

    Working Image

    如果您需要进一步的帮助,请告诉我。

    【讨论】:

    • 谢谢! Codesandbox 没有显示地标(我认为是隐藏在 Firefox 的后面),但我最终设法让它在我的本地工作。我正在尝试将数组 [landmarkList] 传递给 p5 草图。我在沙箱里加了代码codesandbox.io/s/pedantic-hellman-sqk136?file=/src/App.js 不行,我觉得是关于执行顺序的问题?
    • 绘制草图需要时间,您必须等待几秒钟,而且我没有看到您在此处尝试实现的代码更改?
    • 对不起,由于某种原因它没有保存我的更改,再次更新。基本上试图将数组/地标列表传递给 p5wrapper。
    • 你可以像这样得到你的地标const landmarkPositions = await faceapi.detectFaceLandmarks(img);
    • 是的,我已经将它们放在一个数组中。我想要做的是将它作为道具传递给包装器
    猜你喜欢
    • 2021-03-01
    • 2019-12-13
    • 2022-01-21
    • 1970-01-01
    • 2023-04-06
    • 2014-07-01
    • 1970-01-01
    • 2014-09-10
    • 2020-05-12
    相关资源
    最近更新 更多