-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathindex.js
More file actions
167 lines (162 loc) · 4.81 KB
/
Copy pathindex.js
File metadata and controls
167 lines (162 loc) · 4.81 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
const path = require('path');
const fs = require('fs');
const defaults = require('lodash.defaults');
const Promise = require('bluebird');
const NativeOrbIndexer = require('./build/Release/orbidx');
var default_options = {
nfeatures: 2000,
scaleFactor: 1.02,
nlevels: 100,
edgeThreshold: 15,
firstLevel: 0,
WTA_K: 2,
scoreType: 0, // 0: Harris Score, 1: Fast Score
patchSize: 15,
fastThreshold: 20,
maxKeypoints: 2000, // Max number of keypoints in the grid
gridRows: 1, // Grid Based Features - to make sure they are
gridCols: 1, // distributed in the image; use 1x1 to have the
// usual distribution, -1x-1 will use Pyramid
minFeatureDistance: 30 // The minumum distance of a feature from an existing
// one before it's added to the visual word DB
}
var OrbIndexer = function (options) {
options = defaults(options, default_options);
options.wordIndexPath = options.wordIndexPath || path.resolve(path.join(__dirname, 'data', 'visualWordsORB.dat'));
this.options = options;
this.orbIndexer = new NativeOrbIndexer.OrbIndexer();
}
OrbIndexer.prototype = {
initialize: function () {
var _this = this;
return new Promise(function (resolve, reject) {
try {
_this.orbIndexer.initWordIndex(function (err, res) {
if (err) {
console.log('Error: ' + err);
return reject(new Error(err));
}
resolve(res);
}, _this.options.wordIndexPath);
} catch (err) {
console.log('Error: ' + err.message);
reject(err);
}
});
},
_loadImageFile: function (imagePath) {
return new Promise(function (resolve, reject) {
fs.readFile(imagePath, (err, data) => {
if (err) {
return reject(err);
}
resolve(new Buffer(data));
});
});
},
indexImageFile: function (imageId, imagePath) {
var _this = this;
return _this._loadImageFile(imagePath)
.then(function (imageData) {
return _this.indexImage(imageId, imageData);
});
},
indexImage: function (imageId, imageBuffer) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
_this.orbIndexer.indexImage(function (err, hits) {
if (err) {
return reject(new Error(err));
}
resolve(hits);
}, imageId, imageBuffer, imageBuffer.length,
_this.options.nfeatures,
_this.options.scaleFactor,
_this.options.nlevels,
_this.options.edgeThreshold,
_this.options.firstLevel,
_this.options.WTA_K,
_this.options.scoreType,
_this.options.patchSize,
_this.options.fastThreshold,
_this.options.maxKeypoints,
_this.options.gridRows,
_this.options.gridCols
);
} catch (err) {
reject(err);
}
});
},
startTraining: function() {
return this.orbIndexer.startTraining();
},
trainImageFile: function (imagePath) {
var _this = this;
return _this._loadImageFile(imagePath)
.then(function (imageData) {
return _this.trainImage(imageData);
});
},
trainImage: function(imageBuffer) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
_this.orbIndexer.trainImage(function (err, message) {
if (err) {
return reject(new Error(err));
}
resolve(message);
}, imageBuffer, imageBuffer.length,
_this.options.nfeatures,
_this.options.scaleFactor,
_this.options.nlevels,
_this.options.edgeThreshold,
_this.options.firstLevel,
_this.options.WTA_K,
_this.options.scoreType,
_this.options.patchSize,
_this.options.fastThreshold,
_this.options.maxKeypoints,
_this.options.gridRows,
_this.options.gridCols,
_this.options.minFeatureDistance
);
} catch (err) {
reject(err);
}
});
},
saveTrainedIndex: function(trainingFile) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
_this.orbIndexer.saveTraining(function (err, result) {
if (err) {
return reject(new Error(err));
}
resolve(result);
}, trainingFile);
} catch (err) {
reject(err);
}
});
},
loadTrainedIndex: function(trainingFile) {
var _this = this;
return new Promise(function (resolve, reject) {
try {
_this.orbIndexer.loadTraining(function (err, result) {
if (err) {
return reject(new Error(err));
}
resolve(result);
}, trainingFile);
} catch (err) {
reject(err);
}
});
}
}
module.exports = OrbIndexer;