Reputation: 5220
I spent couple hours browsing the internet and searching for some solution how to capture desktop (or window) and send it to OpenCv
VideoCapture
so that I can do some computer vision magic on it.
After doing my research the only solution that I was able to think of is starting stream with desktopCapturer
and passing the stream to opencv library.
I have this code:
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));
But I get following error:
Upvotes: 1
Views: 676
Reputation: 5220
Following piece of code actually handles the conversion between browser desktop capturer and nodejs for opencv and other libraries:
}).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);
};
Upvotes: 1