1

I need to share the webcam frames with server. I'm newbie in this. Currently I'm doing this by drawing it on canvas and sending the Base64 string of the image to the server using fetch API. Is there any other method which can do it quickly and efficiently.

        var width = 320; // We will scale the photo width to this
        var height = 0; // This will be computed based on the input stream

        var streaming = false;

        var video = null;
        var canvas = null;
        var photo = null;
        var startbutton = null;

        function startup() {
            video = document.getElementById('video');
            canvas = document.getElementById('canvas');
            photo = document.getElementById('photo');
            startbutton = document.getElementById('startbutton');

            navigator.mediaDevices.getUserMedia({
                    video: true,
                    audio: false
                })
                .then(function(stream) {
                    video.srcObject = stream;
                    video.play();
                })
                .catch(function(err) {
                    console.log("An error occurred: " + err);
                });

            video.addEventListener('canplay', function(ev) {
                if (!streaming) {
                    height = video.videoHeight / (video.videoWidth / width);

                    if (isNaN(height)) {
                        height = width / (4 / 3);
                    }

                    video.setAttribute('width', width);
                    video.setAttribute('height', height);
                    canvas.setAttribute('width', width);
                    canvas.setAttribute('height', height);
                    streaming = true;
                }
            }, false);

            startbutton.addEventListener('click', function(ev) {
                takepicture();
                ev.preventDefault();
            }, false);

            clearphoto();
        }


        function clearphoto() {
            var context = canvas.getContext('2d');
            context.fillStyle = "#AAA";
            context.fillRect(0, 0, canvas.width, canvas.height);

            var data = canvas.toDataURL('image/png');
            photo.setAttribute('src', data);
        }

        function takepicture() {
            var context = canvas.getContext('2d');
            if (width && height) {
                canvas.width = width;
                canvas.height = height;
                context.drawImage(video, 0, 0, width, height);

                var data = canvas.toDataURL('image/png');
                console.log(data);
                sendImage(data);
                photo.setAttribute('src', data);
            } else {
                clearphoto();
            }
        }

        window.addEventListener('load', startup, false);

    function  sendImage(dataURL){
      fetch('/recognize', {
        method: 'post',
        headers: {
          'Accept': 'application/json, text/plain, */*',
          'Content-Type': 'application/json'
        },
        body: JSON.stringify({imageBase64:dataURL})
      }).then(res=>res.json())
        .then(res => console.log(res));
          }
Sudesh Chaudhary
  • 395
  • 6
  • 13
  • You may want to [compress the image with gzip](https://stackoverflow.com/questions/45943481/enabling-gzip-compression-with-fetch-js) before sending it to the server. – terrymorse Dec 30 '20 at 21:40
  • What about using webrtc ,Is it better? – Sudesh Chaudhary Jan 03 '21 at 19:55
  • Setting up webrtc is quite involved and complicated. It's designed for peer-to-peer communication without using an intermediate server, not client-to-server communication. – terrymorse Jan 03 '21 at 20:34

0 Answers0