NodeJS OpenCV and Streaming Images Using Net Socket

0 votes

My ultimate goal is to have video streaming from my laptop to a server. I'm trying to do this with NodeJs on both the laptop and the server. To capture the video on the laptop and save it as a jpg file, I use the OpenCV library. I then read the file and convert it to base64 so that I can send it over the network with Node's Net.socket module. Capture, encode, and send are all ongoing processes.

The server code for sending a single jpg file is as follows:

var cv = require('opencv');
var fs = require('fs');
var net = require('net');
var camera = new cv.VideoCapture(0);
var server = net.createServer();
server.listen('50007', '127.0.0.1');

server.on('connection', function(socket){
    camera.read(function(image){
        image.save('original.jpg');

        fs.readFile('original.jpg', 'base64', function(err, image){
            socket.write(image, 'base64', function(){
                socket.end();
            });
        });
    });
});

On the client I loop until the FIN is received from the server. Here is the client code:

var net = require('net');
var fs = require('fs');
var client = new net.Socket();
var buffer ='';
client.setEncoding('base64');

client.connect('50007', '127.0.0.1', function(){
    console.log('Connecting to server...');
});

client.on('data', function(data){
    buffer += data;
});

client.on('end', function(){
    var dataBuffer = new Buffer(buffer, 'base64');
    fs.writeFile('copy.jpg', dataBuffer, function(err){
        if(err){
            console.log(err);
        }
    });
});

The issue is that the complete image does not get transmitted. When I open the copied.jpg file, it always has a piece missing at the bottom.

The purpose in the final version is to send one jpg after another, with a term like 'EndOfFile' used to delimit the end of each 'jpg'. I attempted to do this by inserting the keyword 'EndOfFile' to my base64 encoded image before sending, but this was completely messed up on the receiving end.

Sample Advanced Server:

fs.readFile('original.jpg', 'base64', function(err, image){
    image += 'EndOfFile';
    socket.write(image, 'base64');
}); 

On the client side, the loop would look for the keyword in each chunk of data, and if it was found, whatever was in the buffer would be put to file and the buffer would be reset, ready for the next file.

Sample Advanced Client

client.on('data', function(data){
    if(data.indexOf('EndOfFile') > 0){
        buffer += data.substr(0, data.indexOf('EndOfLine'));
        var dataBuffer = new Buffer(buffer, 'base64');

        fs.writeFile('copy.jpg', dataBuffer, function(err){
            if(err){
                console.log(err);
            }
        });

        buffer = '';
    } else {
        buffer += data;
    }
});

I got this to work in Python, so I believe my logic is sound, but I'm not as familiar with NodeJS.

If someone could tell me if this is a reasonable approach and where I could have gone wrong, I'd be grateful.

Thank you so much in advance!

Jun 21, 2022 in Node-js by Vaani
• 7,070 points
977 views

No answer to this question. Be the first to respond.

Your answer

Your name to display (optional):
Privacy: Your email address will only be used for sending these notifications.

Related Questions In Node-js

0 votes
0 answers

Upload file using NodeJS and BusBoy

I'm using NodeJS to upload a file. ...READ MORE

Jun 21, 2022 in Node-js by Vaani
• 7,070 points
1,970 views
0 votes
0 answers

Sync contacts using Google Contacts API version 3.0 and NodeJS' Passport

I'm using passport and would like to ...READ MORE

Jun 21, 2022 in Node-js by Vaani
• 7,070 points
538 views
0 votes
0 answers

Upload file using NodeJS and BusBoy

I'm using NodeJS to upload a file. ...READ MORE

Jun 22, 2022 in Node-js by Vaani
• 7,070 points
3,424 views
0 votes
0 answers

Sync contacts using Google Contacts API version 3.0 and NodeJS' Passport

I want to sync Google contacts with ...READ MORE

Jun 22, 2022 in Node-js by Vaani
• 7,070 points
684 views
0 votes
1 answer

Error: listen EADDRINUSE while using nodejs?

Hello @kartik, EADDRINUSE means that the port number which listen() tries ...READ MORE

answered Jul 9, 2020 in Node-js by Niroj
• 82,840 points
6,604 views
0 votes
1 answer

How to extract request http headers from a request using NodeJS connect?

Hello @kartik, To see a list of HTTP ...READ MORE

answered Jul 15, 2020 in Node-js by Niroj
• 82,840 points
23,300 views
0 votes
1 answer

Truffle tests not running after truffle init

This was a bug. They've fixed it. ...READ MORE

answered Sep 11, 2018 in Blockchain by Christine
• 15,790 points
1,943 views
0 votes
1 answer

Hyperledger Sawtooth vs Quorum in concurrency and speed Ask

Summary: Both should provide similar reliability of ...READ MORE

answered Sep 26, 2018 in IoT (Internet of Things) by Upasana
• 8,620 points
1,469 views
0 votes
0 answers

How to transfer data from a controller to a socket.io in nodejs?

I'm developing a web application that will ...READ MORE

Jun 23, 2022 in Node-js by Vaani
• 7,070 points
2,693 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP