Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
preprocess(data, width, height) {
// data processing
const dataTensor = ndarray(new Float32Array(data), [width, height, 4]);
const dataProcessedTensor = ndarray(new Float32Array(width * height * 3), [1, 3, width, height]);
ops.divseq(dataTensor, 128.0);
ops.subseq(dataTensor, 1.0);
ops.assign(dataProcessedTensor.pick(0, 0, null, null), dataTensor.pick(null, null, 2));
ops.assign(dataProcessedTensor.pick(0, 1, null, null), dataTensor.pick(null, null, 1));
ops.assign(dataProcessedTensor.pick(0, 2, null, null), dataTensor.pick(null, null, 0));
const tensor = new onnx.Tensor(dataProcessedTensor.data, 'float32', [1, 3, width, height]);
return tensor;
}
}
async postprocess(tensor: Tensor, inferenceTime: number) {
try {
const originalOutput = new Tensor(tensor.data as Float32Array, 'float32', [1, 125, 13, 13]);
const outputTensor = yoloTransforms.transpose(originalOutput, [0, 2, 3, 1]);
// postprocessing
const boxes = await yolo.postprocess(outputTensor, 20);
boxes.forEach(box => {
const {
top, left, bottom, right, classProb, className,
} = box;
this.drawRect(left, top, right-left, bottom-top,
`${className} Confidence: ${Math.round(classProb * 100)}% Time: ${inferenceTime.toFixed(1)}ms`);
});
} catch (e) {
alert('Model is not valid!');
}
}
preprocess(ctx: CanvasRenderingContext2D): Tensor {
const imageData = ctx.getImageData(0, 0, ctx.canvas.width, ctx.canvas.height);
const { data, width, height } = imageData;
// data processing
const dataTensor = ndarray(new Float32Array(data), [width, height, 4]);
const dataProcessedTensor = ndarray(new Float32Array(width * height * 3), [1, 3, width, height]);
ops.assign(dataProcessedTensor.pick(0, 0, null, null), dataTensor.pick(null, null, 0));
ops.assign(dataProcessedTensor.pick(0, 1, null, null), dataTensor.pick(null, null, 1));
ops.assign(dataProcessedTensor.pick(0, 2, null, null), dataTensor.pick(null, null, 2));
const tensor = new Tensor(new Float32Array(width* height* 3), 'float32', [1, 3, width, height]);
(tensor.data as Float32Array).set(dataProcessedTensor.data);
return tensor;
}
const dataProcessedTensor = ndarray(new Float32Array(width * height * 3), [1, 3, width, height]);
ops.assign(dataProcessedTensor.pick(0, 0, null, null), dataTensor.pick(null, null, 0));
ops.assign(dataProcessedTensor.pick(0, 1, null, null), dataTensor.pick(null, null, 1));
ops.assign(dataProcessedTensor.pick(0, 2, null, null), dataTensor.pick(null, null, 2));
ops.divseq(dataProcessedTensor, 255);
ops.subseq(dataProcessedTensor.pick(0, 0, null, null), 0.485);
ops.subseq(dataProcessedTensor.pick(0, 1, null, null), 0.456);
ops.subseq(dataProcessedTensor.pick(0, 2, null, null), 0.406);
ops.divseq(dataProcessedTensor.pick(0, 0, null, null), 0.229);
ops.divseq(dataProcessedTensor.pick(0, 1, null, null), 0.224);
ops.divseq(dataProcessedTensor.pick(0, 2, null, null), 0.225);
const tensor = new Tensor(new Float32Array(3 * width * height), 'float32', [1, 3, width, height]);
(tensor.data as Float32Array).set(dataProcessedTensor.data);
return tensor;
}
const dataProcessedTensor = ndarray(new Float32Array(width * height * 3), [1, 3, width, height]);
ops.assign(dataProcessedTensor.pick(0, 0, null, null), dataTensor.pick(null, null, 0));
ops.assign(dataProcessedTensor.pick(0, 1, null, null), dataTensor.pick(null, null, 1));
ops.assign(dataProcessedTensor.pick(0, 2, null, null), dataTensor.pick(null, null, 2));
ops.divseq(dataProcessedTensor, 255);
ops.subseq(dataProcessedTensor.pick(0, 0, null, null), 0.485);
ops.subseq(dataProcessedTensor.pick(0, 1, null, null), 0.456);
ops.subseq(dataProcessedTensor.pick(0, 2, null, null), 0.406);
ops.divseq(dataProcessedTensor.pick(0, 0, null, null), 0.229);
ops.divseq(dataProcessedTensor.pick(0, 1, null, null), 0.224);
ops.divseq(dataProcessedTensor.pick(0, 2, null, null), 0.225);
const tensor = new Tensor(new Float32Array(width * height * 3), 'float32', [1, 3, width, height]);
(tensor.data as Float32Array).set(dataProcessedTensor.data);
return tensor;
}