Javascript 是否显示网络摄像头提要、捕获图像并将其保存在node.js中?

Javascript 是否显示网络摄像头提要、捕获图像并将其保存在node.js中?,javascript,node.js,Javascript,Node.js,我正在尝试建立一个面部识别模块,以便在我的项目中使用它。该模块稍后将在electron.js中用于构建跨平台应用程序 基本思想是: 用户将看到一个显示其网络摄像头提要的网页。他/她可以单击捕获按钮,将图像保存在服务器端。这将重复多次,以获得训练数据来训练面部识别模型。我使用名为“节点网络摄像头”的第三方npm模块实现了图像捕获部分: const nodeWebCam = require('node-webcam'); const fs = require('fs'); const app = r

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

基本思想是:

用户将看到一个显示其网络摄像头提要的网页。他/她可以单击捕获按钮,将图像保存在服务器端。这将重复多次,以获得训练数据来训练面部识别模型。我使用名为“节点网络摄像头”的第三方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....");
});
const nodeWebCam=require('node-webcam');
常数fs=要求('fs');
const-app=require('express')();
const path=require('path');
//指定要拍摄的照片的参数
变量选项={
宽度:1280,
身高:720,
质量:100,
延误:1,
保存快照:是的,
输出:“jpeg”,
设备:错误,
callbackReturn:“位置”
};
//使用上述选项创建实例
var webcam=nodeWebCam.create(选项);
//捕捉功能,用于捕捉图像并将其以给定名称保存在同名文件夹中
var captureShot=(金额、i、名称)=>{
var path=`./images/${name}`;
//仅当文件夹不存在时创建文件夹
如果(!fs.existsSync(路径)){
fs.mkdirSync(路径);
} 
//捕捉图像
网络摄像头.capture(`./images/${name}/${name}${i}.${options.output}`,,(err,data)=>{
如果(!err){
console.log('Image created')
}
控制台日志(err);
i++;
如果(i){
res.sendFile(path.join(uu dirname,'index.html');
});
app.listen(3000,()=>{
console.log(“在端口3000处侦听…”);
});

但是,在这部分之后,我迷路了。我不知道如何让live feed显示在用户看到的网页上。而且,我后来意识到这是一个服务器端代码,无法调用captureShot()来自客户端的函数。任何帮助都将不胜感激。

将捕获快照转化为承诺,然后在路由中渲染。我们将设置一个运行函数的路由,然后使用HTML字符串返回图像路径。我不知道如何从函数返回数据以制作图像。但假设它返回准确的path、 您可以解析回调的路径

您还需要创建一个由express提供服务的静态目录

const nodeWebCam=require('node-webcam');
常数fs=要求('fs');
const-app=require('express')();
const path=require('path');
app.use(express.static('images')//要提供服务的图像文件夹
//现在我们可以说localhost:3000/image.jpg
//指定要拍摄的照片的参数
变量选项={
宽度:1280,
身高:720,
质量:100,
延误:1,
保存快照:是的,
输出:“jpeg”,
设备:错误,
callbackReturn:“位置”
};
//使用上述选项创建实例
var webcam=nodeWebCam.create(选项);
//捕捉功能,可以捕捉到动态对象,然后可以呈现页面。并在拍照后为用户动态设置一个
/>

我举了一个承诺的例子,然后用一个静态目录和express。你可以理解我的意思

函数创建(){
返回新承诺(解决=>{
如果(真){
解决('https://example.com/image.jpg')
}否则{
拒绝('错误')
}
})
}
创建()
。然后((响应)=>{
console.log(``)
})
.catch((错误)=>{
//错误
console.log(错误)

})
我正在开发一个基于Puppeter(无头谷歌chrome)的解决方案,该解决方案超级便携,视频流传输速度可以接受(40fps 800x600帧)。安装和使用非常方便,我已经在使用它在基于gtk、cairo、opengl和qt的桌面应用程序上捕获视频、音频和桌面,没有任何问题

我对opencv很熟悉,但是新用户很难安装基于opencv的库。我的项目不需要任何本机依赖关系或客户机-服务器通信,尽管它对于嵌入小型设备(Puppeter大小约80mb)来说可能是非轻量级的。欢迎反馈!谢谢

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....");
});