Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Realtime Webcam with Primus and OpenCV
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>HAN-Bot Realtime</title>
<script src="/primus/primus.js"></script>
</head>
<body>
<img id="camera" />
<script>
var socket = Primus.connect();
var image = document.querySelector('#camera');
socket.on('image', function (data) {
image.src = 'data:image/png;base64,' + data;
});
</script>
</body>
</html>
{
"name": "hanbot",
"version": "1.0.0",
"description": "HAN-Bot Streaming",
"main": "server.js",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Julian Duque",
"license": "MIT",
"dependencies": {
"opencv": "^1.0.0",
"primus": "^2.4.9",
"primus-emitter": "^3.0.3",
"ws": "^0.4.32"
}
}
var Primus = require('primus'),
Emitter = require('primus-emitter'),
http = require('http'),
fs = require('fs'),
cv = require('opencv');
var camera = new cv.VideoCapture(0);
camera.setWidth(400);
camera.setHeight(400);
var server = http.createServer(function (req, res) {
res.writeHead(200, { 'Content-type': 'text/html' });
fs.createReadStream('index.html').pipe(res);
});
var primus = new Primus(server);
primus.use('emitter', Emitter);
primus.on('connection', function (socket) {
var loop = function () {
camera.read(function (err, image) {
socket.send('image', image.toBuffer().toString('base64'));
setTimeout(loop, 100);
});
};
loop();
});
server.listen(8080);
@cronopio

This comment has been minimized.

Copy link

@cronopio cronopio commented Oct 21, 2014

Awesome!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.