mirror of https://github.com/vladmandic/human
update demo node decoder
parent
8440d78d5f
commit
951fae2322
22
demo/node.js
22
demo/node.js
|
@ -66,20 +66,28 @@ async function detect(input) {
|
||||||
// decode image using tfjs-node so we don't need external depenencies
|
// decode image using tfjs-node so we don't need external depenencies
|
||||||
// can also be done using canvas.js or some other 3rd party image library
|
// can also be done using canvas.js or some other 3rd party image library
|
||||||
if (!buffer) return {};
|
if (!buffer) return {};
|
||||||
const decoded = human.tf.node.decodeImage(buffer);
|
const tensor = tf.tidy(() => {
|
||||||
const casted = decoded.toFloat();
|
const decode = human.tf.node.decodeImage(buffer, 3);
|
||||||
const tensor = casted.expandDims(0);
|
let expand;
|
||||||
decoded.dispose();
|
if (decode.shape[2] === 4) { // input is in rgba format, need to convert to rgb
|
||||||
casted.dispose();
|
const channels = human.tf.split(decode, 4, 2); // tf.split(tensor, 4, 2); // split rgba to channels
|
||||||
|
const rgb = human.tf.stack([channels[0], channels[1], channels[2]], 2); // stack channels back to rgb and ignore alpha
|
||||||
|
expand = human.tf.reshape(rgb, [1, decode.shape[0], decode.shape[1], 3]); // move extra dim from the end of tensor and use it as batch number instead
|
||||||
|
} else {
|
||||||
|
expand = human.tf.expandDims(decode, 0);
|
||||||
|
}
|
||||||
|
const cast = human.tf.cast(expand, 'float32');
|
||||||
|
return cast;
|
||||||
|
});
|
||||||
|
|
||||||
// image shape contains image dimensions and depth
|
// image shape contains image dimensions and depth
|
||||||
log.state('Processing:', tensor.shape);
|
log.state('Processing:', tensor['shape']);
|
||||||
|
|
||||||
// run actual detection
|
// run actual detection
|
||||||
const result = await human.detect(tensor, myConfig);
|
const result = await human.detect(tensor, myConfig);
|
||||||
|
|
||||||
// dispose image tensor as we no longer need it
|
// dispose image tensor as we no longer need it
|
||||||
tensor.dispose();
|
tf.dispose(tensor);
|
||||||
|
|
||||||
// print data to console
|
// print data to console
|
||||||
log.data('Results:');
|
log.data('Results:');
|
||||||
|
|
Loading…
Reference in New Issue