簡體   English   中英

無法從 face-api.js 中的本地節點 js 服務器加載圖像

[英]Not able to load image from local node js server in face-api.js

我嘗試了以下兩種方法 o 從 face-api.js 中的 node js 中的 localserver 獲取圖像並注釋以下代碼的輸出

我應該錯過什么或者我需要嘗試不同的方式請幫助..

第一次嘗試

   var image = fs.readFileSync(path);
            console.log('Exists ' + typeof (image)) // Exists object


            const image = await faceapi.fetchImage(image)
                .then(res =>{console.log(res)})
                .catch(e=> console.log("Error e "+e)) //Error e Type Error: Only absolute URLs are supported

我想從本地服務器獲取圖像並訓練模型,但我無法獲取圖像。 完整的錯誤如下:-

(node:25194) UnhandledPromiseRejectionWarning: TypeError: Only absolute URLs are supported
    at getNodeRequestOptions (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/node-fetch/lib/index.js:1299:9)
    at /home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/node-fetch/lib/index.js:1404:19
    at new Promise (<anonymous>)
    at fetch (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/node-fetch/lib/index.js:1401:9)
    at Object.<anonymous> (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/tfjs-image-recognition-base/build/commonjs/dom/fetchOrThrow.js:12:42)
    at step (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/tslib/tslib.js:136:27)
    at Object.next (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/tslib/tslib.js:117:57)
    at /home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/tslib/tslib.js:110:75
    at new Promise (<anonymous>)
    at Object.__awaiter (/home/milind/Desktop/FaceApi/Face-Detection-JavaScript/node_modules/tslib/tslib.js:106:16)

ps 我已經檢查過這個face-api.js 從磁盤加載圖像文件

編輯:

Repository
        -temp
          image.png
        server.js  
        -Router
          router.js <- here i'm having the below router code

router.post('/upload-file-face',  (req, res) => {
    console.log("Helllo");
    Promise.all([
      faceapi.nets.faceRecognitionNet.loadFromDisk('./weights'),
      faceapi.nets.faceLandmark68Net.loadFromDisk('./weights'),
      faceapi.nets.ssdMobilenetv1.loadFromDisk('./weights'),
    ]).then(async () => {
    tempUpload(req,res, async(err) =>{
        console.log(req.files);
        if(err) {
            return res.end("Error uploading file." + err);
        } 
        var location = "./"+req.files[0].destination
        var imgFile = req.files[0].filename;
        if(!fs.existsSync(fspath.join(location,imgFile))) {
         console.log("Not exists")
      }else{
        console.log('Exists')  //yes Exists
      }

      var pp =fspath.join(location,imgFile);
      console.log("path : "+pp)
        const image =  faceapi.fetchImage(pp)
            .then(res =>{console.log(res)})
            .catch(e=> console.log("Error e " +e)) // Error e TypeError: Only absolute URLs are supported

    res.send("hello");
  })
    })
})

根據文檔faceapi.fetchImage接受路徑,而不是文件內容/緩沖區。

所以,不要做var image = fs.readFileSync(path)你應該這樣做:

try{
    const image = await faceapi.fetchImage(path)
} catch (e){
    console.log("Error e "+e)
}

fetchImage不適用於本地資源,因為“fetch”適用於網絡。

您需要npm install canvas然后簡單地執行以下操作:

const canvas = require('canvas');
faceapi.env.monkeyPatch({ Canvas, Image })
const img = await canvas.loadImage('./img.jpg');
const detections = await faceapi.detectSingleFace(img);

參見另一個討論: https : //stackoverflow.com/a/59828665/5017391

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM