Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

'''
<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>
'''

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 'texImage2D' on 'WebGL2RenderingContext': 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

'''
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 
'''

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.

like image 276
Bharat_K Avatar asked Jan 03 '20 11:01

Bharat_K


People also ask

What is ‘uncaught (in promise) domexception’?

Uncaught (in promise) DOMException: Failed to execute ‘toDataURL’ on ‘HTMLCanvasElement’: Tainted canvases may not be exported. After some research I got know that it was not a bug in any of the libraries I used, It was a limitation to prevent unauthorized use of external usage of images in canvas in HTML5.

Why ‘getimagedata’ failed to execute on ‘canvasrenderingcontext2d’?

Failed to execute ‘getImageData’ on ‘CanvasRenderingContext2D’: The canvas has been tainted by cross-origin data. Unable to apply filter.

Why ‘todataurl’ failed to execute on ‘htmlcanvaselement’?

Failed to execute ‘toDataURL’ on ‘HTMLCanvasElement’: Tainted canvases may not be exported. Unable to get image data from canvas because the canvas has been tainted by cross-origin data.

What is the error message for the 'htmlcanvaselement'?

Below is the detailed exception error message. Uncaught DOMException: Failed to execute 'toDataURL' on 'HTMLCanvasElement': Tainted canvases may not be exported.


1 Answers

crossorigin='anonymous' needs to be added to the image tag

<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='https://i.imgur.com/jlFgGpe.jpg' crossorigin='anonymous' />
</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>

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 .
<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=' http://127.0.0.1:8080/temp.png' crossorigin="anonymous" />
</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');

    imageElement.crossOrigin = "Anonymous";

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

</html>
like image 146
edkeveked Avatar answered Sep 23 '22 01:09

edkeveked