Skip to content

Instantly share code, notes, and snippets.

@Marina-Miranovich
Last active November 14, 2018 20:46
Show Gist options
  • Save Marina-Miranovich/f0a581ef5008c7665f1c33489cf0e6a6 to your computer and use it in GitHub Desktop.
Save Marina-Miranovich/f0a581ef5008c7665f1c33489cf0e6a6 to your computer and use it in GitHub Desktop.
code_snippet_5 for "Playing Mortal Kombat with TensorFlow.js. Transfer learning and data augmentation" translation
export const readInput = img => imageToInput(readImage(img), TotalChannels);
const readImage = path => jpeg.decode(fs.readFileSync(path), true);
const imageToInput = image => {
const values = serializeImage(image);
return tf.tensor3d(values, [image.height, image.width, 3], 'int32');
};
const serializeImage = image => {
const totalPixels = image.width * image.height;
const result = new Int32Array(totalPixels * 3);
for (let i = 0; i < totalPixels; i++) {
result[i * 3 + 0] = image.data[i * 4 + 0];
result[i * 3 + 1] = image.data[i * 4 + 1];
result[i * 3 + 2] = image.data[i * 4 + 2];
}
return result;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment