Skip to content

Commit

Permalink
Merge pull request #169 from ericblade/dev2
Browse files Browse the repository at this point in the history
Cleanup and separating some functionality out to quagga directory
  • Loading branch information
ericblade authored Apr 15, 2020
2 parents 52490dd + 6309a9c commit 73f9762
Show file tree
Hide file tree
Showing 14 changed files with 4,510 additions and 9,525 deletions.
2 changes: 1 addition & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
"dot-notation": "error",
"eol-last": "error",
"func-call-spacing": [ "error", "never" ],
"indent": ["error", 4],
"indent": ["error", 4, { "SwitchCase": 1 }],
"key-spacing": ["warn", { "beforeColon": false, "afterColon": true }],
"keyword-spacing": [ "error", { "after": true }],
"max-len" : ["warn", 120],
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/nodejs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:

strategy:
matrix:
node-version: [8.x, 10.x, 12.x, 13.x]
node-version: [10.x, 12.x, 13.x]
os: [ubuntu-latest, windows-latest, macOS-latest]

steps:
Expand Down
13,156 changes: 4,001 additions & 9,155 deletions package-lock.json

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@ericblade/quagga2",
"version": "0.0.19",
"version": "0.0.20",
"description": "An advanced barcode-scanner written in JavaScript",
"main": "lib/quagga.js",
"types": "type-definitions/quagga.d.ts",
Expand Down
56 changes: 28 additions & 28 deletions src/input/frame_grabber_node.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,33 @@
const CVUtils = require('../common/cv_utils'),
Ndarray = require("ndarray"),
Interp2D = require("ndarray-linear-interpolate").d2;
const CVUtils = require('../common/cv_utils');
const Ndarray = require('ndarray');
const Interp2D = require('ndarray-linear-interpolate').d2;

var FrameGrabber = {};
const FrameGrabber = {};

FrameGrabber.create = function(inputStream) {
var _that = {},
_streamConfig = inputStream.getConfig(),
_video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight()),
_canvasSize = inputStream.getCanvasSize(),
_size = CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight()),
_topRight = inputStream.getTopRight(),
_data = new Uint8Array(_size.x * _size.y),
_grayData = new Uint8Array(_video_size.x * _video_size.y),
_canvasData = new Uint8Array(_canvasSize.x * _canvasSize.y),
_grayImageArray = Ndarray(_grayData, [_video_size.y, _video_size.x]).transpose(1, 0),
_canvasImageArray = Ndarray(_canvasData, [_canvasSize.y, _canvasSize.x]).transpose(1, 0),
_targetImageArray = _canvasImageArray.hi(_topRight.x + _size.x, _topRight.y + _size.y).lo(_topRight.x, _topRight.y),
_stepSizeX = _video_size.x/_canvasSize.x,
_stepSizeY = _video_size.y/_canvasSize.y;
const _that = {};
const _video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight());
const _canvasSize = inputStream.getCanvasSize();
const _size = CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight());
const _topRight = inputStream.getTopRight();
let _data = new Uint8Array(_size.x * _size.y);
const _grayData = new Uint8Array(_video_size.x * _video_size.y);
const _canvasData = new Uint8Array(_canvasSize.x * _canvasSize.y);
/* eslint-disable new-cap */
const _grayImageArray = Ndarray(_grayData, [_video_size.y, _video_size.x]).transpose(1, 0);
const _canvasImageArray = Ndarray(_canvasData, [_canvasSize.y, _canvasSize.x]).transpose(1, 0);
const _targetImageArray = _canvasImageArray
.hi(_topRight.x + _size.x, _topRight.y + _size.y)
.lo(_topRight.x, _topRight.y);
const _stepSizeX = _video_size.x / _canvasSize.x;
const _stepSizeY = _video_size.y / _canvasSize.y;

