justadudewhohacks / opencv4nodejs

Nodejs bindings to OpenCV 3 and OpenCV 4
MIT License
4.92k stars 812 forks source link

MJPEG streaming via webcam #230

Open asishap007 opened 6 years ago

asishap007 commented 6 years ago

How can I get MJPEG stream using this module ? Is there any option like below link.

http://answers.opencv.org/question/6805/how-to-get-mjpeg-compression-format-from-webcam/

Here code is in Python.

nicholasc commented 6 years ago

I searched through and the CV_FOURCC macro is not present in the library at the moment.

Lucky for us, the CV_FOURCC macro can easily be implemented in javascript. You should be able to do something similar to what's in the link you posted with the VideoCapture class like so:

// javscript version of the CV_FOURCC macro
const fourcc = (c1, c2, c3, c4) => {
  return (c1 & 255) + ((c2 & 255) << 8) + ((c3 & 255) << 16) + ((c4 & 255) << 24);
}

// create the webcam video capture object
const webcam = new cv.VideoCapture(0);

// set the capture props using CV_FOURCC
webcam.set(cv.CAP_PROP_FOURCC, fourcc('M','J','P','G'));
webcam.set(cv.CAP_PROP_FRAME_WIDTH, 1920);
webcam.set(cv.CAP_PROP_FRAME_HEIGHT, 1080);

FYI: The MJPG CV_FOURCC result is apparently 0.

@justadudewhohacks should the CV_FOURCC macro be ported directly to C++ or maybe we can add it to the js lib? I am willing to work on this as my first contribution to the project :)

justadudewhohacks commented 6 years ago

Hi, The CV_FOURCC macro is available as a static function on the VideoWriter object. You can use it like this: const code = cv.VideoWriter.fourcc('MJPG').

nicholasc commented 6 years ago

Absolutely! How did I miss that. Thanks.

asishap007 commented 6 years ago

Thanks guys. Will try that

wbern commented 6 years ago

@asishap007 Since we are still missing good implementation resources for doing this, do share code snippets if you get it to work. I myself am trying to get something like this to work, but opencv only seems to be able to write video to a file and not to memory (for streaming directly via websockets for example). I would be happy if I was mistaken.

justadudewhohacks commented 6 years ago

Actually I am not sure either, if it is possible with OpenCV to compress frames to a stream. The only thing I am aware of is to use imencode, to encode frames, but this can not be done with inter frame compression algorithms.