0

Upon clicking on the pink button, I want to add images into my classifier. Currently, it gives a the titled CORS warning. What could I be doing wrong? Is it because of how I load images?

<!DOCTYPE html>
<html lang="en">
  <head>

    <title>Getting Started with ml5.js</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <!-- p5 -->
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/p5.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.dom.min.js"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/p5.js/0.9.0/addons/p5.sound.min.js"></script>
    <!-- ml5 -->
    <script src="https://unpkg.com/ml5@0.4.3/dist/ml5.min.js"></script>
  </head>
;
  <body>
    <img src="./images/pink/1.png" class="pink1" alt="pink1">
    <img src="./images/pink/2.png" class="pink2" alt="pink2">
    <img src="./images/pink/3.png" class="pink3" alt="pink3">
  </body>
    <script>
      let pinkButton
      console.log('ml5 version:', ml5.version);
      function mobileReady() {
       console.log('mobileNet loaded');
       
      }

      function setup() {
        mobileNet = ml5.featureExtractor('MobileNet')
        classifier = mobileNet.classification()
        pinkButton = createButton('add pink')
        pinkButton.mousePressed(function(){
          image1 = document.querySelector(".pink1")
          image2 = document.querySelector(".pink2")
          image3 = document.querySelector(".pink3")
          classifier.addImage(image1, 'pink')
          classifier.addImage(image2, 'pink')
          classifier.addImage(image3, 'pink')
          console.log('CLASSIFER', classifier)
        })
      }      
    </script>
  </body>
</html>

Edit: Tried adding this after the querySelector as but well but doesn't work for me.

          image1.crossOrigin = 'anonymous'
          image2.crossOrigin = 'anonymous'
          image3.crossOrigin = 'anonymous'
  • Images that come from a domain different from your own are "tainted" and cannot be used in any way that would allow the image content to be analyzed. It's a security feature. – Pointy Oct 09 '20 at 12:58
  • However, those images *seem* like they should be same-domain. – Pointy Oct 09 '20 at 12:59
  • So, does this mean I cannot use images from my computer and should use the webcam only? Or is there a workaround for this? Would it still work despite the warning? @Pointy –  Oct 09 '20 at 13:00
  • 1
    Oh wait; are you loading your stuff from `file://` URLs? Then yes that's the issue: browsers do not trust content on your machine via `file://` URLs. It seems crazy but it's because of things like HTML documentation for software, which could be used to snoop around your machine. – Pointy Oct 09 '20 at 13:10
  • [On file scheme URLs and CORS](https://stackoverflow.com/questions/10752055/cross-origin-requests-are-only-supported-for-http-error-when-loading-a-local) – Quentin Oct 09 '20 at 13:22
  • [On webGL and cross-origin images](https://stackoverflow.com/questions/46422356/webgl-cross-origin-images-dont-work) – Quentin Oct 09 '20 at 13:22
  • @Quentin I went through the links but couldn't really get how to fix this. I tried adding crossOrigin (updated qs) but it doesnt seem to work for me –  Oct 09 '20 at 13:46
  • you have to install a server on your local machine(pc) , and load your images from inside the server folder, – Tou You Oct 09 '20 at 13:49
  • No shorter way of doing this? @TouYou –  Oct 09 '20 at 13:56
  • you can try the previous version of edge (before chromium), – Tou You Oct 09 '20 at 21:42

0 Answers0