console.log("FrameGrabber", JSON.stringify({
console.log('FrameGrabber', JSON.stringify({
videoSize: _grayImageArray.shape,
canvasSize: _canvasImageArray.shape,
stepSize: [_stepSizeX, _stepSizeY],
size: _targetImageArray.shape,
topRight: _topRight
topRight: _topRight,
}));

/**
Expand Down Expand Up @@ -57,29 +59,27 @@ FrameGrabber.create = function(inputStream) {
}
};

// eslint-disable-next-line
_that.scaleAndCrop = function(frame) {
var x,
y;

// 1. compute full-sized gray image
CVUtils.computeGray(frame.data, _grayData);

// 2. interpolate
for (y = 0; y < _canvasSize.y; y++) {
for (x = 0; x < _canvasSize.x; x++) {
for (let y = 0; y < _canvasSize.y; y++) {
for (let x = 0; x < _canvasSize.x; x++) {
_canvasImageArray.set(x, y, (Interp2D(_grayImageArray, x * _stepSizeX, y * _stepSizeY)) | 0);
}
}

// targetImageArray must be equal to targetSize
if (_targetImageArray.shape[0] !== _size.x ||
_targetImageArray.shape[1] !== _size.y) {
throw new Error("Shapes do not match!");
throw new Error('Shapes do not match!');
}

// 3. crop
for (y = 0; y < _size.y; y++) {
for (x = 0; x < _size.x; x++) {
for (let y = 0; y < _size.y; y++) {
for (let x = 0; x < _size.x; x++) {
_data[y * _size.x + x] = _targetImageArray.get(x, y);
}
}
Expand Down
69 changes: 40 additions & 29 deletions src/input/input_stream_node.js
Original file line number Diff line number Diff line change
@@ -1,29 +1,32 @@
const GetPixels = require("get-pixels");
const GetPixels = require('get-pixels');

var InputStream = {};

InputStream.createImageStream = function() {
var that = {};
var _config = null;

var width = 0,
height = 0,
frameIdx = 0,
paused = true,
loaded = false,
frame = null,
baseUrl,
ended = false,
size,
calculatedWidth,
calculatedHeight,
_eventNames = ['canrecord', 'ended'],
_eventHandlers = {},
_topRight = {x: 0, y: 0},
_canvasSize = {x: 0, y: 0};
const that = {};
let _config = null;

let width = 0;
let height = 0;
let loaded = false;
let frame = null;
let baseUrl;
let ended = false;
let calculatedWidth;
let calculatedHeight;
const _eventNames = ['canrecord', 'ended'];
const _eventHandlers = {};
const _topRight = {x: 0, y: 0};
const _canvasSize = {x: 0, y: 0};
/* eslint-disable no-unused-vars */ // false eslint errors? weird.
let size = 0;
let frameIdx = 0;
let paused = false;
/* eslint-enable no-unused-vars */

function loadImages() {
loaded = false;
/* eslint-disable new-cap */
GetPixels(baseUrl, _config.mime, function(err, pixels) {
if (err) {
console.error('**** quagga loadImages error:', err);
Expand All @@ -34,14 +37,22 @@ InputStream.createImageStream = function() {
frame = pixels;
width = pixels.shape[0];
height = pixels.shape[1];
calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
calculatedWidth = _config.size ?
width / height > 1 ?
_config.size
: Math.floor((width / height) * _config.size)
: width;
calculatedHeight = _config.size ?
width / height > 1 ?
Math.floor((height / width) * _config.size)
: _config.size
: height;

_canvasSize.x = calculatedWidth;
_canvasSize.y = calculatedHeight;

setTimeout(function() {
publishEvent("canrecord", []);
publishEvent('canrecord', []);
}, 0);
});
}
Expand All @@ -68,12 +79,12 @@ InputStream.createImageStream = function() {
return calculatedHeight;
};

that.setWidth = function(width) {
calculatedWidth = width;
that.setWidth = function(w) {
calculatedWidth = w;
};

that.setHeight = function(height) {
calculatedHeight = height;
that.setHeight = function(h) {
calculatedHeight = h;
};

that.getRealWidth = function() {
Expand Down Expand Up @@ -131,9 +142,9 @@ InputStream.createImageStream = function() {
return _topRight;
};

that.setCanvasSize = function(size) {
_canvasSize.x = size.x;
_canvasSize.y = size.y;
that.setCanvasSize = function(sz) {
_canvasSize.x = sz.x;
_canvasSize.y = sz.y;
};

that.getCanvasSize = function() {
Expand Down
Loading

0 comments on commit 73f9762

Please sign in to comment.