使用 Ruby 将 base64 而不是 url 传递给 Microsoft Azure Computer vision API 的正确语法是什么

What is the correct syntax to pass a base64 instead of a url to Microsoft Azure Computer vision API using Ruby

require 'net/http'

uri = URI('https://westcentralus.api.cognitive.microsoft.com/vision/v1.0/ocr')
uri.query = URI.encode_www_form({

    'language' => 'unk',
    'detectOrientation ' => 'true'
})

request = Net::HTTP::Post.new(uri.request_uri)

request['Content-Type'] = 'application/octet-stream'

request['Ocp-Apim-Subscription-Key'] = 'MY_SUBSCRIPTION_KEY'

request.body = "raw_image_binary"


response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
    http.request(request)
end

puts response.body

我收到了这个回复。

{"code":"InvalidImageFormat","requestId":"3aee7147-2acc-4f64-b1c6-8453815eda38","message":"Input data is not a valid image."}

求助!!

听起来您想通过 application/octet-stream Ruby 中的内容类型将图像文件传递给 Azure Computer Vision API,因此您需要将二进制图像数据传递给 response.body, 不传递图片的base64字符串。

请使用下面的代码而不是当前的代码使其工作。

# Supported image formats: JPEG, PNG, GIF, BMP.
request.body = File.binread("<your image file name>") 
# Or `request.body = File.open("<your image file name>") {|io| io.read}` also works