Created
May 19, 2018 12:30
-
-
Save syshen/a0c2afc8c82f1e2160b4640e17af8e23 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
toTensor: function(canvas) { | |
return tf.tidy(() => { | |
// Convert the ImageData to Tensor | |
const image = tf.fromPixels(canvas) | |
// The input shape of YOLO is 416x416x3, we need to crop the image | |
const size = this.size.width | |
const beginX = image.shape[1] / 2 - size / 2 | |
const beginY = image.shape[0] / 2 - size / 2 | |
const cropped = image.slice([beginY, beginX, 0], [size, size, 3]) | |
// Create a batch of image but with only one image | |
const batchImage = cropped.expandDims(0) | |
// normalize the data | |
return batchImage.toFloat().div(tf.scalar(255)) | |
}) | |
}, |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment