Fix parallel image processing

pull/319/head
Adam Dobrawy 7 years ago
parent a4de46f9a9
commit fe6a2e8be1

754
dist/quagga.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

10421
package-lock.json generated

File diff suppressed because it is too large Load Diff

@ -14,7 +14,8 @@ const vec2 = {
clone: require('gl-vec2/clone')
};
var _inputStream,
exports.default = function() {
var _inputStream,
_framegrabber,
_stopped,
_canvasContainer = {
@ -35,12 +36,12 @@ var _inputStream,
_resultCollector,
_config = {};
function initializeData(imageWrapper) {
function initializeData(imageWrapper) {
initBuffers(imageWrapper);
_decoder = BarcodeDecoder.create(_config.decoder, _inputImageWrapper);
}
}
function initInputStream(cb) {
function initInputStream(cb) {
var video;
if (_config.inputStream.type === "VideoStream") {
video = document.createElement("video");
@ -68,9 +69,9 @@ function initInputStream(cb) {
_inputStream.setAttribute("preload", "auto");
_inputStream.setInputStream(_config.inputStream);
_inputStream.addEventListener("canrecord", canRecord.bind(undefined, cb));
}
}
function getViewPort() {
function getViewPort() {
var target = _config.inputStream.target;
// Check if target is already a DOM element
if (target && target.nodeName && target.nodeType === 1) {
@ -80,27 +81,27 @@ function getViewPort() {
var selector = typeof target === 'string' ? target : '#interactive.viewport';
return document.querySelector(selector);
}
}
}
function canRecord(cb) {
function canRecord(cb) {
BarcodeLocator.checkImageConstraints(_inputStream, _config.locator);
initCanvas(_config);
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
adjustWorkerPool(_config.numOfWorkers, function() {
adjustWorkerPool(_config.numOfWorkers, function () {
if (_config.numOfWorkers === 0) {
initializeData();
}
ready(cb);
});
}
}
function ready(cb){
function ready(cb) {
_inputStream.play();
cb();
}
}
function initCanvas() {
function initCanvas() {
if (typeof document !== "undefined") {
var $viewport = getViewPort();
_canvasContainer.dom.image = document.querySelector("canvas.imgBuffer");
@ -132,9 +133,9 @@ function initCanvas() {
_canvasContainer.dom.overlay.width = _inputStream.getCanvasSize().x;
_canvasContainer.dom.overlay.height = _inputStream.getCanvasSize().y;
}
}
}
function initBuffers(imageWrapper) {
function initBuffers(imageWrapper) {
if (imageWrapper) {
_inputImageWrapper = imageWrapper;
} else {
@ -154,9 +155,9 @@ function initBuffers(imageWrapper) {
vec2.clone([_inputImageWrapper.size.x, 0])
];
BarcodeLocator.init(_inputImageWrapper, _config.locator);
}
}
function getBoundingBoxes() {
function getBoundingBoxes() {
if (_config.locate) {
return BarcodeLocator.locate();
} else {
@ -166,9 +167,9 @@ function getBoundingBoxes() {
vec2.clone(_boxSize[2]),
vec2.clone(_boxSize[3])]];
}
}
}
function transformResult(result) {
function transformResult(result) {
var topRight = _inputStream.getTopRight(),
xOffset = topRight.x,
yOffset = topRight.y,
@ -213,9 +214,9 @@ function transformResult(result) {
line[1].x += xOffset;
line[1].y += yOffset;
}
}
}
function addResult (result, imageData) {
function addResult(result, imageData) {
if (!imageData || !_resultCollector) {
return;
}
@ -226,15 +227,15 @@ function addResult (result, imageData) {
} else if (result.codeResult) {
_resultCollector.addResult(imageData, _inputStream.getCanvasSize(), result.codeResult);
}
}
}
function hasCodeResult (result) {
function hasCodeResult(result) {
return result && (result.barcodes ?
result.barcodes.some(barcode => barcode.codeResult) :
result.codeResult);
}
}
function publishResult(result, imageData) {
function publishResult(result, imageData) {
let resultToPublish = result;
if (result && _onUIThread) {
@ -247,9 +248,9 @@ function publishResult(result, imageData) {
if (hasCodeResult(result)) {
Events.publish("detected", resultToPublish);
}
}
}
function locateAndDecode() {
function locateAndDecode() {
var result,
boxes;
@ -262,14 +263,14 @@ function locateAndDecode() {
} else {
publishResult();
}
}
}
function update() {
function update() {
var availableWorker;
if (_onUIThread) {
if (_workerPool.length > 0) {
availableWorker = _workerPool.filter(function(workerThread) {
availableWorker = _workerPool.filter(function (workerThread) {
return !workerThread.busy;
})[0];
if (availableWorker) {
@ -294,9 +295,9 @@ function update() {
} else {
locateAndDecode();
}
}
}
function startContinuousUpdate() {
function startContinuousUpdate() {
var next = null,
delay = 1000 / (_config.frequency || 60);
@ -311,17 +312,17 @@ function startContinuousUpdate() {
window.requestAnimFrame(frame);
}
}(performance.now()));
}
}
function start() {
function start() {
if (_onUIThread && _config.inputStream.type === "LiveStream") {
startContinuousUpdate();
} else {
update();
}
}
}
function initWorker(cb) {
function initWorker(cb) {
var blobURL,
workerThread = {
worker: undefined,
@ -332,7 +333,7 @@ function initWorker(cb) {
blobURL = generateWorkerBlob();
workerThread.worker = new Worker(blobURL);
workerThread.worker.onmessage = function(e) {
workerThread.worker.onmessage = function (e) {
if (e.data.event === 'initialized') {
URL.revokeObjectURL(blobURL);
workerThread.busy = false;
@ -358,9 +359,9 @@ function initWorker(cb) {
imageData: workerThread.imageData,
config: configForWorker(_config)
}, [workerThread.imageData.buffer]);
}
}
function configForWorker(config) {
function configForWorker(config) {
return {
...config,
inputStream: {
@ -368,9 +369,9 @@ function configForWorker(config) {
target: null
}
};
}
}
function workerInterface(factory) {
function workerInterface(factory) {
/* eslint-disable no-undef*/
if (factory) {
var Quagga = factory().default;
@ -381,7 +382,7 @@ function workerInterface(factory) {
}
var imageWrapper;
self.onmessage = function(e) {
self.onmessage = function (e) {
if (e.data.cmd === 'init') {
var config = e.data.config;
config.numOfWorkers = 0;
@ -412,9 +413,9 @@ function workerInterface(factory) {
}
/* eslint-enable */
}
}
function generateWorkerBlob() {
function generateWorkerBlob() {
var blob,
factorySource;
@ -428,26 +429,26 @@ function generateWorkerBlob() {
{type: 'text/javascript'});
return window.URL.createObjectURL(blob);
}
}
function setReaders(readers) {
function setReaders(readers) {
if (_decoder) {
_decoder.setReaders(readers);
} else if (_onUIThread && _workerPool.length > 0) {
_workerPool.forEach(function(workerThread) {
_workerPool.forEach(function (workerThread) {
workerThread.worker.postMessage({cmd: 'setReaders', readers: readers});
});
}
}
}
function adjustWorkerPool(capacity, cb) {
function adjustWorkerPool(capacity, cb) {
const increaseBy = capacity - _workerPool.length;
if (increaseBy === 0) {
return cb && cb();
}
if (increaseBy < 0) {
const workersToTerminate = _workerPool.slice(increaseBy);
workersToTerminate.forEach(function(workerThread) {
workersToTerminate.forEach(function (workerThread) {
workerThread.worker.terminate();
if (ENV.development) {
console.log("Worker terminated!");
@ -462,15 +463,15 @@ function adjustWorkerPool(capacity, cb) {
function workerInitialized(workerThread) {
_workerPool.push(workerThread);
if (_workerPool.length >= capacity){
if (_workerPool.length >= capacity) {
cb && cb();
}
}
}
}
}
export default {
init: function(config, cb, imageWrapper) {
return {
init: function (config, cb, imageWrapper) {
_config = merge({}, Config, config);
if (imageWrapper) {
_onUIThread = false;
@ -480,10 +481,10 @@ export default {
initInputStream(cb);
}
},
start: function() {
start: function () {
start();
},
stop: function() {
stop: function () {
_stopped = true;
adjustWorkerPool(0);
if (_config.inputStream.type === "LiveStream") {
@ -491,31 +492,31 @@ export default {
_inputStream.clearEventHandlers();
}
},
pause: function() {
pause: function () {
_stopped = true;
},
onDetected: function(callback) {
onDetected: function (callback) {
Events.subscribe("detected", callback);
},
offDetected: function(callback) {
offDetected: function (callback) {
Events.unsubscribe("detected", callback);
},
onProcessed: function(callback) {
onProcessed: function (callback) {
Events.subscribe("processed", callback);
},
offProcessed: function(callback) {
offProcessed: function (callback) {
Events.unsubscribe("processed", callback);
},
setReaders: function(readers) {
setReaders: function (readers) {
setReaders(readers);
},
registerResultCollector: function(resultCollector) {
registerResultCollector: function (resultCollector) {
if (resultCollector && typeof resultCollector.addResult === 'function') {
_resultCollector = resultCollector;
}
},
canvas: _canvasContainer,
decodeSingle: function(config, resultCallback) {
decodeSingle: function (config, resultCallback) {
config = merge({
inputStream: {
type: "ImageStream",
@ -540,4 +541,5 @@ export default {
ImageDebug: ImageDebug,
ResultCollector: ResultCollector,
CameraAccess: CameraAccess,
};
};
}
Loading…
Cancel
Save