mirror of
https://github.com/serratus/quaggaJS.git
synced 2026-08-12 21:21:42 +08:00
Added tests for image-size calculation
This commit is contained in:
@@ -0,0 +1,65 @@
|
|||||||
|
|
||||||
|
define(['barcode_locator', 'config', 'html_utils'],
|
||||||
|
function(BarcodeLocator, Config, HtmlUtils){
|
||||||
|
|
||||||
|
describe('checkImageConstraints', function() {
|
||||||
|
var config,
|
||||||
|
inputStream,
|
||||||
|
imageSize;
|
||||||
|
|
||||||
|
beforeEach(function() {
|
||||||
|
imageSize = {
|
||||||
|
x: 640, y: 480
|
||||||
|
};
|
||||||
|
config = HtmlUtils.mergeObjects({}, Config);
|
||||||
|
inputStream = {
|
||||||
|
getWidth: function() {
|
||||||
|
return imageSize.x;
|
||||||
|
},
|
||||||
|
getHeight: function() {
|
||||||
|
return imageSize.y;
|
||||||
|
},
|
||||||
|
setWidth: function() {},
|
||||||
|
setHeight: function() {}
|
||||||
|
};
|
||||||
|
sinon.stub(inputStream, "setWidth", function(width) {
|
||||||
|
imageSize.x = width;
|
||||||
|
});
|
||||||
|
sinon.stub(inputStream, "setHeight", function(height) {
|
||||||
|
imageSize.y = height;
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
afterEach(function() {
|
||||||
|
inputStream.setWidth.restore();
|
||||||
|
inputStream.setHeight.restore();
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should not adjust the image-size if not needed', function() {
|
||||||
|
var expected = {x: imageSize.x, y: imageSize.y};
|
||||||
|
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||||
|
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||||
|
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should adjust the image-size', function() {
|
||||||
|
var expected = {x: imageSize.x, y: imageSize.y};
|
||||||
|
|
||||||
|
config.locator.halfSample = true;
|
||||||
|
imageSize.y += 1;
|
||||||
|
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||||
|
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||||
|
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should adjust the image-size', function() {
|
||||||
|
var expected = {x: imageSize.x, y: imageSize.y};
|
||||||
|
|
||||||
|
imageSize.y += 1;
|
||||||
|
config.locator.halfSample = false;
|
||||||
|
BarcodeLocator.checkImageConstraints(inputStream, config.locator);
|
||||||
|
expect(inputStream.getHeight()).to.be.equal(expected.y);
|
||||||
|
expect(inputStream.getWidth()).to.be.equal(expected.x);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
});
|
||||||
@@ -8,4 +8,20 @@ define(['cv_utils'], function(CVUtils){
|
|||||||
expect(res.toVec2()[0]).to.equal(1);
|
expect(res.toVec2()[0]).to.equal(1);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
|
describe('calculatePatchSize', function() {
|
||||||
|
it('should not throw an error in case of valid image size', function() {
|
||||||
|
var expected = {x: 32, y: 32},
|
||||||
|
patchSize = CVUtils.calculatePatchSize("medium", {x: 640, y: 480});
|
||||||
|
|
||||||
|
expect(patchSize).to.be.deep.equal(expected);
|
||||||
|
});
|
||||||
|
|
||||||
|
it('should thow an error if image size it not valid', function() {
|
||||||
|
var expected = {x: 32, y: 32},
|
||||||
|
patchSize = CVUtils.calculatePatchSize("medium", {x: 640, y: 480});
|
||||||
|
|
||||||
|
expect(patchSize).to.be.deep.equal(expected);
|
||||||
|
});
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -486,6 +486,7 @@ function(ImageWrapper, CVUtils, Rasterizer, Tracer, skeletonizer, ArrayHelper, I
|
|||||||
initBuffers();
|
initBuffers();
|
||||||
initCanvas();
|
initCanvas();
|
||||||
},
|
},
|
||||||
|
|
||||||
locate : function() {
|
locate : function() {
|
||||||
var patchesFound,
|
var patchesFound,
|
||||||
topLabels = [],
|
topLabels = [],
|
||||||
@@ -516,6 +517,30 @@ function(ImageWrapper, CVUtils, Rasterizer, Tracer, skeletonizer, ArrayHelper, I
|
|||||||
|
|
||||||
boxes = findBoxes(topLabels, maxLabel);
|
boxes = findBoxes(topLabels, maxLabel);
|
||||||
return boxes;
|
return boxes;
|
||||||
|
},
|
||||||
|
|
||||||
|
checkImageConstraints: function(inputStream, config) {
|
||||||
|
var patchSize,
|
||||||
|
width = inputStream.getWidth(),
|
||||||
|
height = inputStream.getHeight(),
|
||||||
|
halfSample = config.halfSample ? 0.5 : 1,
|
||||||
|
size = {
|
||||||
|
x: Math.floor(width * halfSample),
|
||||||
|
y: Math.floor(height * halfSample)
|
||||||
|
};
|
||||||
|
|
||||||
|
patchSize = CVUtils.calculatePatchSize(config.patchSize, size);
|
||||||
|
inputStream.setWidth(Math.floor(Math.floor(size.x/patchSize.x)*(1/halfSample)*patchSize.x));
|
||||||
|
inputStream.setHeight(Math.floor(Math.floor(size.y/patchSize.y)*(1/halfSample)*patchSize.y));
|
||||||
|
|
||||||
|
console.log("Patch-Size: " + JSON.stringify(patchSize));
|
||||||
|
if ((inputStream.getWidth() % patchSize.x) === 0 && (inputStream.getHeight() % patchSize.y) === 0) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
throw new Error("Image dimensions do not comply with the current settings: Width (" +
|
||||||
|
width + " )and height (" + height +
|
||||||
|
") must a multiple of " + patchSize.x);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -647,7 +647,6 @@ define(['cluster', 'glMatrixAddon', "array_helper"], function(Cluster2, glMatrix
|
|||||||
optimalPatchSize = findPatchSizeForDivisors(common);
|
optimalPatchSize = findPatchSizeForDivisors(common);
|
||||||
if (!optimalPatchSize) {
|
if (!optimalPatchSize) {
|
||||||
optimalPatchSize = findPatchSizeForDivisors(this._computeDivisors(wideSide));
|
optimalPatchSize = findPatchSizeForDivisors(this._computeDivisors(wideSide));
|
||||||
throw new AdjustToSizeError("", optimalPatchSize);
|
|
||||||
}
|
}
|
||||||
return optimalPatchSize;
|
return optimalPatchSize;
|
||||||
};
|
};
|
||||||
|
|||||||
+5
-36
@@ -14,8 +14,7 @@ define([
|
|||||||
"config",
|
"config",
|
||||||
"events",
|
"events",
|
||||||
"camera_access",
|
"camera_access",
|
||||||
"image_debug",
|
"image_debug"],
|
||||||
"cv_utils"],
|
|
||||||
function(Code128Reader,
|
function(Code128Reader,
|
||||||
EANReader,
|
EANReader,
|
||||||
InputStream,
|
InputStream,
|
||||||
@@ -27,8 +26,7 @@ function(Code128Reader,
|
|||||||
_config,
|
_config,
|
||||||
Events,
|
Events,
|
||||||
CameraAccess,
|
CameraAccess,
|
||||||
ImageDebug,
|
ImageDebug) {
|
||||||
CVUtils) {
|
|
||||||
"use strict";
|
"use strict";
|
||||||
|
|
||||||
var _inputStream,
|
var _inputStream,
|
||||||
@@ -107,39 +105,10 @@ function(Code128Reader,
|
|||||||
_inputStream.addEventListener("canrecord", canRecord.bind(undefined, cb));
|
_inputStream.addEventListener("canrecord", canRecord.bind(undefined, cb));
|
||||||
}
|
}
|
||||||
|
|
||||||
function checkImageConstraints() {
|
|
||||||
var patchSize,
|
|
||||||
width = _inputStream.getWidth(),
|
|
||||||
height = _inputStream.getHeight(),
|
|
||||||
halfSample = _config.locator.halfSample ? 0.5 : 1,
|
|
||||||
size = {
|
|
||||||
x: Math.floor(width * halfSample),
|
|
||||||
y: Math.floor(height * halfSample)
|
|
||||||
};
|
|
||||||
|
|
||||||
if (_config.locate) {
|
|
||||||
try {
|
|
||||||
console.log(size);
|
|
||||||
patchSize = CVUtils.calculatePatchSize(_config.locator.patchSize, size);
|
|
||||||
} catch (error) {
|
|
||||||
if (error instanceof CVUtils.AdjustToSizeError) {
|
|
||||||
_inputStream.setWidth(Math.floor(Math.floor(size.x/error.patchSize.x)*(1/halfSample)*error.patchSize.x));
|
|
||||||
_inputStream.setHeight(Math.floor(Math.floor(size.y/error.patchSize.y)*(1/halfSample)*error.patchSize.y));
|
|
||||||
patchSize = error.patchSize;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
console.log("Patch-Size: " + JSON.stringify(patchSize));
|
|
||||||
if ((_inputStream.getWidth() % patchSize.x) === 0 && (_inputStream.getHeight() % patchSize.y) === 0) {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
throw new Error("Image dimensions do not comply with the current settings: Width (" +
|
|
||||||
width + " )and height (" + height +
|
|
||||||
") must a multiple of " + patchSize.x);
|
|
||||||
}
|
|
||||||
|
|
||||||
function canRecord(cb) {
|
function canRecord(cb) {
|
||||||
checkImageConstraints();
|
if (_config.locate) {
|
||||||
|
BarcodeLocator.checkImageConstraints(_inputStream, _config.locator);
|
||||||
|
}
|
||||||
initCanvas();
|
initCanvas();
|
||||||
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
|
_framegrabber = FrameGrabber.create(_inputStream, _canvasContainer.dom.image);
|
||||||
initConfig();
|
initConfig();
|
||||||
|
|||||||
Reference in New Issue
Block a user