问题描述
我正在创建一个简单的身体分割 html following this
我试过了:
<body>
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@0.13.3"></script>
<!-- Load BodyPix -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/body-pix"></script>
bodypix.load().then(function(net) {
// BodyPix model loaded
});
</script>
</body>
</html>
使用这个脚本:
// load the BodyPix model from a checkpoint
const net = await bodyPix.load();
// arguments for estimating person segmentation.
const outputStride = 16;
const segmentationThreshold = 0.5;
const personSegmentation = await net.estimatePersonSegmentation(imageElement,outputStride,segmentationThreshold);
它工作正常,没有错误,但问题是它甚至不能从简单的图像中检测到人。我已经尝试使用此链接进行演示,使用 source code 但即使在那里,它也掩盖了整个图像,没有检测到任何返回一个 uint8clamped 数组的主体,该数组仅包含零 相同的模型在我的手机上工作。 是不是我的电脑有问题。 它是 i7 2600 3.7ghz
注意 - 我没有 GPU
解决方法
错误
实际上错误在于tensorflow默认使用webGL后端。如果你没有 GPU 那么它就会崩溃
解决方案
注意-此解决方案适用于 node js,但您也可以将类似的概念与脚本标签一起使用
要解决这个问题,你必须首先导入@tensorflow/tfjs 或@tensorflow/tfjs-core 或@tensorflow/tfjs-node,然后将其后端设置为cpu(或tensorflow,如果您使用的是@tensorflow/tfjs-节点) 参考下面的脚本
//main.js file,works well with webpack
import * as tf from '@tensorflow/tfjs-core'
import * as bodypix from '@tensorflow-models/body-pix'
tf.setBackend('cpu')
//now you can load bodypix or any model
有关文档,请参阅 here