Uncaught (in promise) DOMException: Failed to execute 'texImage2D' on 'WebGL2RenderingContext': Tainted canvases may not be loaded

huangapple go评论76阅读模式
英文:

Uncaught (in promise) DOMException: Failed to execute 'texImage2D' on 'WebGL2RenderingContext': Tainted canvases may not be loaded

问题

以下是您提供的HTML代码的翻译部分:

<!DOCTYPE html>
<html>
<head>  
<meta charset="UTF-8"> 
<!-- Load TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs"></script>
<!-- Load Posenet -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/posenet"></script>
</head>

<body>
<img id='cat' src='cat.jpg' />
</body>
<!-- Place your code in the script tag below. You can also use an external .js file -->
<script>
var flipHorizontal = false;

var imageElement = document.getElementById('cat');

posenet.load().then(function(net) {
const pose = net.estimateSinglePose(imageElement, {
flipHorizontal: true
});
return pose;
}).then(function(pose){
console.log(pose);
})
</script>
</html>

关于您遇到的错误,这是由于浏览器的安全策略导致的问题,涉及到在WebGL上加载带有安全性问题的图像。在Google Chrome中,错误消息是“Failed to execute 'texImage2D' on 'WebGL2RenderingContext': Tainted canvases may not be loaded.”(在WebGL2RenderingContext上执行'texImage2D'操作失败:不能加载有问题的画布)。在Firefox中,错误消息是“SecurityError: The operation is insecure.”(安全错误:操作不安全)。

这可能是由于您加载的图像 'cat.jpg' 具有安全问题,浏览器会拒绝加载它。要解决此问题,您可以尝试以下几种方法:

  1. 确保图像路径正确: 确保 'cat.jpg' 图像位于与HTML文件相同的目录中,并且文件名拼写正确。

  2. 使用HTTPS协议: 如果您的网页是通过HTTPS协议提供的,尝试使用HTTPS链接加载图像,例如 src='https://example.com/cat.jpg'

  3. 检查图像权限: 确保图像文件具有适当的权限,允许浏览器加载它。确保您没有在图像的URL中添加不必要的身份验证或安全性设置。

  4. 避免跨域问题: 确保图像和HTML文件位于相同的域中,以避免跨域访问问题。

  5. 尝试使用本地图像: 尝试将图像更改为本地计算机上的图像,而不是远程链接,以排除远程服务器问题。

如果上述方法都无法解决问题,您可能需要更详细地检查您的网络设置和浏览器安全性设置,以确保没有其他问题导致加载失败。

英文:
&#39;&#39;&#39;
&lt;html&gt;
&lt;head&gt;  
&lt;meta charset=&quot;UTF-8&quot;&gt; 
&lt;!-- Load TensorFlow.js --&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/@tensorflow/tfjs&quot;&gt;&lt;/script&gt;
&lt;!-- Load Posenet --&gt;
&lt;script src=&quot;https://cdn.jsdelivr.net/npm/@tensorflow-models/posenet&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
&lt;img id=&#39;cat&#39; src=&#39;cat.jpg &#39;/&gt;
&lt;/body&gt;
&lt;!-- Place your code in the script tag below. You can also use an external .js file --&gt;
&lt;script&gt;
var flipHorizontal = false;

