I'm trying to load a lot of images which are listed in a CVS file in the following format:
./path/to/img1.ext;label1
./path/to/img2.ext;label2
This is the script I've written:
var cv = require("opencv"),
fs = require("fs"),
console = require("console"),
util = require("util"),
lazy = require("lazy.js");
var basePath = '/some/path/';
var csvFile = fs.createReadStream(basePath + 'db.csv', {flags:'r'});
var images = [],
labels = [];
lazy(csvFile)
.lines()
.each(function(l) {
var d = lazy(l).split(';').toArray();
cv.readImage(basePath + d[0], function(e, m) {
images.push(m);
});
labels.push(d[1]);
});
console.log(util.inspect(images));
console.log(util.inspect(labels));
It prints two line containing the representation of an empty array []
.
The images are actually getting loaded by OpenCV, because if you try to print m
before pushing it into the array it correctly prints [Matrix HxW ]
, where H
and W
stand for the height and the width of the images.
EDIT: also, can you think of a better way than 2 separated arrays for keeping each image associated with its label?
EDIT: the problem seems to be that the images are loaded asynchronously. So the problem is my lack of experience with asynchronous programming. How can I make this work?