Created
October 19, 2014 22:45
-
-
Save julianduque/244d77504516571d729d to your computer and use it in GitHub Desktop.
Realtime Webcam with Primus and OpenCV
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Awesome!