使用网络套接字的NodeJS,OpenCV的和图像流 [英] NodeJS, OpenCV and Streaming Images Using Net Socket

查看:203
本文介绍了使用网络套接字的NodeJS,OpenCV的和图像流的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的最终目标是从流我的笔记本电脑的视频服务器。我想用在笔记本电脑和放大器的NodeJS做到这一点;服务器。我用的是OpenCV库捕捉到笔记本电脑上的视频,并将其保存为JPG文件。然后我读了文件,并将其转换为base64这样我可以使用节点的Net.socket模块的不足。这是一个连续的过程:采集,连接code,并发送

My end goal is to stream video from my laptop to a server. I'm trying to accomplish this by using NodeJs on the laptop & the server. I use the OpenCV library to capture the video on the laptop and save it to a jpg file. I then read the file and convert it to base64 so that I can transport it using the Net.socket module in Node. This is a continous process: capture, encode, and send.

下面是服务器code只是传输一个JPG文件格式:

Here is the server code for just transmitting one jpg file:

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();
            });
        });
    });
});

在客户端我循环,直至FIN从服务器接收。下面是客户端code:

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);
        }
    });
});

的问题是,整个图像实际上不被发送。当我打开所接收的文件,copy.jpg,总有一个块在底部丢失。

The problem is that the entire image does not actually get sent. When I open the received file, copy.jpg, there is always a chunk missing at the bottom.

在最终版本的目标是陆续发送一个JPG和划定通过关键字每个JPG的结尾,如EndOfFile。我试图通过附加关键字EndOfFile我的base64 EN codeD映像要做到这一点之前发送,但是,真正得到搞砸了的接收端。

In the final version the goal is to send one jpg after another and delimit the end of each 'jpg' via a keyword such as 'EndOfFile'. I tried to do this by appending the keyword 'EndOfFile' to my base64 encoded image before sending but on the receiving end that really got screwed up.

样Advanced Server的:

Sample Advanced Server:

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

一客户端的环路将审查该关键字的每个数据块,如果它发现它那么无论是在缓冲将被写入到文件和缓冲器复位,准备下一个文件。

One the client side the loop would examine each chunk of data for the keyword and if it found it then whatever is in the buffer would be written to file and the buffer reset, ready for the next file.

样品高级客户端

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;
    }
});

我已经得到这在Python的工作,所以我觉得我的逻辑是正确的,但我不是作为的NodeJS舒适。

I've gotten this to work in Python so I think my logic is correct but I'm not as confortable in NodeJS.

如果有人能告诉我,如果这是可以做到这一点理智的方式,并在可能我已经错了。

If someone could tell me if this is a sane way to do this and where may I have gone wrong.

在此先感谢!

推荐答案

我怀疑你看到的结束事件,而数据的最后一位仍在缓冲。

I suspect you're seeing end event while the last bit of data is still buffered.

尝试等待关闭事件,而不是结束事件。我不知道插座,但在其他节点API像产卵结束事件被解雇初期,前相关数据流被刷新,所以有可能仍在缓冲数据等待。

Try waiting for the close event rather than the end event. I'm not sure about sockets, but in other Node APIs like spawn, the end event is fired early, before related streams are flushed, so there may still be buffered data waiting.

您可能避免管道这个自己管理。使用 fs.createWriteStream() .pipe()插座流文件。

You could avoid managing this yourself by piping. Use fs.createWriteStream() and .pipe() the socket stream to the file.

这篇关于使用网络套接字的NodeJS,OpenCV的和图像流的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