我试图将TensorFlow模型加载到javascript文件中,但我从一行中得到了一个未捕获的TypeError,但我不明白为什么会得到这个错误。
这是产生错误的代码块
const demosSection = document.getElementById('demos');
const MODEL_FILE_URL = 'models/Graph/model.json';
// For Keras use tf.loadLayersModel().
const model = tf.loadGraphModel(MODEL_FILE_URL);
// Before we can use COCO-SSD class we must wait for it to finish
// loading. Machine Learning models can be large and take a moment to
// get everything needed to run.
model.then(function(loadedModel) {
MODEL_FILE_URL = loadedModel;
// Show demo section now model is ready to use.
demosSection.classList.remove('invisible');
});
这是控制台错误消息-letsscan1.js:12 Uncaught (in promise) TypeError: Assignment to constant variable. at letsscan1.js:12
const
类型的变量为final
类型。一旦为该变量指定了值,就不能更改其值。
你在这里推翻了它。
const demosSection = document.getElementById('demos');
let MODEL_FILE_URL = 'models/Graph/model.json'; // <-- this variable gets assigned another value below. Changed it to let
// For Keras use tf.loadLayersModel().
const model = tf.loadGraphModel(MODEL_FILE_URL);
// Before we can use COCO-SSD class we must wait for it to finish
// loading. Machine Learning models can be large and take a moment to
// get everything needed to run.
model.then(function(loadedModel) {
MODEL_FILE_URL = loadedModel; // <-- you were reassigning a value to a const variable here.
// Show demo section now model is ready to use.
demosSection.classList.remove('invisible');
});