mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
Moved remaining code to instances
This commit is contained in:
committed by
Christoph Oberhofer
parent
b733c2b7fd
commit
633eabe86c
@@ -1,4 +1,4 @@
|
|||||||
export default (function() {
|
export default function createEventedElement() {
|
||||||
var events = {};
|
var events = {};
|
||||||
|
|
||||||
function getEvent(eventName) {
|
function getEvent(eventName) {
|
||||||
@@ -79,4 +79,4 @@ export default (function() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
})();
|
};
|
||||||
|
|||||||
@@ -24,9 +24,10 @@ const mat2 = {
|
|||||||
copy: require('gl-mat2/copy'),
|
copy: require('gl-mat2/copy'),
|
||||||
create: require('gl-mat2/create'),
|
create: require('gl-mat2/create'),
|
||||||
invert: require('gl-mat2/invert')
|
invert: require('gl-mat2/invert')
|
||||||
}
|
};
|
||||||
|
|
||||||
var _config,
|
export default function createLocator(inputImageWrapper, config) {
|
||||||
|
var _config = config,
|
||||||
_currentImageWrapper,
|
_currentImageWrapper,
|
||||||
_skelImageWrapper,
|
_skelImageWrapper,
|
||||||
_subImageWrapper,
|
_subImageWrapper,
|
||||||
@@ -45,10 +46,13 @@ var _config,
|
|||||||
}
|
}
|
||||||
},
|
},
|
||||||
_numPatches = {x: 0, y: 0},
|
_numPatches = {x: 0, y: 0},
|
||||||
_inputImageWrapper,
|
_inputImageWrapper = inputImageWrapper,
|
||||||
_skeletonizer;
|
_skeletonizer;
|
||||||
|
|
||||||
function initBuffers() {
|
initBuffers();
|
||||||
|
initCanvas();
|
||||||
|
|
||||||
|
function initBuffers() {
|
||||||
var skeletonImageData;
|
var skeletonImageData;
|
||||||
|
|
||||||
if (_config.halfSample) {
|
if (_config.halfSample) {
|
||||||
@@ -85,9 +89,9 @@ function initBuffers() {
|
|||||||
}, undefined, Array, true);
|
}, undefined, Array, true);
|
||||||
_patchGrid = new ImageWrapper(_imageToPatchGrid.size, undefined, undefined, true);
|
_patchGrid = new ImageWrapper(_imageToPatchGrid.size, undefined, undefined, true);
|
||||||
_patchLabelGrid = new ImageWrapper(_imageToPatchGrid.size, undefined, Int32Array, true);
|
_patchLabelGrid = new ImageWrapper(_imageToPatchGrid.size, undefined, Int32Array, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
function initCanvas() {
|
function initCanvas() {
|
||||||
if (_config.useWorker || typeof document === 'undefined') {
|
if (_config.useWorker || typeof document === 'undefined') {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -99,13 +103,13 @@ function initCanvas() {
|
|||||||
_canvasContainer.ctx.binary = _canvasContainer.dom.binary.getContext("2d");
|
_canvasContainer.ctx.binary = _canvasContainer.dom.binary.getContext("2d");
|
||||||
_canvasContainer.dom.binary.width = _binaryImageWrapper.size.x;
|
_canvasContainer.dom.binary.width = _binaryImageWrapper.size.x;
|
||||||
_canvasContainer.dom.binary.height = _binaryImageWrapper.size.y;
|
_canvasContainer.dom.binary.height = _binaryImageWrapper.size.y;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a bounding box which encloses all the given patches
|
* Creates a bounding box which encloses all the given patches
|
||||||
* @returns {Array} The minimal bounding box
|
* @returns {Array} The minimal bounding box
|
||||||
*/
|
*/
|
||||||
function boxFromPatches(patches) {
|
function boxFromPatches(patches) {
|
||||||
var overAvg,
|
var overAvg,
|
||||||
i,
|
i,
|
||||||
j,
|
j,
|
||||||
@@ -191,24 +195,24 @@ function boxFromPatches(patches) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return box;
|
return box;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a binary image of the current image
|
* Creates a binary image of the current image
|
||||||
*/
|
*/
|
||||||
function binarizeImage() {
|
function binarizeImage() {
|
||||||
otsuThreshold(_currentImageWrapper, _binaryImageWrapper);
|
otsuThreshold(_currentImageWrapper, _binaryImageWrapper);
|
||||||
_binaryImageWrapper.zeroBorder();
|
_binaryImageWrapper.zeroBorder();
|
||||||
if (ENV.development && _config.debug.showCanvas) {
|
if (ENV.development && _config.debug.showCanvas) {
|
||||||
_binaryImageWrapper.show(_canvasContainer.dom.binary, 255);
|
_binaryImageWrapper.show(_canvasContainer.dom.binary, 255);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Iterate over the entire image
|
* Iterate over the entire image
|
||||||
* extract patches
|
* extract patches
|
||||||
*/
|
*/
|
||||||
function findPatches() {
|
function findPatches() {
|
||||||
var i,
|
var i,
|
||||||
j,
|
j,
|
||||||
x,
|
x,
|
||||||
@@ -254,14 +258,14 @@ function findPatches() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return patchesFound;
|
return patchesFound;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Finds those connected areas which contain at least 6 patches
|
* Finds those connected areas which contain at least 6 patches
|
||||||
* and returns them ordered DESC by the number of contained patches
|
* and returns them ordered DESC by the number of contained patches
|
||||||
* @param {Number} maxLabel
|
* @param {Number} maxLabel
|
||||||
*/
|
*/
|
||||||
function findBiggestConnectedAreas(maxLabel){
|
function findBiggestConnectedAreas(maxLabel){
|
||||||
var i,
|
var i,
|
||||||
sum,
|
sum,
|
||||||
labelHist = [],
|
labelHist = [],
|
||||||
@@ -294,12 +298,12 @@ function findBiggestConnectedAreas(maxLabel){
|
|||||||
});
|
});
|
||||||
|
|
||||||
return topLabels;
|
return topLabels;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
*
|
||||||
*/
|
*/
|
||||||
function findBoxes(topLabels, maxLabel) {
|
function findBoxes(topLabels, maxLabel) {
|
||||||
var i,
|
var i,
|
||||||
j,
|
j,
|
||||||
sum,
|
sum,
|
||||||
@@ -336,13 +340,13 @@ function findBoxes(topLabels, maxLabel) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return boxes;
|
return boxes;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Find similar moments (via cluster)
|
* Find similar moments (via cluster)
|
||||||
* @param {Object} moments
|
* @param {Object} moments
|
||||||
*/
|
*/
|
||||||
function similarMoments(moments) {
|
function similarMoments(moments) {
|
||||||
var clusters = cluster(moments, 0.90);
|
var clusters = cluster(moments, 0.90);
|
||||||
var topCluster = topGeneric(clusters, 1, function(e) {
|
var topCluster = topGeneric(clusters, 1, function(e) {
|
||||||
return e.getPoints().length;
|
return e.getPoints().length;
|
||||||
@@ -355,9 +359,9 @@ function similarMoments(moments) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
function skeletonize(x, y) {
|
function skeletonize(x, y) {
|
||||||
_binaryImageWrapper.subImageAsCopy(_subImageWrapper, imageRef(x, y));
|
_binaryImageWrapper.subImageAsCopy(_subImageWrapper, imageRef(x, y));
|
||||||
_skeletonizer.skeletonize();
|
_skeletonizer.skeletonize();
|
||||||
|
|
||||||
@@ -365,9 +369,9 @@ function skeletonize(x, y) {
|
|||||||
if (ENV.development && _config.debug.showSkeleton) {
|
if (ENV.development && _config.debug.showSkeleton) {
|
||||||
_skelImageWrapper.overlay(_canvasContainer.dom.binary, 360, imageRef(x, y));
|
_skelImageWrapper.overlay(_canvasContainer.dom.binary, 360, imageRef(x, y));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Extracts and describes those patches which seem to contain a barcode pattern
|
* Extracts and describes those patches which seem to contain a barcode pattern
|
||||||
* @param {Array} moments
|
* @param {Array} moments
|
||||||
* @param {Object} patchPos,
|
* @param {Object} patchPos,
|
||||||
@@ -375,7 +379,7 @@ function skeletonize(x, y) {
|
|||||||
* @param {Number} y
|
* @param {Number} y
|
||||||
* @returns {Array} list of patches
|
* @returns {Array} list of patches
|
||||||
*/
|
*/
|
||||||
function describePatch(moments, patchPos, x, y) {
|
function describePatch(moments, patchPos, x, y) {
|
||||||
var k,
|
var k,
|
||||||
avg,
|
avg,
|
||||||
eligibleMoments = [],
|
eligibleMoments = [],
|
||||||
@@ -428,13 +432,13 @@ function describePatch(moments, patchPos, x, y) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
return patchesFound;
|
return patchesFound;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* finds patches which are connected and share the same orientation
|
* finds patches which are connected and share the same orientation
|
||||||
* @param {Object} patchesFound
|
* @param {Object} patchesFound
|
||||||
*/
|
*/
|
||||||
function rasterizeAngularSimilarity(patchesFound) {
|
function rasterizeAngularSimilarity(patchesFound) {
|
||||||
var label = 0,
|
var label = 0,
|
||||||
threshold = 0.95,
|
threshold = 0.95,
|
||||||
currIdx = 0,
|
currIdx = 0,
|
||||||
@@ -523,17 +527,9 @@ function rasterizeAngularSimilarity(patchesFound) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
return label;
|
return label;
|
||||||
}
|
}
|
||||||
|
|
||||||
export default {
|
|
||||||
init: function(inputImageWrapper, config) {
|
|
||||||
_config = config;
|
|
||||||
_inputImageWrapper = inputImageWrapper;
|
|
||||||
|
|
||||||
initBuffers();
|
|
||||||
initCanvas();
|
|
||||||
},
|
|
||||||
|
|
||||||
|
return {
|
||||||
locate: function() {
|
locate: function() {
|
||||||
var patchesFound,
|
var patchesFound,
|
||||||
topLabels,
|
topLabels,
|
||||||
@@ -564,9 +560,10 @@ export default {
|
|||||||
|
|
||||||
boxes = findBoxes(topLabels, maxLabel);
|
boxes = findBoxes(topLabels, maxLabel);
|
||||||
return boxes;
|
return boxes;
|
||||||
},
|
}
|
||||||
|
}
|
||||||
checkImageConstraints: function(inputStream, config) {
|
}
|
||||||
|
export function checkImageConstraints(inputStream, config) {
|
||||||
var patchSize,
|
var patchSize,
|
||||||
width = inputStream.getWidth(),
|
width = inputStream.getWidth(),
|
||||||
height = inputStream.getHeight(),
|
height = inputStream.getHeight(),
|
||||||
@@ -603,5 +600,4 @@ export default {
|
|||||||
throw new Error("Image dimensions do not comply with the current settings: Width (" +
|
throw new Error("Image dimensions do not comply with the current settings: Width (" +
|
||||||
width + " )and height (" + height +
|
width + " )and height (" + height +
|
||||||
") must a multiple of " + patchSize.x);
|
") must a multiple of " + patchSize.x);
|
||||||
}
|
}
|
||||||
};
|
|
||||||
|
|||||||
+2
-31
@@ -2,7 +2,6 @@ import TypeDefs from './common/typedefs'; // eslint-disable-line no-unused-vars
|
|||||||
import WebrtcAdapter from 'webrtc-adapter'; // eslint-disable-line no-unused-vars
|
import WebrtcAdapter from 'webrtc-adapter'; // eslint-disable-line no-unused-vars
|
||||||
import createScanner from './scanner';
|
import createScanner from './scanner';
|
||||||
import ImageWrapper from './common/image_wrapper';
|
import ImageWrapper from './common/image_wrapper';
|
||||||
import Events from './common/events';
|
|
||||||
import ImageDebug from './common/image_debug';
|
import ImageDebug from './common/image_debug';
|
||||||
import ResultCollector from './analytics/result_collector';
|
import ResultCollector from './analytics/result_collector';
|
||||||
import Config from './config/config';
|
import Config from './config/config';
|
||||||
@@ -31,29 +30,19 @@ function fromImage(config, imageSrc, imageConfig) {
|
|||||||
const scanner = createScanner(config);
|
const scanner = createScanner(config);
|
||||||
return {
|
return {
|
||||||
addEventListener: (eventType, cb) => {
|
addEventListener: (eventType, cb) => {
|
||||||
scanner.init(config, () => {
|
scanner.decodeSingle(config, cb);
|
||||||
Events.once(eventType, (result) => {
|
|
||||||
scanner.stop();
|
|
||||||
cb(result);
|
|
||||||
}, true);
|
|
||||||
scanner.start();
|
|
||||||
});
|
|
||||||
},
|
},
|
||||||
removeEventListener(cb) {
|
removeEventListener(cb) {
|
||||||
console.log("Remove listener");
|
console.log("Remove listener");
|
||||||
},
|
},
|
||||||
toPromise() {
|
toPromise() {
|
||||||
return new Promise((resolve, reject) => {
|
return new Promise((resolve, reject) => {
|
||||||
scanner.init(config, () => {
|
scanner.decodeSingle(config, (result) => {
|
||||||
Events.once('processed', (result) => {
|
|
||||||
scanner.stop();
|
|
||||||
if (result.codeResult && result.codeResult.code) {
|
if (result.codeResult && result.codeResult.code) {
|
||||||
return resolve(result);
|
return resolve(result);
|
||||||
}
|
}
|
||||||
return reject(result);
|
return reject(result);
|
||||||
});
|
});
|
||||||
scanner.start();
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@@ -117,9 +106,6 @@ function createApi(configuration = Config) {
|
|||||||
config(conf) {
|
config(conf) {
|
||||||
return createApi(merge({}, configuration, conf));
|
return createApi(merge({}, configuration, conf));
|
||||||
},
|
},
|
||||||
init: function(config, cb, imageWrapper) {
|
|
||||||
defaultScanner.init(config, cb, imageWrapper);
|
|
||||||
},
|
|
||||||
start: function() {
|
start: function() {
|
||||||
defaultScanner.start();
|
defaultScanner.start();
|
||||||
},
|
},
|
||||||
@@ -129,24 +115,9 @@ function createApi(configuration = Config) {
|
|||||||
pause: function() {
|
pause: function() {
|
||||||
defaultScanner.pause();
|
defaultScanner.pause();
|
||||||
},
|
},
|
||||||
onDetected: function(callback) {
|
|
||||||
defaultScanner.onDetected(callback);
|
|
||||||
},
|
|
||||||
offDetected: function(callback) {
|
|
||||||
defaultScanner.offDetected(callback);
|
|
||||||
},
|
|
||||||
onProcessed: function(callback) {
|
|
||||||
defaultScanner.onProcessed(callback);
|
|
||||||
},
|
|
||||||
offProcessed: function(callback) {
|
|
||||||
defaultScanner.offProcessed(callback);
|
|
||||||
},
|
|
||||||
registerResultCollector: function(resultCollector) {
|
registerResultCollector: function(resultCollector) {
|
||||||
defaultScanner.registerResultCollector(resultCollector);
|
defaultScanner.registerResultCollector(resultCollector);
|
||||||
},
|
},
|
||||||
decodeSingle: function(config, resultCallback) {
|
|
||||||
defaultScanner.decodeSingle(config, resultCallback);
|
|
||||||
},
|
|
||||||
ImageWrapper: ImageWrapper,
|
ImageWrapper: ImageWrapper,
|
||||||
ImageDebug: ImageDebug,
|
ImageDebug: ImageDebug,
|
||||||
ResultCollector: ResultCollector,
|
ResultCollector: ResultCollector,
|
||||||
|
|||||||
+15
-13
@@ -1,7 +1,7 @@
|
|||||||
import ImageWrapper from './common/image_wrapper';
|
import ImageWrapper from './common/image_wrapper';
|
||||||
import BarcodeLocator from './locator/barcode_locator';
|
import createLocator, {checkImageConstraints} from './locator/barcode_locator';
|
||||||
import BarcodeDecoder from './decoder/barcode_decoder';
|
import BarcodeDecoder from './decoder/barcode_decoder';
|
||||||
import Events from './common/events';
|
import createEventedElement from './common/events';
|
||||||
import CameraAccess from './input/camera_access';
|
import CameraAccess from './input/camera_access';
|
||||||
import ImageDebug from './common/image_debug';
|
import ImageDebug from './common/image_debug';
|
||||||
import ResultCollector from './analytics/result_collector';
|
import ResultCollector from './analytics/result_collector';
|
||||||
@@ -34,7 +34,9 @@ function createScanner() {
|
|||||||
_workerPool = [],
|
_workerPool = [],
|
||||||
_onUIThread = true,
|
_onUIThread = true,
|
||||||
_resultCollector,
|
_resultCollector,
|
||||||
_config = {};
|
_config = {},
|
||||||
|
_events = createEventedElement(),
|
||||||
|
_locator;
|
||||||
|
|
||||||
function initializeData(imageWrapper) {
|
function initializeData(imageWrapper) {
|
||||||
initBuffers(imageWrapper);
|
initBuffers(imageWrapper);
|
||||||
@@ -85,7 +87,7 @@ function createScanner() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
function canRecord(cb) {
|
function canRecord(cb) {
|
||||||
BarcodeLocator.checkImageConstraints(_inputStream, _config.locator);
|
checkImageConstraints(_inputStream, _config.locator);
|
||||||
initCanvas(_config);
|
initCanvas(_config);
|
||||||
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
|
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
|
||||||
|
|
||||||
@@ -155,12 +157,12 @@ function createScanner() {
|
|||||||
vec2.clone([_inputImageWrapper.size.x, _inputImageWrapper.size.y]),
|
vec2.clone([_inputImageWrapper.size.x, _inputImageWrapper.size.y]),
|
||||||
vec2.clone([_inputImageWrapper.size.x, 0])
|
vec2.clone([_inputImageWrapper.size.x, 0])
|
||||||
];
|
];
|
||||||
BarcodeLocator.init(_inputImageWrapper, _config.locator);
|
_locator = createLocator(_inputImageWrapper, _config.locator);
|
||||||
}
|
}
|
||||||
|
|
||||||
function getBoundingBoxes() {
|
function getBoundingBoxes() {
|
||||||
if (_config.locate) {
|
if (_config.locate) {
|
||||||
return BarcodeLocator.locate();
|
return _locator.locate();
|
||||||
} else {
|
} else {
|
||||||
return [[
|
return [[
|
||||||
vec2.clone(_boxSize[0]),
|
vec2.clone(_boxSize[0]),
|
||||||
@@ -245,9 +247,9 @@ function createScanner() {
|
|||||||
resultToPublish = result.barcodes || result;
|
resultToPublish = result.barcodes || result;
|
||||||
}
|
}
|
||||||
|
|
||||||
Events.publish("processed", resultToPublish);
|
_events.publish("processed", resultToPublish);
|
||||||
if (hasCodeResult(result)) {
|
if (hasCodeResult(result)) {
|
||||||
Events.publish("detected", resultToPublish);
|
_events.publish("detected", resultToPublish);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -498,16 +500,16 @@ function createScanner() {
|
|||||||
_stopped = true;
|
_stopped = true;
|
||||||
},
|
},
|
||||||
onDetected: function(callback) {
|
onDetected: function(callback) {
|
||||||
Events.subscribe("detected", callback);
|
_events.subscribe("detected", callback);
|
||||||
},
|
},
|
||||||
offDetected: function(callback) {
|
offDetected: function(callback) {
|
||||||
Events.unsubscribe("detected", callback);
|
_events.unsubscribe("detected", callback);
|
||||||
},
|
},
|
||||||
onProcessed: function(callback) {
|
onProcessed: function(callback) {
|
||||||
Events.subscribe("processed", callback);
|
_events.subscribe("processed", callback);
|
||||||
},
|
},
|
||||||
offProcessed: function(callback) {
|
offProcessed: function(callback) {
|
||||||
Events.unsubscribe("processed", callback);
|
_events.unsubscribe("processed", callback);
|
||||||
},
|
},
|
||||||
registerResultCollector: function(resultCollector) {
|
registerResultCollector: function(resultCollector) {
|
||||||
if (resultCollector && typeof resultCollector.addResult === 'function') {
|
if (resultCollector && typeof resultCollector.addResult === 'function') {
|
||||||
@@ -516,7 +518,7 @@ function createScanner() {
|
|||||||
},
|
},
|
||||||
decodeSingle: function(config, resultCallback) {
|
decodeSingle: function(config, resultCallback) {
|
||||||
this.init(config, () => {
|
this.init(config, () => {
|
||||||
Events.once("processed", (result) => {
|
_events.once("processed", (result) => {
|
||||||
this.stop();
|
this.stop();
|
||||||
resultCallback.call(null, result);
|
resultCallback.call(null, result);
|
||||||
}, true);
|
}, true);
|
||||||
|
|||||||
Reference in New Issue
Block a user