我如何使用上传按钮上传图像(使用NodeJs)作为烧瓶中的图像分类器api或XYZ.com上运行的图像分类器的输入



我很难找到如何将图像上传到我的图像分类器api并对其进行预测。在nodejs中,我复制粘贴了许多代码示例,但它只将文件的副本保存到上传文件夹中,但没有将文件添加到api的选项。我在本地主机上同时运行服务器(NodeJs和flask(api((。我需要类似的东西,比如AWS Rekognition让我们选择一张图片并显示响应。

这是我复制粘贴的代码,但不知道如何添加api并发送页面上选择的图像

var http = require('http');
var formidable = require('formidable');
var fs = require('fs');
http.createServer(function (req, res) {
if (req.url == '/fileupload') {
var form = new formidable.IncomingForm();
form.parse(req, function (err, fields, files) {
var oldpath = files.filetoupload.path;
console.log(oldpath);
console.log('Fields', fields)
console.log('Files', files)
var newpath = 'C:/Users/Pratham Nishad/Desktop/' + 
files.filetoupload.name;
fs.rename(oldpath, newpath, function (err) {
if (err) throw err;
res.write('File uploaded and moved!');
res.end();
});
});
} else {
res.writeHead(200, { 'Content-Type': 'text/html' });
res.write('<form action="fileupload" method="post" 
enctype="multipart/form-data">');
res.write('<input type="file" name="filetoupload"><br>');
res.write('<input type="submit">');
res.write('</form>');
return res.end();
}
}).listen(4000);

我想要什么-假设我有一个图像分类器,可以在XYZ.com上对图像进行分类,我想让用户从他的/她的存储中选择一个图像(此处有问题(,并对其进行处理并发布结果。我看的所有教程都只是从一个位置选择图像并将其粘贴到目标位置,没有关于在XYZ.com上运行的图像分类器将如何获得图像的信息。

在npm:上查看multer包

https://www.npmjs.com/package/multer

根据要求,我将展示我是如何实际使用此工具的。

前端代码:

<!DOCTYPE html>
<html>
<body>
<p>Click on the "Choose File" button to upload a file:</p>
<form action="xyz.com" method="post">
<input type="file" id="myFile" name="filename">
<input type="submit">
</form>
</body>
</html>

后端代码:

const multer = require('multer');
//Multer DiskStorage Config
const diskStorage = multer.diskStorage({
destination: 'path/to/your/desired/folder',
filename: (req, file, call_back) => {
//Prepend date to the filename
//or anything that can uniquely identify the file
//so it won't get overwritten by the new ones
call_back(null, Date.now() + '_' + file.originalname);
}
});
//Create Multer Instance
const upload = multer({ storage: diskStorage });

//Upload picture
router.post('/uploadpic', upload.single('file'), (req, res) => {
res.status(200);
});

保存文件后,可以将其与图像分类器一起使用。

您可以自由创建自己的文件结构。没有特定的约束或规则,你必须遵守。

最新更新