gpt4 book ai didi

javascript - tf.browser.fromPixels() 不工作因为 "DOM is not ready yet"

转载 作者:行者123 更新时间:2023-11-30 19:20:13 24 4
gpt4 key购买 nike

我正在尝试使用 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');
}

Github tensorflow/tfjs

根据上面的代码,抛出错误是因为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 在任何子资源还没有加载之前被调用。所以在你的代码被执行的时候,document.readyState 仍然是 interactive

忽略错误提示并尝试使用 load 事件监听器。

关于javascript - tf.browser.fromPixels() 不工作因为 "DOM is not ready yet",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57549521/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com