Created
August 19, 2013 04:38
-
-
Save markjlorenz/6265807 to your computer and use it in GitHub Desktop.
OpenCV in node.js without temp files
This file contains hidden or 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
# Use the mac's built in webcame to snap a pic, and circle the faces. | |
# requires (imagesnap)[https://github.com/rharder/imagesnap] be in `/vendor/imagesnap` | |
# and that you have openCV installed: | |
# `brew update` | |
# `brew tap homebrew/science` | |
# `brew install opencv` | |
# Ye 'olde requires. | |
cv = require('opencv') | |
spawn = require('child_process').spawn | |
uuid = require('uuid') | |
net = require('net') | |
sockFile = "tmp/#{uuid.v4()}.sock" | |
# Listen for child process to return with the | |
# image data on our socket | |
server = net.createServer() | |
server.listen sockFile | |
server.on 'connection', (sock)-> | |
image = new Buffer(0) | |
sock.on 'data', (data)-> | |
image = Buffer.concat [image, new Buffer(data)] | |
sock.on 'end', (data)-> | |
server.close() | |
faceDetect image | |
# Have the child process that takes the picture send it's stdout | |
# output to the socket | |
sock = new net.Socket | |
sock.connect sockFile, ()-> | |
snap = spawn 'vendor/imagesnap/imagesnap', [ '-' ], | |
stdio: [ process.stdin, sock, process.stderr ] | |
snap.on 'exit', ()-> ( sock.end() ) | |
# Do the face detection | |
# basically the example code | |
faceDetect = (buff)-> | |
cvStream = new cv.ImageStream | |
cvStream.on 'data', (im)-> | |
im.detectObject cv.FACE_CASCADE, {}, (err, faces)-> | |
for i in [0..faces.length-1] | |
x = faces[i] | |
im.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2) | |
outFile ="out/#{uuid.v4()}.jpg" | |
im.save(outFile) | |
console.log outFile | |
cvStream.write buff |
This file contains hidden or 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": "CircleMyFace", | |
"description": "", | |
"version": "0.0.1", | |
"engines": { | |
"node": "*" | |
}, | |
"dependencies": { | |
"opencv" : "*", | |
"uuid" : "*" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment