In javascript, I'm successfully converting a RGB video frame from my webcam to a tensor using TensorFlowJS's function tf.browser.fromPixels(). Now, I'd like to select only a part of this tensor according to values I've previously obtained. specifically a rectangle from the video frame with coordinates [x1,y1,x2,y2] but I'm struggling to do so using TFJS function tf.stridedSlice(), because I can't figure out how the function's parameters works.
For example, the video frame tensor has shape [480,640,3], and I'd like to cut a whole rectangle from it with shape [270,202,3], of which I know the upper left (x1,y1) and bottom right (x2,y2) coordinates, how can I achieve this in some manner like:
tensorImg = tf.browser.fromPixels(videoFrame);
tensorCropped = tf.stridedSlice(tensorImg,[x1,y1],[x1+x2,y1+y2]); ???
Thanks.
This should work:
const cropBox = [[0.15, 0.15, 0.85, 0.85]]; // top,left,bottom,right in range 0..1 (not in pixel range)
const outputSize = [200, 200]; // how large we want output to be
const resize = tf.image.cropAndResize(inputTensor, cropBox, [0], outputSize);
Note that cropBox itself is array of arrays and then second param says which entry to actually use [0])