tf.browser.fromPixels() 不工作因为 "DOM is not ready yet"
tf.browser.fromPixels() not working because "DOM is not ready yet"
我正在尝试使用 TensorFlow.js 的 tf.browser.fromPixels()
函数。不幸的是,我收到以下错误:
backend_webgl.ts:309 Uncaught Error: The DOM is not ready yet. Please call tf.browser.fromPixels() once the DOM is ready. One way to do that is to add an event listener for `DOMContentLoaded` on the document object
at e.fromPixels (backend_webgl.ts:309)
at t.fromPixels (engine.ts:870)
at fromPixels_ (browser.ts:55)
at Object.fromPixels (operation.ts:45)
at ex4.html:10
但我是从传递给 window.addEventListener('DOMContentLoaded', ...)
的函数内部调用 tf.browser.fromPixels()
。知道为什么我会收到此错误以及如何让 fromPixels
函数工作吗?
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.2.7/dist/tf.min.js"></script>
</head>
<body>
<img id="test" src="https://jpeg.org/images/jpeg-home.jpg">
<script>
window.addEventListener('DOMContentLoaded', ev => {
const img = document.getElementById('test')
tf.browser.fromPixels(img).print()
})
</script>
</body>
</html>
if (document.readyState !== 'complete') {
throw new Error(
'The DOM is not ready yet. Please call ' +
'tf.browser.fromPixels() once the DOM is ready. One way to ' +
'do that is to add an event listener for `DOMContentLoaded` ' +
'on the document object');
}
根据上面的代码,抛出错误是因为 document.readyState
不是 complete
.
The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.
https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event
The readyState of a document can be one of following:
loading
The document is still loading.
interactive
The document has finished loading and the document has been parsed but sub-resources such as images, stylesheets and frames are still loading.
complete
The document and all sub-resources have finished loading. The state indicates that the load event is about to fire.
https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
DOMContentLoaded
在未加载任何子资源之前调用 yet.So 在执行代码时,document.readyState
仍然是 interactive
。
忽略错误提示并尝试 load
事件侦听器。
我正在尝试使用 TensorFlow.js 的 tf.browser.fromPixels()
函数。不幸的是,我收到以下错误:
backend_webgl.ts:309 Uncaught Error: The DOM is not ready yet. Please call tf.browser.fromPixels() once the DOM is ready. One way to do that is to add an event listener for `DOMContentLoaded` on the document object
at e.fromPixels (backend_webgl.ts:309)
at t.fromPixels (engine.ts:870)
at fromPixels_ (browser.ts:55)
at Object.fromPixels (operation.ts:45)
at ex4.html:10
但我是从传递给 window.addEventListener('DOMContentLoaded', ...)
的函数内部调用 tf.browser.fromPixels()
。知道为什么我会收到此错误以及如何让 fromPixels
函数工作吗?
<html>
<head>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@1.2.7/dist/tf.min.js"></script>
</head>
<body>
<img id="test" src="https://jpeg.org/images/jpeg-home.jpg">
<script>
window.addEventListener('DOMContentLoaded', ev => {
const img = document.getElementById('test')
tf.browser.fromPixels(img).print()
})
</script>
</body>
</html>
if (document.readyState !== 'complete') {
throw new Error(
'The DOM is not ready yet. Please call ' +
'tf.browser.fromPixels() once the DOM is ready. One way to ' +
'do that is to add an event listener for `DOMContentLoaded` ' +
'on the document object');
}
根据上面的代码,抛出错误是因为 document.readyState
不是 complete
.
The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. https://developer.mozilla.org/en-US/docs/Web/API/Window/DOMContentLoaded_event
The readyState of a document can be one of following:
loading
The document is still loading.interactive
The document has finished loading and the document has been parsed but sub-resources such as images, stylesheets and frames are still loading.complete
The document and all sub-resources have finished loading. The state indicates that the load event is about to fire.
https://developer.mozilla.org/en-US/docs/Web/API/Document/readyState
DOMContentLoaded
在未加载任何子资源之前调用 yet.So 在执行代码时,document.readyState
仍然是 interactive
。
忽略错误提示并尝试 load
事件侦听器。