[英]Unable to load a image , (action blocked by cors policy) while i have set cross-origin to anonymous
所以,我开始在 javascript 中进行机器学习,我想看到一个图像分类器模型在运行。
所以当我尝试在浏览器中加载图像时,它不起作用。
这是我得到的错误:
Access to image at 'file:///F:/rishit/ml5/imagerecognition/bird.png' from origin 'null' has been blocked by CORS policy: Cross origin requests are only supported for protocol schemes: http, data, chrome, chrome-extension, chrome-untrusted, https.
这是我的 index.html
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>ML-5</title>
<script src="https://unpkg.com/ml5@0.4.3/dist/ml5.min.js"></script>
</head>
<body>
<h1 style="text-align: center;" >Image classification</h1>
<img src="bird.png"
alt="bird" id ="bird" >
<script src="script.js"></script>
</body>
</html>
这是我的 script.js
let img = document.getElementById('bird')
img.crossOrigin = 'Anonymous'
let classifier = ml5.imageClassifier('MobileNet',onLoad)
function onLoad(){
console.log('yay!,its Done')
}
classifier.classify(img).then(
(results)=>{console.log(results)}
)
这是我的文件结构
Bird.png index.html script.js
您确定要从正确的文件夹中提供图像吗? 从原点“null” Access to image at 'file:///F:/rishit/ml5/imagerecognition/bird.png'
。
我认为您不应该添加img.crossOrigin = 'Anonymous'
或类似内容,很可能是您提供静态文件的方式有问题
只有当您直接从文件资源管理器打开 index.html 时,问题才会持续存在。 因为它使用 file:// 协议。
当您托管它或在 Visual Studio Code 中使用 Live Server 扩展(我强烈推荐)之类的东西时,一切都应该正常工作,因为它将使用 http:// 协议。
我为我的 web 应用程序执行此操作。 仅使用 html 和 javascript 从桌面和 Web 服务器工作。 如果您不想在 PC 上创建本地主机,则很有用。
既然您正在学习机器学习,我假设您熟悉所有这些 html 和 javascript api。 我广泛使用它来从文件系统操作画布上的图像,这通常是 CORS 的噩梦! 如果您对一些代码示例感兴趣,请告诉我。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.