显示网络摄像头提要,捕获图像并将其本地保存在node.js中?

时间:2018-10-03 16:58:52

标签: javascript node.js

我正在尝试构建一个面部识别模块以在我的项目中使用它。此模块稍后将在electron.js中用于构建跨平台应用程序。

基本思想是:

向用户显示一个显示其网络摄像头供稿的网页。他/她可以单击捕获按钮,这会将图像保存在服务器端。这将重复多次以获取训练数据以训练面部识别模型。我使用名为“ node-webcam”的第三方npm模块实现了图像捕获部分:

const nodeWebCam = require('node-webcam');
const fs = require('fs');
const app = require('express')();
const path = require('path');

// specifying parameters for the pictures to be taken
var options = {
    width: 1280,
    height: 720, 
    quality: 100,
    delay: 1,
    saveShots: true,
    output: "jpeg",
    device: false,
    callbackReturn: "location"
};

// create instance using the above options
var webcam = nodeWebCam.create(options);

// capture function that snaps <amount> images and saves them with the given name in a folder of the same name
var captureShot = (amount, i, name) => {
    var path = `./images/${name}`;

    // create folder if and only if it does not exist
    if(!fs.existsSync(path)) {
        fs.mkdirSync(path);
    } 

    // capture the image
    webcam.capture(`./images/${name}/${name}${i}.${options.output}`, (err, data) => {
        if(!err) {
            console.log('Image created')
        }
        console.log(err);
        i++;
        if(i <= amount) {
            captureShot(amount, i, name);
        }
    });  
};

// call the capture function
captureShot(30, 1, 'robin');

app.get('/', (req, res) => {
    res.sendFile(path.join(__dirname, 'index.html'));
});

app.listen(3000, () => {
    console.log("Listening at port 3000....");
});

但是,这部分之后我迷路了。我不知道如何将实时供稿显示在用户看到的网页上。另外,我后来意识到这是服务器端代码,无法从客户端调用captureShot()函数。任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:0)

将捕获的镜头转换为诺言,然后在路线中进行渲染。我们将设置一个运行函数的路由,然后使用HTML字符串将路径返回到图像。我不知道如何从您的函数返回数据以制作图像。但是,假设它返回的确切路径,则可以解析回调的路径。

您还需要创建一个由express服务的静态目录。因此,您可以使用http://localhost:3000/myimage.jpg

const nodeWebCam = require('node-webcam');
const fs = require('fs');
const app = require('express')();
const path = require('path');

app.use(express.static('images')) // images folder to be served
// Now we can just say localhost:3000/image.jpg

// specifying parameters for the pictures to be taken
var options = {
    width: 1280,
    height: 720, 
    quality: 100,
    delay: 1,
    saveShots: true,
    output: "jpeg",
    device: false,
    callbackReturn: "location"
};

// create instance using the above options
var webcam = nodeWebCam.create(options);

// capture function that snaps <amount> images and saves them with the given name in a folder of the same name
var captureShot = (amount, i, name) => {
 // Make sure this returns a real url to an image.
 return new Promise(resolve => {
    var path = `./images/${name}`;

    // create folder if and only if it does not exist
    if(!fs.existsSync(path)) {
        fs.mkdirSync(path);
    } 

    // capture the image
    webcam.capture(`./images/${name}/${name}${i}.${options.output}`, (err, data) => {
        if(!err) {
            console.log('Image created')
        }
        console.log(err);
        i++;
        if(i <= amount) {
            captureShot(amount, i, name);
        }
        resolve('/path/to/image.jpg')
    }); 
 })

};

// call the capture function


app.get('/', (req, res) => {
    captureShot(30, 1, 'robin');
      .then((response) => { 
        // Whatever we resolve in captureShot, that's what response will contain
         res.send('<img src="${response}"/>')
      })
});

app.listen(3000, () => {
    console.log("Listening at port 3000....");
});

如果您要设计具有特定动态内容的页面。使用带有express的模板引擎,例如EJS。 http://ejs.co然后,您可以使用动态对象渲染页面。并在拍照后为用户动态设置<img src=<%= image %>/>

我举了一个Promise的例子,然后使用带有express的静态目录。您可以了解我在说什么。

function create() {
  return new Promise(resolve => {
     if (true) {
        resolve('https://example.com/image.jpg')
     } else {
        reject('Error')
     }
  })
}

create()
  .then((response) => {
     console.log(`<img src="${response}"/>`)
  })
  .catch((error) => {
     // Error
     console.log(error)
  })

答案 1 :(得分:0)

我正在研究基于puppeteer(无头的Google chrome)的解决方案,该解决方案具有超强的便携性,并且可以以可接受的速度(40fps 800x600帧)流式传输视频。它非常易于安装和使用,并且我已经使用它来捕获基于gtk,cairo,opengl和qt的桌面应用程序上的视频,音频和桌面,而没有任何问题。

https://www.npmjs.com/package/camera-capture

我对opencv很熟悉,但是基于此的库确实很难被新用户安装。我的项目不需要任何本地依赖项或客户端-服务器通信,尽管它对于嵌入小型设备(伪造者的大小约为80mb)而言可能是非轻量级的。欢迎反馈!谢谢

相关问题