【问题标题】:Send an image as the body of a request, image recived with a request from outside发送图像作为请求的主体,图像接收来自外部的请求
【发布时间】:2018-03-03 22:28:29
【问题描述】:

是的,我有点不知道如何正确输入标题... 我有一个节点服务器,它通过发布表单接收图像。然后,我想将此图像发送到 Microsoft vision 和相同的 Google 服务,以便从两者中收集信息,做一些事情,并将结果返回给访问我服务器的用户。 我的问题是:如何发送实际数据?

这是真正关心的代码:

const microsofComputerVision = require("microsoft-computer-vision");

module.exports = function(req, res)
{
var file;
if(req.files)
    {
    file = req.files.file;

    // Everything went fine
    microsofComputerVision.analyzeImage(
        {
        "Ocp-Apim-Subscription-Key": vision_key,
        "content-type": "multipart/form-data",
        "body": file.data.toString(),
        "visual-features":"Tags, Faces",
        "request-origin":"westcentralus"
        }).then((result) => 
            {
            console.log("A");
            res.write(result);
            res.end();
            }).catch((err)=>
            {
            console.log(err);
            res.writeHead(400, {'Content-Type': 'application/json'});
            res.write(JSON.stringify({error: "The request must contain an image"}));
            res.end();
            });
    }
else
    {
    res.writeHead(400, {'Content-Type': 'application/octet-stream'});
    res.write(JSON.stringify({error: "The request must contain an image"}));
    res.end();
    }

}

如果我不调用“analyzeImage”,而是执行以下操作

    res.set('Content-Type', 'image/jpg')
    res.send(file.data);
    res.end();

浏览器正确呈现图像,这让我认为“file.data”包含实际文件(认为它是缓冲区类型)。 但显然微软不同意这一点,因为当我将请求发送到计算机视觉时,我得到以下响应: "无效图像格式"

我发现的唯一示例是here,该示例中使用的“数据”来自文件系统读取,而不是直接来自请求。但是保存文件以加载它然后将其删除对我来说似乎是一个可怕的解决方法,所以我想知道我应该以什么形式以及如何处理我必须为 API 正确发送的“文件”打电话。


编辑:如果我使用 file.data(我认为这是最正确的,因为它将原始图像作为正文发送)我收到一个错误,说我必须使用字符串或缓冲区作为内容。显然,file.data 不是“body”需要 O.o 的缓冲区,我不是很理解。


已解决,错误非常愚蠢。在“then”部分, res.write(result) 不接受 result 作为参数。当我实际使用正确的请求(file.data 是一个缓冲区)时,就会发生这种情况。每次我尝试在 file.data 上使用 toString() 时都会发生其他错误,在这种情况下,请求未被接受。

【问题讨论】:

  • 您是否使用github.com/expressjs/multer 来解析文件上传?你能确认req.files.file 是不是一个缓冲区吗?
  • 如果是缓冲区,可以试试file.toString('binary')file.toString('utf-8')吗?让我知道它是否有效,以便我可以将其发布为答案
  • @Theo 我没有使用 multer,我使用的是 express-fileupload。 req.files.file 包含一个 json 对象, req.files.file.data 包含一个缓冲区。但由于某种原因,我无法将该缓冲区分配给计算机视觉请求正文字段。
  • 是的,因为文档说你应该把image_binary body,而不是缓冲区github.com/viane/microsoft-computer-vision#analyze-image 你可以试试file.toString('binary') 还是file.toString('utf-8')?让我知道它是否有效,以便我可以将其发布为答案
  • 它没有。在这两种情况下,我都会收到“InvalidImageFormat 错误。“输入数据不是有效的图像。”

标签: node.js api express raw-data


【解决方案1】:

已解决,请求请求缓冲区,而file.data确实是缓冲区。在以任何可能的方式检查 file.data 类型后,我开始寻找其他问题。这个错误要容易得多,请原谅我太愚蠢了,太愚蠢了,以至于看不出来。结果是 json,res.write 不接受 json 作为参数。

【讨论】:

    【解决方案2】:

    这就是我使用 Amazon Recognition Image Classifier 的方法,我知道它与您使用的服务不同 - 希望这对您有所帮助:

    const imagePath = `./bat.jpg`;
    const bitmap = fs.readFileSync(imagePath);
    const params = {
                Image: { Bytes: bitmap },
                MaxLabels: 10,
                MinConfidence: 50.0
            };
    route.post('/', upload.single('image'), (req, res) => {
        let params = getImage();
        rekognition.detectLabels(params, function(err, data) {
            if (err) {
                console.log('error');
            }else {
                console.log(data);
                res.json(data);
            }
        });
    });
    

    【讨论】:

    • 好吧......也许如果你解释一下“upload.single”来自哪里,如果你的“路线”是一个快速应用程序,我可以更好地理解差异......记住我想要避免必须将文件保存在本地再次加载然后删除它。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-04-05
    • 1970-01-01
    • 2018-04-20
    • 2013-11-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多