Skip to content

Commit

Permalink
Merge pull request #2 from piercus/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
piercus authored Mar 29, 2019
2 parents 7394dc8 + 4f3a64b commit 458f6e5
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 12 deletions.
33 changes: 21 additions & 12 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ npm install image-augment

```javascript
// First you need a backend for image processing
// this can be one of the following :
// * @tensorflow/tfjs
// this can be one of the following :
// * @tensorflow/tfjs
// * @tensorflow/tfjs-node
// * @tensorflow/tfjs-node-gpu
// * opencv4nodejs
Expand All @@ -29,22 +29,31 @@ const tf = require('@tensorflow/tfjs-node');

// Then initialize with the backend

const ia = require('image-augment')(tf);
const ia = require('../..')(tf);

// create an augmentation pipeline
// Create an augmentation pipeline
const basicAugmentation = ia.sequential([
// add a noise with a standard deviation of 15
// Add a noise with a standard deviation of 15
ia.additiveNoise(15),
// rotate 30°
ia.affineTransform({ rotate: 30 }),
// add a blur kernel of 3 pixel
// Rotate 30°
ia.affine({rotate: 30}),
// Add a blur kernel of 3 pixel
ia.blur(3)
]);

const {images} = basicAugmentation.read('lenna.jpg')

// images is a tensor4d image
// or a [tensor4d] when output images have different shapes
// tensorflow backend needs Tensor4d <-> filename function
// see test/examples/simple-example.js for full implementation of those helpers (fileToTensor and tensorToFile)

fileToTensor('test/data/tfjs/lenna.png')
.then(({images}) => {
return basicAugmentation.read({images});
})
.then(({images}) => {
return tensorToFile('test/data/tfjs/lenna-example.png', {images});
})
.then(() => {
console.log('done');
});

```

Expand Down
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
"bluebird": "^3.5.3",
"jsdoc": "^3.5.5",
"opencv4nodejs": "^4.14.1",
"pngjs": "^3.4.0",
"semantic-release": "^15.13.3",
"xo": "^0.24.0"
},
Expand Down
Binary file added test/data/tfjs/lenna-example.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
66 changes: 66 additions & 0 deletions test/examples/simple-example.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
// Tfjs does not provide any read/write manipulation
// function on nodejs, so we nee to to this with pngjs
const fs = require('fs');
const {PNG} = require('pngjs');

const fileToTensor = function (filename) {
return new Promise((resolve, reject) => {
const inputPng = new PNG();
fs.createReadStream(filename)
.pipe(inputPng)
.on('parsed', () => {
const images = tf.tensor4d(inputPng.data, [1, inputPng.height, inputPng.width, 4]);
resolve({images});
})
.on('error', reject);
});
};

const tensorToFile = function (filename, {images}) {
return new Promise((resolve, reject) => {
const png = new PNG({
width: images.shape[2],
height: images.shape[1]
});
png.data = images.dataSync();
png
.pack()
.pipe(fs.createWriteStream(filename))
.on('error', reject)
.on('close', resolve);
});
};

// First you need a backend for image processing
// this can be one of the following :
// * @tensorflow/tfjs
// * @tensorflow/tfjs-node
// * @tensorflow/tfjs-node-gpu
// * opencv4nodejs

const tf = require('@tensorflow/tfjs-node');

// Then initialize with the backend

const ia = require('../..')(tf);

// Create an augmentation pipeline
const basicAugmentation = ia.sequential([
// Add a noise with a standard deviation of 15
ia.additiveNoise(15),
// Rotate 30°
ia.affine({rotate: 30}),
// Add a blur kernel of 3 pixel
ia.blur(3)
]);

fileToTensor('test/data/tfjs/lenna.png')
.then(({images}) => {
return basicAugmentation.read({images});
})
.then(({images}) => {
return tensorToFile('test/data/tfjs/lenna-example.png', {images});
})
.then(() => {
console.log('done');
});

0 comments on commit 458f6e5

Please sign in to comment.