version for node working
parent
9295e41655
commit
6a56d7a1c2
File diff suppressed because it is too large
Load Diff
File diff suppressed because one or more lines are too long
@ -1,101 +1,95 @@
|
|||||||
/* jshint undef: true, unused: true, browser:true, devel: true */
|
const CVUtils = require('../src/cv_utils'),
|
||||||
/* global define */
|
Ndarray = require("ndarray"),
|
||||||
|
Interp2D = require("ndarray-linear-interpolate").d2;
|
||||||
define(["cv_utils"], function(CVUtils) {
|
|
||||||
"use strict";
|
var FrameGrabber = {};
|
||||||
|
|
||||||
var Ndarray = require("ndarray"),
|
FrameGrabber.create = function(inputStream) {
|
||||||
Interp2D = require("ndarray-linear-interpolate").d2;
|
var _that = {},
|
||||||
|
_streamConfig = inputStream.getConfig(),
|
||||||
var FrameGrabber = {};
|
_video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight()),
|
||||||
|
_canvasSize = inputStream.getCanvasSize(),
|
||||||
FrameGrabber.create = function(inputStream) {
|
_size = CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight()),
|
||||||
var _that = {},
|
_topRight = inputStream.getTopRight(),
|
||||||
_streamConfig = inputStream.getConfig(),
|
_data = new Uint8Array(_size.x * _size.y),
|
||||||
_video_size = CVUtils.imageRef(inputStream.getRealWidth(), inputStream.getRealHeight()),
|
_grayData = new Uint8Array(_video_size.x * _video_size.y),
|
||||||
_canvasSize = inputStream.getCanvasSize(),
|
_canvasData = new Uint8Array(_canvasSize.x * _canvasSize.y),
|
||||||
_size = CVUtils.imageRef(inputStream.getWidth(), inputStream.getHeight()),
|
_grayImageArray = Ndarray(_grayData, [_video_size.y, _video_size.x]).transpose(1, 0),
|
||||||
_topRight = inputStream.getTopRight(),
|
_canvasImageArray = Ndarray(_canvasData, [_canvasSize.y, _canvasSize.x]).transpose(1, 0),
|
||||||
_data = new Uint8Array(_size.x * _size.y),
|
_targetImageArray = _canvasImageArray.hi(_topRight.x + _size.x, _topRight.y + _size.y).lo(_topRight.x, _topRight.y),
|
||||||
_grayData = new Uint8Array(_video_size.x * _video_size.y),
|
_stepSizeX = _video_size.x/_canvasSize.x,
|
||||||
_canvasData = new Uint8Array(_canvasSize.x * _canvasSize.y),
|
_stepSizeY = _video_size.y/_canvasSize.y;
|
||||||
_grayImageArray = Ndarray(_grayData, [_video_size.y, _video_size.x]).transpose(1, 0),
|
|
||||||
_canvasImageArray = Ndarray(_canvasData, [_canvasSize.y, _canvasSize.x]).transpose(1, 0),
|
console.log("FrameGrabber", JSON.stringify({
|
||||||
_targetImageArray = _canvasImageArray.hi(_topRight.x + _size.x, _topRight.y + _size.y).lo(_topRight.x, _topRight.y),
|
videoSize: _grayImageArray.shape,
|
||||||
_stepSizeX = _video_size.x/_canvasSize.x,
|
canvasSize: _canvasImageArray.shape,
|
||||||
_stepSizeY = _video_size.y/_canvasSize.y;
|
stepSize: [_stepSizeX, _stepSizeY],
|
||||||
|
size: _targetImageArray.shape,
|
||||||
console.log("FrameGrabber", JSON.stringify({
|
topRight: _topRight
|
||||||
videoSize: _grayImageArray.shape,
|
}));
|
||||||
canvasSize: _canvasImageArray.shape,
|
|
||||||
stepSize: [_stepSizeX, _stepSizeY],
|
/**
|
||||||
size: _targetImageArray.shape,
|
* Uses the given array as frame-buffer
|
||||||
topRight: _topRight
|
*/
|
||||||
}));
|
_that.attachData = function(data) {
|
||||||
|
_data = data;
|
||||||
/**
|
};
|
||||||
* Uses the given array as frame-buffer
|
|
||||||
*/
|
|
||||||
_that.attachData = function(data) {
|
|
||||||
_data = data;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Returns the used frame-buffer
|
|
||||||
*/
|
|
||||||
_that.getData = function() {
|
|
||||||
return _data;
|
|
||||||
};
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Fetches a frame from the input-stream and puts into the frame-buffer.
|
|
||||||
* The image-data is converted to gray-scale and then half-sampled if configured.
|
|
||||||
*/
|
|
||||||
_that.grab = function() {
|
|
||||||
var frame = inputStream.getFrame();
|
|
||||||
|
|
||||||
if (frame) {
|
/**
|
||||||
this.scaleAndCrop(frame);
|
* Returns the used frame-buffer
|
||||||
return true;
|
*/
|
||||||
} else {
|
_that.getData = function() {
|
||||||
return false;
|
return _data;
|
||||||
}
|
};
|
||||||
};
|
|
||||||
|
|
||||||
_that.scaleAndCrop = function(frame) {
|
/**
|
||||||
var x,
|
* Fetches a frame from the input-stream and puts into the frame-buffer.
|
||||||
y;
|
* The image-data is converted to gray-scale and then half-sampled if configured.
|
||||||
|
*/
|
||||||
|
_that.grab = function() {
|
||||||
|
var frame = inputStream.getFrame();
|
||||||
|
|
||||||
|
if (frame) {
|
||||||
|
this.scaleAndCrop(frame);
|
||||||
|
return true;
|
||||||
|
} else {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
// 1. compute full-sized gray image
|
_that.scaleAndCrop = function(frame) {
|
||||||
CVUtils.computeGray(frame.data, _grayData);
|
var x,
|
||||||
|
y;
|
||||||
|
|
||||||
// 2. interpolate
|
// 1. compute full-sized gray image
|
||||||
for (y = 0; y < _canvasSize.y; y++) {
|
CVUtils.computeGray(frame.data, _grayData);
|
||||||
for (x = 0; x < _canvasSize.x; x++) {
|
|
||||||
_canvasImageArray.set(x, y, (Interp2D(_grayImageArray, x * _stepSizeX, y * _stepSizeY)) | 0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// targetImageArray must be equal to targetSize
|
// 2. interpolate
|
||||||
if (_targetImageArray.shape[0] !== _size.x ||
|
for (y = 0; y < _canvasSize.y; y++) {
|
||||||
_targetImageArray.shape[1] !== _size.y) {
|
for (x = 0; x < _canvasSize.x; x++) {
|
||||||
throw new Error("Shapes do not match!");
|
_canvasImageArray.set(x, y, (Interp2D(_grayImageArray, x * _stepSizeX, y * _stepSizeY)) | 0);
|
||||||
}
|
}
|
||||||
|
}
|
||||||
// 3. crop
|
|
||||||
for (y = 0; y < _size.y; y++) {
|
// targetImageArray must be equal to targetSize
|
||||||
for (x = 0; x < _size.x; x++) {
|
if (_targetImageArray.shape[0] !== _size.x ||
|
||||||
_data[y * _size.x + x] = _targetImageArray.get(x, y);
|
_targetImageArray.shape[1] !== _size.y) {
|
||||||
}
|
throw new Error("Shapes do not match!");
|
||||||
|
}
|
||||||
|
|
||||||
|
// 3. crop
|
||||||
|
for (y = 0; y < _size.y; y++) {
|
||||||
|
for (x = 0; x < _size.x; x++) {
|
||||||
|
_data[y * _size.x + x] = _targetImageArray.get(x, y);
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
|
},
|
||||||
|
|
||||||
_that.getSize = function() {
|
_that.getSize = function() {
|
||||||
return _size;
|
return _size;
|
||||||
};
|
|
||||||
|
|
||||||
return _that;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
return (FrameGrabber);
|
return _that;
|
||||||
});
|
};
|
||||||
|
|
||||||
|
module.exports = FrameGrabber;
|
||||||
|
@ -1,160 +1,153 @@
|
|||||||
/* jshint undef: true, unused: true, browser:true, devel: true */
|
const GetPixels = require("get-pixels");
|
||||||
/* global define */
|
|
||||||
|
var InputStream = {};
|
||||||
define(function() {
|
|
||||||
"use strict";
|
InputStream.createImageStream = function() {
|
||||||
|
var that = {};
|
||||||
var GetPixels = require("get-pixels");
|
var _config = null;
|
||||||
|
|
||||||
var InputStream = {};
|
var width = 0,
|
||||||
|
height = 0,
|
||||||
InputStream.createImageStream = function() {
|
frameIdx = 0,
|
||||||
var that = {};
|
paused = true,
|
||||||
var _config = null;
|
loaded = false,
|
||||||
|
frame = null,
|
||||||
var width = 0,
|
baseUrl,
|
||||||
height = 0,
|
ended = false,
|
||||||
frameIdx = 0,
|
size,
|
||||||
paused = true,
|
calculatedWidth,
|
||||||
loaded = false,
|
calculatedHeight,
|
||||||
frame = null,
|
_eventNames = ['canrecord', 'ended'],
|
||||||
baseUrl,
|
_eventHandlers = {},
|
||||||
ended = false,
|
_topRight = {x: 0, y: 0},
|
||||||
size,
|
_canvasSize = {x: 0, y: 0};
|
||||||
calculatedWidth,
|
|
||||||
calculatedHeight,
|
function loadImages() {
|
||||||
_eventNames = ['canrecord', 'ended'],
|
loaded = false;
|
||||||
_eventHandlers = {},
|
GetPixels(baseUrl, function(err, pixels) {
|
||||||
_topRight = {x: 0, y: 0},
|
if (err) {
|
||||||
_canvasSize = {x: 0, y: 0};
|
console.log(err);
|
||||||
|
exit(1);
|
||||||
function loadImages() {
|
}
|
||||||
loaded = false;
|
loaded = true;
|
||||||
GetPixels(baseUrl, function(err, pixels) {
|
console.log(pixels.shape);
|
||||||
if (err) {
|
frame = pixels;
|
||||||
console.log(err);
|
width = pixels.shape[0];
|
||||||
exit(1);
|
height = pixels.shape[1];
|
||||||
}
|
calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
|
||||||
loaded = true;
|
calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
|
||||||
console.log(pixels.shape);
|
|
||||||
frame = pixels;
|
_canvasSize.x = calculatedWidth;
|
||||||
width = pixels.shape[0];
|
_canvasSize.y = calculatedHeight;
|
||||||
height = pixels.shape[1];
|
|
||||||
calculatedWidth = _config.size ? width/height > 1 ? _config.size : Math.floor((width/height) * _config.size) : width;
|
setTimeout(function() {
|
||||||
calculatedHeight = _config.size ? width/height > 1 ? Math.floor((height/width) * _config.size) : _config.size : height;
|
publishEvent("canrecord", []);
|
||||||
|
}, 0);
|
||||||
_canvasSize.x = calculatedWidth;
|
});
|
||||||
_canvasSize.y = calculatedHeight;
|
}
|
||||||
|
|
||||||
setTimeout(function() {
|
function publishEvent(eventName, args) {
|
||||||
publishEvent("canrecord", []);
|
var j,
|
||||||
}, 0);
|
handlers = _eventHandlers[eventName];
|
||||||
});
|
|
||||||
}
|
if (handlers && handlers.length > 0) {
|
||||||
|
for ( j = 0; j < handlers.length; j++) {
|
||||||
function publishEvent(eventName, args) {
|
handlers[j].apply(that, args);
|
||||||
var j,
|
|
||||||
handlers = _eventHandlers[eventName];
|
|
||||||
|
|
||||||
if (handlers && handlers.length > 0) {
|
|
||||||
for ( j = 0; j < handlers.length; j++) {
|
|
||||||
handlers[j].apply(that, args);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
that.trigger = publishEvent;
|
that.trigger = publishEvent;
|
||||||
|
|
||||||
that.getWidth = function() {
|
that.getWidth = function() {
|
||||||
return calculatedWidth;
|
return calculatedWidth;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.getHeight = function() {
|
that.getHeight = function() {
|
||||||
return calculatedHeight;
|
return calculatedHeight;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.setWidth = function(width) {
|
that.setWidth = function(width) {
|
||||||
calculatedWidth = width;
|
calculatedWidth = width;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.setHeight = function(height) {
|
that.setHeight = function(height) {
|
||||||
calculatedHeight = height;
|
calculatedHeight = height;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.getRealWidth = function() {
|
that.getRealWidth = function() {
|
||||||
return width;
|
return width;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.getRealHeight = function() {
|
that.getRealHeight = function() {
|
||||||
return height;
|
return height;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.setInputStream = function(stream) {
|
that.setInputStream = function(stream) {
|
||||||
_config = stream;
|
_config = stream;
|
||||||
baseUrl = _config.src;
|
baseUrl = _config.src;
|
||||||
size = 1;
|
size = 1;
|
||||||
loadImages();
|
loadImages();
|
||||||
};
|
};
|
||||||
|
|
||||||
that.ended = function() {
|
that.ended = function() {
|
||||||
return ended;
|
return ended;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.setAttribute = function() {};
|
that.setAttribute = function() {};
|
||||||
|
|
||||||
that.getConfig = function() {
|
that.getConfig = function() {
|
||||||
return _config;
|
return _config;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.pause = function() {
|
that.pause = function() {
|
||||||
paused = true;
|
paused = true;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.play = function() {
|
that.play = function() {
|
||||||
paused = false;
|
paused = false;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.setCurrentTime = function(time) {
|
that.setCurrentTime = function(time) {
|
||||||
frameIdx = time;
|
frameIdx = time;
|
||||||
};
|
};
|
||||||
|
|
||||||
that.addEventListener = function(event, f) {
|
that.addEventListener = function(event, f) {
|
||||||
if (_eventNames.indexOf(event) !== -1) {
|
if (_eventNames.indexOf(event) !== -1) {
|
||||||
if (!_eventHandlers[event]) {
|
if (!_eventHandlers[event]) {
|
||||||
_eventHandlers[event] = [];
|
_eventHandlers[event] = [];
|
||||||
}
|
|
||||||
_eventHandlers[event].push(f);
|
|
||||||
}
|
}
|
||||||
};
|
_eventHandlers[event].push(f);
|
||||||
|
}
|
||||||
that.setTopRight = function(topRight) {
|
};
|
||||||
_topRight.x = topRight.x;
|
|
||||||
_topRight.y = topRight.y;
|
|
||||||
};
|
|
||||||
|
|
||||||
that.getTopRight = function() {
|
that.setTopRight = function(topRight) {
|
||||||
return _topRight;
|
_topRight.x = topRight.x;
|
||||||
};
|
_topRight.y = topRight.y;
|
||||||
|
};
|
||||||
|
|
||||||
that.setCanvasSize = function(size) {
|
that.getTopRight = function() {
|
||||||
_canvasSize.x = size.x;
|
return _topRight;
|
||||||
_canvasSize.y = size.y;
|
};
|
||||||
};
|
|
||||||
|
|
||||||
that.getCanvasSize = function() {
|
that.setCanvasSize = function(size) {
|
||||||
return _canvasSize;
|
_canvasSize.x = size.x;
|
||||||
};
|
_canvasSize.y = size.y;
|
||||||
|
};
|
||||||
|
|
||||||
that.getFrame = function() {
|
that.getCanvasSize = function() {
|
||||||
if (!loaded){
|
return _canvasSize;
|
||||||
return null;
|
};
|
||||||
}
|
|
||||||
return frame;
|
|
||||||
};
|
|
||||||
|
|
||||||
return that;
|
that.getFrame = function() {
|
||||||
|
if (!loaded){
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
return frame;
|
||||||
};
|
};
|
||||||
|
|
||||||
return (InputStream);
|
return that;
|
||||||
});
|
};
|
||||||
|
|
||||||
|
module.exports = InputStream;
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,26 @@
|
|||||||
|
var webpack = require('webpack'),
|
||||||
|
path = require('path');
|
||||||
|
|
||||||
|
module.exports = require('./webpack.config.js');
|
||||||
|
|
||||||
|
console.log(path.resolve(__dirname, 'lib/input_stream'));
|
||||||
|
|
||||||
|
module.exports.resolve = {
|
||||||
|
extensions: ['', '.js', '.jsx'],
|
||||||
|
alias: {
|
||||||
|
'input_stream$': path.resolve(__dirname, 'lib/input_stream'),
|
||||||
|
'frame_grabber$': path.resolve(__dirname, 'lib/frame_grabber')
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
module.exports.externals = [
|
||||||
|
"get-pixels",
|
||||||
|
"gl-matrix",
|
||||||
|
"lodash",
|
||||||
|
"ndarray",
|
||||||
|
"ndarray-linear-interpolate"
|
||||||
|
];
|
||||||
|
module.exports.output.libraryTarget = "commonjs2";
|
||||||
|
module.exports.plugins = [];
|
||||||
|
module.exports.output.path = __dirname + '/lib';
|
||||||
|
module.exports.output.filename = 'quagga.js';
|
Loading…
Reference in New Issue