var imageElement = document.getElementById(&#39;cat&#39;);

posenet.load().then(function(net) {
const pose = net.estimateSinglePose(imageElement, {
flipHorizontal: true
});
return pose;
}).then(function(pose){
console.log(pose);
})
&lt;/script&gt;
&lt;/html&gt;
&#39;&#39;&#39;

Above is html file using which I am trying out posenet from tensorflow js.
Following is the error which I received when I tried to open the above file in google chrome.

Uncaught (in promise) DOMException: Failed to execute &#39;texImage2D&#39; on &#39;WebGL2RenderingContext&#39;: Tainted canvases may not be loaded.
at https://cdn.jsdelivr.net/npm/@tensorflow/tfjs:2:176404
at qt (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs:2:55726)
at vi (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs:2:176377)
at t.uploadPixelDataToTexture (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs:2:181200)
at Object.kernelFunc (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs:2:480876)
at h (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs:2:42226)
at https://cdn.jsdelivr.net/npm/@tensorflow/tfjs:2:42907
at t.scopedRun (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs:2:40845)
at t.runKernelFunc (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs:2:42726)
at t.runKernel (https://cdn.jsdelivr.net/npm/@tensorflow/tfjs:2:41280)

Following is the error when the html file is opened in firefox

&#39;&#39;&#39;
SecurityError: The operation is insecure.
Source map error: Error: request failed with status 404
Resource URL: https://cdn.jsdelivr.net/npm/@tensorflow/tfjs
Source Map URL: tf.min.js.map 
&#39;&#39;&#39;

I am a beginner and I am not able to understand the above error. I am trying to get the pose coordinates from the console in the browser but I just keep getting this error.

Any help would be appreciated and thanks in advance.

答案1

得分: 28

crossorigin=&#39;anonymous&#39; 需要添加到图像标签中

    <img id='cat' src='https://i.imgur.com/jlFgGpe.jpg' crossorigin='anonymous' />

要在本地使用图像,图像需要由允许跨域请求的服务器提供。可以使用以下命令行使用 http-server:

http-server -c1 --cors .
    <img id='cat' src='http://127.0.0.1:8080/temp.png' crossorigin='anonymous' />
英文:

crossorigin=&#39;anonymous&#39; needs to be added to the image tag

<!-- begin snippet: js hide: false console: true babel: false -->

<!-- language: lang-html -->

&lt;html&gt;

&lt;head&gt;
  &lt;meta charset=&quot;UTF-8&quot;&gt;
  &lt;!-- Load TensorFlow.js --&gt;
  &lt;script src=&quot;https://cdn.jsdelivr.net/npm/@tensorflow/tfjs&quot;&gt;&lt;/script&gt;
  &lt;!-- Load Posenet --&gt;
  &lt;script src=&quot;https://cdn.jsdelivr.net/npm/@tensorflow-models/posenet&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
  &lt;img id=&#39;cat&#39; src=&#39;https://i.imgur.com/jlFgGpe.jpg&#39; crossorigin=&#39;anonymous&#39; /&gt;
&lt;/body&gt;
&lt;!-- Place your code in the script tag below. You can also use an external .js file --&gt;
&lt;script&gt;
  var flipHorizontal = false;

  var imageElement = document.getElementById(&#39;cat&#39;);

  posenet.load().then(function(net) {
    const pose = net.estimateSinglePose(imageElement, {
      flipHorizontal: true
    });
    return pose;
  }).then(function(pose) {
    console.log(pose);
  })
&lt;/script&gt;

&lt;/html&gt;

<!-- end snippet -->

To use an image locally, the image needs to be served by a server allowing cors. http-server can be used with the command line

http-server -c1 --cors .

<!-- language: lang-js -->

&lt;html&gt;

&lt;head&gt;
    &lt;meta charset=&quot;UTF-8&quot;&gt;
    &lt;!-- Load TensorFlow.js --&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/@tensorflow/tfjs&quot;&gt;&lt;/script&gt;
    &lt;!-- Load Posenet --&gt;
    &lt;script src=&quot;https://cdn.jsdelivr.net/npm/@tensorflow-models/posenet&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

&lt;body&gt;
    &lt;img id=&#39;cat&#39; src=&#39; http://127.0.0.1:8080/temp.png&#39; crossorigin=&quot;anonymous&quot; /&gt;
&lt;/body&gt;
&lt;!-- Place your code in the script tag below. You can also use an external .js file --&gt;
&lt;script&gt;

    var flipHorizontal = false;

    var imageElement = document.getElementById(&#39;cat&#39;);

    imageElement.crossOrigin = &quot;Anonymous&quot;;

    posenet.load().then(function (net) {
        const pose = net.estimateSinglePose(imageElement, {
            flipHorizontal: true
        });
        return pose;
    }).then(function (pose) {
        console.log(pose);
    })
&lt;/script&gt;

&lt;/html&gt;

答案2

得分: -1

我在我的React应用TensorFlow.js中遇到了同样的问题,但我只意识到我缺少了crossOrigin属性,正如edkeveked所提到的。所以我通过修改img标签并添加crossOrigin属性来解决了我的问题,如下所示:

&lt;img crossOrigin=&#39;anonymous&#39; ... /&gt;

edkeveked的回答是最好的。

祝好运 ✔✔✨✨

英文:

I was having the same problem with my react app and tensorflow.js but i only realised that i was missing the crossOrigin prop or attribute as edkeveked mentioned. So i solved my problem by modifying the img tag and put the attribute or property crossOrigin as follows:

&lt;img crossOrigin=&#39;anonymous&#39; ... /&gt;

edkeveked's answer is the best

Good luck ✔✔✨✨

huangapple
  • 本文由 发表于 2020年1月3日 19:01:53
  • 转载请务必保留本文链接:https://go.coder-hub.com/59577407.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定