Skip to content

Instantly share code, notes, and snippets.

@syshen
Created May 19, 2018 12:30
Show Gist options
  • Save syshen/a0c2afc8c82f1e2160b4640e17af8e23 to your computer and use it in GitHub Desktop.
Save syshen/a0c2afc8c82f1e2160b4640e17af8e23 to your computer and use it in GitHub Desktop.
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