-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2 from piercus/develop
Develop
- Loading branch information
Showing
4 changed files
with
88 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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'); | ||
}); |