Skip to content

Commit 458f6e5

Browse files
authored
Merge pull request #2 from piercus/develop
Develop
2 parents 7394dc8 + 4f3a64b commit 458f6e5

File tree

4 files changed

+88
-12
lines changed

4 files changed

+88
-12
lines changed

README.md

Lines changed: 21 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ npm install image-augment
1919

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

3030
// Then initialize with the backend
3131

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

34-
// create an augmentation pipeline
34+
// Create an augmentation pipeline
3535
const basicAugmentation = ia.sequential([
36-
// add a noise with a standard deviation of 15
36+
// Add a noise with a standard deviation of 15
3737
ia.additiveNoise(15),
38-
// rotate 30°
39-
ia.affineTransform({ rotate: 30 }),
40-
// add a blur kernel of 3 pixel
38+
// Rotate 30°
39+
ia.affine({rotate: 30}),
40+
// Add a blur kernel of 3 pixel
4141
ia.blur(3)
4242
]);
4343

44-
const {images} = basicAugmentation.read('lenna.jpg')
45-
46-
// images is a tensor4d image
47-
// or a [tensor4d] when output images have different shapes
44+
// tensorflow backend needs Tensor4d <-> filename function
45+
// see test/examples/simple-example.js for full implementation of those helpers (fileToTensor and tensorToFile)
46+
47+
fileToTensor('test/data/tfjs/lenna.png')
48+
.then(({images}) => {
49+
return basicAugmentation.read({images});
50+
})
51+
.then(({images}) => {
52+
return tensorToFile('test/data/tfjs/lenna-example.png', {images});
53+
})
54+
.then(() => {
55+
console.log('done');
56+
});
4857

4958
```
5059

package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
"bluebird": "^3.5.3",
6868
"jsdoc": "^3.5.5",
6969
"opencv4nodejs": "^4.14.1",
70+
"pngjs": "^3.4.0",
7071
"semantic-release": "^15.13.3",
7172
"xo": "^0.24.0"
7273
},

test/data/tfjs/lenna-example.png

409 KB
Loading

test/examples/simple-example.js

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
// Tfjs does not provide any read/write manipulation
2+
// function on nodejs, so we nee to to this with pngjs
3+
const fs = require('fs');
4+
const {PNG} = require('pngjs');
5+
6+
const fileToTensor = function (filename) {
7+
return new Promise((resolve, reject) => {
8+
const inputPng = new PNG();
9+
fs.createReadStream(filename)
10+
.pipe(inputPng)
11+
.on('parsed', () => {
12+
const images = tf.tensor4d(inputPng.data, [1, inputPng.height, inputPng.width, 4]);
13+
resolve({images});
14+
})
15+
.on('error', reject);
16+
});
17+
};
18+
19+
const tensorToFile = function (filename, {images}) {
20+
return new Promise((resolve, reject) => {
21+
const png = new PNG({
22+
width: images.shape[2],
23+
height: images.shape[1]
24+
});
25+
png.data = images.dataSync();
26+
png
27+
.pack()
28+
.pipe(fs.createWriteStream(filename))
29+
.on('error', reject)
30+
.on('close', resolve);
31+
});
32+
};
33+
34+
// First you need a backend for image processing
35+
// this can be one of the following :
36+
// * @tensorflow/tfjs
37+
// * @tensorflow/tfjs-node
38+
// * @tensorflow/tfjs-node-gpu
39+
// * opencv4nodejs
40+
41+
const tf = require('@tensorflow/tfjs-node');
42+
43+
// Then initialize with the backend
44+
45+
const ia = require('../..')(tf);
46+
47+
// Create an augmentation pipeline
48+
const basicAugmentation = ia.sequential([
49+
// Add a noise with a standard deviation of 15
50+
ia.additiveNoise(15),
51+
// Rotate 30°
52+
ia.affine({rotate: 30}),
53+
// Add a blur kernel of 3 pixel
54+
ia.blur(3)
55+
]);
56+
57+
fileToTensor('test/data/tfjs/lenna.png')
58+
.then(({images}) => {
59+
return basicAugmentation.read({images});
60+
})
61+
.then(({images}) => {
62+
return tensorToFile('test/data/tfjs/lenna-example.png', {images});
63+
})
64+
.then(() => {
65+
console.log('done');
66+
});

0 commit comments

Comments
 (0)