我正在尝试在node.js中使用imgly/background-removal包。但是,我不断收到此错误:
错误:找不到资源元数据。确保 config.publicPath 配置正确。
在 loadAsBlob (C:\Users 名称\OCR ode_modules@imgly 背景删除\dist\index.cjs:191:11)
在 process.processTicksAndRejections (节点:内部/进程/task_queues:95:5)
在异步 initInference (C:\Users 名称\OCR ode_modules@imgly 背景删除\dist\index.cjs:506:16)
在异步removeBackground(C:\ Users 名称\OCR ode_modules@imgly 背景删除\dist\index.cjs:540:31)
这是我的相关客户端代码,我在其中向服务器发出 ajax 请求。
function sendFile() {
let thePic = document.getElementById("img").files[0]; //This is a file object
const fd = new FormData();
fd.append('thePic', thePic);
let xhttp = new XMLHttpRequest();
xhttp.onreadystatechange = function () {
if (xhttp.readyState === 4 && xhttp.status === 200) {
let imgTag = document.createElement("img");
imgTag.src = this.responseText;
document.body.appendChild(imgTag);
}
};
xhttp.open("POST", "http://localhost:8080/getNewImg", false);
xhttp.setRequestHeader("Content-Type", 'application/x-www-form-urlencoded');
xhttp.send(fd);
}
这是我的相关服务器代码。
const removeBackground = require('@imgly/background-removal').removeBackground;
const public_path = 'https://unpkg.com/browse/@imgly/[email protected]/dist/';
let config = {
publicPath: public_path,
};
app.get('/', (req, res) => {
res.sendFile(__dirname + '/views/index.html');
});
app.post('/getNewImg', (req, res) => {
let img = req.body.thePic;
removeBackground(img, config).then(function(imgNew) {
let imgNewURL = URL.createObjectURL(imgNew);
res.send(imgNewURL);
});
});
我不知道问题出在哪里。该网址是否有问题?
您正在尝试将浏览器版本的背景去除与 NodeJS 结合使用
再试一次
npm install @imgly/background-removal-node