electron.desktopCapturer to cv.VideoCapture [electron,nodejs,opencv4nodejs]

时间:2018-01-14 01:35:57

标签: c++ node.js opencv electron screen

我花了几个小时浏览互联网并搜索一些解决方案如何捕捉桌面(或窗口)并将其发送到OpenCv VideoCapture,以便我可以在其上做一些计算机视觉魔术。< / p>

完成我的研究之后,我能够想到的唯一解决方案是使用desktopCapturer启动流并将流传递给opencv库。

我有这段代码:

const { desktopCapturer } = require('electron');
var cv = require('electron').remote.require('opencv4nodejs');

...some setup...

navigator.mediaDevices.getUserMedia({
  audio: false,
  video: {
    mandatory: {
      chromeMediaSource: 'desktop',
      chromeMediaSourceId: source.id,
      minWidth: 640,
      maxWidth: 640,
      minHeight: 320,
      maxHeight: 320,
    },
  },
})
.then((stream) => {
  console.log('stream ', stream);
  const videoUrl = URL.createObjectURL(stream);
  console.log('videoUrl', videoUrl);
  const capturedVideo = new cv.VideoCapture(videoUrl);
  console.log('captured video', capturedVideo);
})
.catch((error) => console.error(error));

但我得到以下错误:

image

1 个答案:

答案 0 :(得分:1)

以下代码实际上处理了浏览器桌面捕获程序和opencv和其他库的nodejs之间的转换:

}).then((stream) => {
  const video = document.createElement('video');
  video.srcObject = stream;
  video.onloadedmetadata = () => {
  video.play();
  setInterval(() => {
    const canvas = document.createElement('canvas');
    canvas.getContext('2d').drawImage(video, 0, 0, 800, 800);
    canvas.toBlob(blob => {
      toBuffer(blob, function (err, buffer) {
        if (err) throw err;
        // do some magic with buffer
      });
    });
  }, 40);
};