调用计算机视觉OCR时出现415错误

问题描述 投票:2回答:1

我正在使用Azure计算机视觉API。我可以将图像URL发布到Azure并通过nodejs成功获得其结果。

但是,当我要发布本地图像而不是图像URL时,总是会出现415错误。那我错过了什么吗?

提前感谢。

javascript microsoft-cognitive
1个回答
2
投票

如果要使用nodejs将本地映像发布到Azure计算机OCR版本。只需尝试下面的代码:

const fetch = require("node-fetch")
const fs  = require('fs');


let subscriptionKey = '<your subscription key>';
let endpoint = '<your computer vision endpoint>';
let filePath = "<path of your local image>";
const base64 =  fs.readFileSync(filePath, 'base64')
const data = Buffer.from(base64, 'base64')

var uriBase = endpoint + "vision/v2.1/ocr";

fetch(uriBase ,
{
method: 'POST',
headers: 
    {
    'Content-Type': 'application/octet-stream',
    'Ocp-Apim-Subscription-Key': subscriptionKey,
    },
body: data,
}).then((response) => response.json()).then((data) =>
{
console.log(JSON.stringify(data, null, 2));
}).catch((error) =>
{
console.log(error);
});

据我所知,如果您使用multipart/form-data,将导致415错误。

结果:

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.