request("http://10.2.25.11/mjpg/video.jpg", function (err, res, body) { cv.readImage(body, function (im) { imCopy = im.copy(); im.convertHSVscale(); im.inRange([config.lowH, config.lowS, config.lowV], [config.highH, config.highS, config.highV]); im.erode(2); im.dilate(3); var hasTarget = false; contours = im.findContours(); for ( var i = 0; i < contours.size(); i++ ) { contours.approxPolyDP(i, 7, true); rect = contours.boundingRect(i); rect.ratio = rect.width/rect.height; if ( contours.cornerCount(i) == 4 && rect.ratio > 2 ) { hasTarget = true; target_data = rect; imCopy.drawContour(contours, i, [255,0,0]); break; } else { imCopy.drawContour(contours, i, [0,0,255]); target_data = {}; } } callback(image, hasTarget); }); });
var detectFaces = function() { if (!flying) return; if ((!processingImage) && lastPng) { processingImage = true; cv.readImage(lastPng, function(err, im) { var opts = {}; im.detectObject(cv.FACE_CASCADE, opts, function(err, faces) { var biggestFace; for (var k = 0; k < faces.length; k++) { face = faces[k]; if (!biggestFace || biggestFace.width < face.width) biggestFace = face; //im.rectangle([face.x, face.y], [face.x + face.width, face.y + face.height], [0, 255, 0], 2); } if (biggestFace) { foundFace(); } processingImage = false; //im.save('/tmp/salida.png'); }, opts.scale, opts.neighbors, opts.min && opts.min[0], opts.min && opts.min[1]); }); } };
.done(function () { var cv = require('opencv'); cv.readImage('out.png', function (err, im) { im_canny = im.copy(); im_canny.canny(0, 100); im_canny.dilate(2); // iters var contourData = []; var contours = im_canny.findContours(); for (var i = 0; i < contours.size(); ++i) { var arcLength = contours.arcLength(i, true); contours.approxPolyDP(i, 0.01 * arcLength, true); console.log('contour area: ', contours.area(i)); console.log('contour corners: ', contours.cornerCount(i)); if ((contours.area(i) < 1000)) { continue; } // extract perimiter of region var points = _.map(_.range(0, contours.cornerCount(i)), function (p) { return contours.point(i, p); }); contourData.push(points); } console.log('drawing contours'); drawContours('out.png', 'out_contour.png', contourData, function () { console.log('done drawing contours'); }); }); });
images.forEach(function(imageName) { var p = path.resolve('', indir+ '/' +imageName); cv.readImage(p, function(err, im) { var vis, lines, angle, centroid; if (err) { console.error('error reading ' + imageName); return; } vis = createVisualizerImage(im); lines = findLines(im); angle = getAngle(lines); centroid = getCentroid(lines); lines.forEach(function(line) { drawLine(vis, line, '#000000'); }); drawAngle(vis, angle, centroid); drawCentroid(vis, centroid); saveImage(vis, '../' + outdir + '/' + imageName); }); });
process.on('message', function (message) { if (message.command == 'start record') { savePath = message.data.savePath; encoder = new GIFEncoder(480, 480); // fs.rmdirSync(savePath); // fs.mkdirSync(savePath); } else if (message.command == 'write frame') { frameSequence ++; cv.readImage(new Buffer(message.data.frame), function (err, mat) { mat.save(savePath + 'frame-' + frameSequence.toString() + '.png'); }) } else if (message.command == 'stop record') { PNGFileStream(savePath + 'frame-?.png') .pipe(encoder.createWriteStream({ repeat: 0, delay: 100, quality: 10 })) .pipe(fs.createWriteStream(savePath + message.data.GifFileName)); process.send({ command: 'record stopped' }) } })
process.on('message', function (message) { if (message.command == 'start record') { encoder = new GIFEncoder(480, 480); frameSequence = 0; setTimeout(function () { process.send({ command: 'stop record' }); pngFileStream('./images/frame-?.png') .pipe(encoder.createWriteStream({ repeat: 0, delay: 100, quality: 10 })) .pipe(fs.createWriteStream('./images/myanimated.gif')); }, 5000); } else if (message.command == 'frame') { if (!!message.data.frame) { cv.readImage(new Buffer(message.data.frame), function (err, mat) { frameSequence ++; mat.save('./images/frame-' + frameSequence.toString() + '.png'); }) } } })
], function(filename, callback) { cv.readImage(filename, function(error, image) { vision.detectButtons( image, function(error, results) { annotateImage( filename, image, results, callback ); }); }); }, done);
function cropFaceGray(image, pgmPath, callback){ var base64noHead = image.replace(/^data:image\/\w+;base64,/,"") var bitmap = new Buffer(base64noHead, 'base64') cv.readImage(bitmap, function(err, im){ im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){ if (!faces){ console.log("No Faces") return err; } var face = faces[0] if(face && face.x){ var im2 = im.roi(face.x, face.y, face.width, face.height) convertBufferToPGM(im2.toBuffer(), pgmPath, function(err){ if(err){ console.log('server log: convert error') return callback(err = true) } else { console.log('server log: convert success') return callback(err = false) } }) } else { return callback(err = true) } }) }) }
.done(function(err, datum){ if(err) { return callback(err, null); } else { if(datum.length > 0){ trainingData = datum[datum.length - 1]; lbphFaceRecognizer.loadSync(trainingData.LBPHFace_path); cv.readImage(pgm_image, function(err, im){ if(err) { return callback(err, null); } var label = lbphFaceRecognizer.predictSync(im).id, distance = lbphFaceRecognizer.predictSync(im).confidence if(label != 1){ console.log("server log: label isn't 1") return callback("You are not the same person with the account's owner", distance) } else { console.log('server log: got distance') callback(null, distance) } }); } else{ return callback(err, null); } } });
client.on('end', function () { // 关闭与iPhone的链接 client.end(); // 解码图像数据 var imageData = new Buffer(base64ImageData, 'base64'); // 通过OpenCV读取图像 opencv.readImage(imageData, function (err, mat) { if (err) return; // 若还未锁定范围 if (!lock) { // 灰度化后, 识别图像中的轮廓 im_canny = mat.copy(); im_canny.convertGrayscale(); var lowThresh = 180; var highThresh = 255; var nIters = 2; im_canny.canny(lowThresh, highThresh); im_canny.dilate(nIters); contours = im_canny.findContours(); // 用于绘制识别出的、满足条件的轮廓 var big = new opencv.Matrix(mat.height(), mat.width()); for(i = 0; i < contours.size(); i++) { // 大于最小面积 if(contours.area(i) > lowerBoundArea) { // 选出只有4个角的轮廓并绘制 var arcLength = contours.arcLength(i, true); contours.approxPolyDP(i, 0.01 * arcLength, true); switch(contours.cornerCount(i)) { case 4: { lockedContour = contours.boundingRect(i); var moments = contours.moments(i); var cgx = Math.round(moments.m10 / moments.m00); var cgy = Math.round(moments.m01 / moments.m00); big.drawContour(contours, i, GREEN); big.line([cgx - 5, cgy], [cgx + 5, cgy], RED); big.line([cgx, cgy - 5], [cgx, cgy + 5], RED); break; } default: break; } } } window.show(big); } else { // 若已经锁定范围 // 通过lockedContour确定Region of Interest var ROI = mat.crop(lockedContour.x, lockedContour.y, lockedContour.width, lockedContour.height); // 如有必要, 调整大小 if (shouldResize) ROI.resize(ROI.width() * ratio, ROI.height() * ratio); // 显示 window.show(ROI); } window.blockingWaitKey(0, 35); }); });
xit('getSingleCharOcr()', function(done) { var map = captchaParser.loadTrainData(); var img = opencv.readImage(path.join(__dirname, '_output/char_1.png'), function(err, img) { var result = captchaParser.getSingleCharOcr(img, map); console.log(result); done(); }); });
var saveImage = function(imageName) { cv.readImage(tempImage, function(err, im) { if (err) throw err; if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size'); im.save(imageName); console.log('Image saved to ' + imageName); }); };
self.read = function(png, callback) { cv.readImage(png, function(err, im) { if (err) { return callback(err); } self.handle(im, callback); }); };
reader.parse(function(err, png){ if (err) throw err; var whiteBalance = 0; calculateWhiteBalance(png, whiteBalance, settings); cv.readImage(data, function(err, im){ XY = exports.cvProcess(err, im, settings); }); });
function processImage(orgfilename){ var destimg = filepath + output_directory + orgfilename; var orgimg = filepath + input_directory + orgfilename; cv.readImage(orgimg, function(err, im) { im.convertGrayscale(); im_canny = im.copy(); im_canny.canny(lowThresh, highThresh); im_canny.dilate(nIters); contours = im_canny.findContours(); var points = []; console.log("contours size = " + contours.size()); for(i = 0; i < contours.size(); i++) { var area = contours.area(i); console.log("area" + area); if(area < minArea) continue; var arcLength = contours.arcLength(i, true); contours.approxPolyDP(i, 0.01 * arcLength, true); if(contours.cornerCount(i) != 4) continue; // point coordinates var p0 = [contours.point(i, 0).x, contours.point(i, 0).y]; var p1 = [contours.point(i, 1).x, contours.point(i, 1).y]; var p2 = [contours.point(i, 2).x, contours.point(i, 2).y]; var p3 = [contours.point(i, 3).x, contours.point(i, 3).y]; var av_x = (p0[0] + p1[0] + p2[0] + p3[0])/4; var av_y = (p0[1] + p1[1] + p2[1] + p3[1])/4; points.push([av_x,av_y]); } var shape = new shapemaker(); var data = { image_width: im_canny.width(), image_height: im_canny.height(), coordinates: points }; console.log(points); shape.mask(data, orgimg, destimg, function(err, destimage){ sb.send("output image", "string", hosted_path + output_directory + orgfilename); }); }); }
xit('splitImage()', function(done) { opencv.readImage(path.join(__dirname, 'input/test1.jpeg'), function(err, img) { expect(err).to.be(null); var imgCopy = img.clone(); var imgs = captchaParser.splitImage(imgCopy); expect(imgs[0]).to.be.a(opencv.Matrix); done(); }); });
var grayScale = function() { var output = './grayScale.jpg'; cv.readImage(tempImage, function(err, im) { if (err) throw err; if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size'); im.convertGrayscale(); im.save(output); console.log('Image saved to ' + output); }); };
check: function(img, done) { cv.readImage(img, function(err, im) { im.detectObject(cv.FACE_CASCADE, {}, function(err, faces) { if (err) done(err) else done(null, faces.length > 0) }); }) }
image: function(cb) { cv.readImage(body, function(err, image) { /*if (image) { var height = 480; var width = Math.round((image.width() / image.height()) * height); image.resize(width, height); }*/ cb(err, image); }); },
function cv(FileName, cb, data) { console.log(FileName); var cv = require('openCV'); var easyimg = require('easyimage'); var Caman = require('caman').Caman; cv.readImage("input/" + FileName, function(err, im){ im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){ var imgWidth = im.width(); console.log(err,faces); Caman("input/" + FileName, function () { this.resize({ width: 600 }); // this.crop(face.width*rate, face.height*rate ,face.x*rate , face.y*rate); this.brightness(15) this.exposure(15) this.curves('rgb', [0, 0], [200, 0], [155, 255], [255, 255]) this.saturation(-50) this.gamma(1.8) this.vignette("50%", 60) this.brightness(5) this.contrast(30); this.sepia(60); this.render(function () { // this.save('output/'+ FileName ); for (var i=0;i<faces.length; i++){ var face = faces[i] var rate = 64/face.width; im.ellipse(face.x + face.width/2, face.y + face.height/2, face.width/2, face.height/2); Caman("input/" + FileName, function () { this.resize({ width: imgWidth*rate }); this.crop(face.width*rate, face.height*rate ,face.x*rate , face.y*rate); this.brightness(15) this.exposure(15) this.curves('rgb', [0, 0], [200, 0], [155, 255], [255, 255]) this.saturation(-50) this.gamma(1.8) this.vignette("50%", 60) this.brightness(5) this.contrast(30); this.sepia(60); this.render(function () { // this.save('output/crop/'+ FileName + '_' + i + '.jpg'); }); }); } cb(JSON.stringify(faces));// }); }); }); }); }
var prom = new Promise(function(resolve, reject) { cv.readImage(buf, function(err, matrix){ if (err) { reject(err) return self.emit('error', err) } self.push(matrix) callback() resolve() }) })
snap.shoot((buffer) => { cv.readImage(buffer, function(err, im){ im.detectObject(cv.FACE_CASCADE, {}, function(err, faces){ for (var i=0;i<faces.length; i++){ var x = faces[i]; im.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2); } im.save('./out.jpg'); }); }) })
var checkImage = function() { var output = './detect.jpg'; cv.readImage(tempImage, function(err, im) { if (err) throw err; if (im.width() < 1 || im.height() < 1) throw new Error('Image has no size'); im.inRange(lower_threshold, upper_threshold); im.save(output); console.log('Image saved to ' + output); }); };
exports.matrixHandler = function matrixHandler(matrix, callback){ var results = []; cv.readImage(matrix, function(err, img){ detectFace(img, function(result){ results.push(result); }); // detectBody(img, function(result){ // results.push(result); // }); callback(results); }); };
file.on('end', function() { cv.readImage(Buffer.concat(bufs), function(err, mat) { if (err) throw err; mat.convertGrayscale(); var imageBuffer = mat.toBuffer(); fs.writeFileSync(saveTo, imageBuffer); Student.find({}, function(err, stus) { for (var i = 0; i < stus.length; i++) stus[i].checkModel(saveTo); }); }); });
app.post('/img', function(req, res){ cv.readImage(req.files.img.path, function(err, im){ im.detectObject("node_modules/opencv/data/" + req.body.cascade, {}, function(err, faces){ for (var i=0;i<faces.length; i++){ var x = faces[i] im.ellipse(x.x + x.width/2, x.y + x.height/2, x.width/2, x.height/2); } res.setHeader('Content-Type', 'image/png' ); res.end(im.toBuffer()); }); }) })
xit('opencv.Matrix functions', function(done) { opencv.readImage(path.join(__dirname, 'input/test1.jpeg'), function(err, img) { expect(err).to.be(null); console.log(util.inspect(opencv.Matrix.prototype)) console.log(img.width()) console.log(img.height()); console.log(img.pixel(0,0)); console.log(img.pixelRow(10)); console.log(img.crop(0, 0, 10, 10)) done(); }); });
var getAllOcr = function(buf,map,callback) { //** TODO 如果buf == undefined? opencv.readImage(buf, function(err, mat) { if (err) return logger.error(err); var result = ''; var imgs = splitImage(mat); async.each(imgs, function(img) { result += getSingleCharOcr(img,map); }); mat.save(path.join(__dirname, '../../test/captcha/_output/result.jpg')); if(callback) return callback && callback(null,result); }); };
var detectShapes = function() { var output = './shapes.png'; var lowThresh = 0; var highThresh = 100; var nIters = 2; var minArea = 2000; var BLUE = [0, 255, 0]; // B, G, R var RED = [0, 0, 255]; // B, G, R var GREEN = [0, 255, 0]; // B, G, R var WHITE = [255, 255, 255]; // B, G, R cv.readImage(tempImage, function(err, im) { if (err) throw err; width = im.width(); height = im.height(); if (width < 1 || height < 1) throw new Error('Image has no size'); var out = new cv.Matrix(height, width); im.convertGrayscale(); im_canny = im.copy(); im_canny.canny(lowThresh, highThresh); im_canny.dilate(nIters); contours = im_canny.findContours(); for (i = 0; i < contours.size(); i++) { if (contours.area(i) < minArea) continue; var arcLength = contours.arcLength(i, true); contours.approxPolyDP(i, 0.01 * arcLength, true); switch (contours.cornerCount(i)) { case 3: out.drawContour(contours, i, GREEN); break; case 4: out.drawContour(contours, i, RED); break; default: out.drawContour(contours, i, WHITE); } } out.save(output); console.log('Image saved to ' + output); }); };
module.exports = function(path, imgName, socket) { cv.readImage(path + imgName, function(err, im) { im.detectObject(cv.FACE_CASCADE, {}, function(err, faces) { for (var i = 0; i < faces.length; i++) { var x = faces[i]; var face = faces[i]; im.ellipse(x.x + x.width / 2, x.y + x.height / 2, x.width / 2, x.height / 2); } im.save('./public/detectedFaces/' + imgName); console.log(im); socket.emit('faceImage', {src: './detectedFaces/' + imgName}); }); }); };