【发布时间】:2016-09-06 06:26:59
【问题描述】:
我正在使用 Microsoft Emotion API 在 Rails 应用程序中处理视频中的情绪。我能够调用 API 来提交操作,但现在我必须查询另一个 API 以获取操作的状态,一旦完成,它将提供情绪数据。
我的问题是,当我查询结果 API 时,响应是找不到我的操作。如题,不存在。
我首先通过我的控制器发送了以下请求,效果很好:
#static controller
uri = URI('https://api.projectoxford.ai/emotion/v1.0/recognizeinvideo')
uri.query = URI.encode_www_form({})
request = Net::HTTP::Post.new(uri.request_uri)
request['Ocp-Apim-Subscription-Key'] = ENV['MEA_SubscriptionKey1']
request['Content-Type'] = 'application/octet-stream'
request.body = File.read("./public/mark_zuck.mov")
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(request)
end
# Get response headers
response.each_header do |key, value|
p "#{key} => #{value}"
end
# Get operation location and id of operation
operation_location = response["operation-location"]
oid = operation_location.split("/")[6]
第一次调用的响应是:
"operation-location => https://api.projectoxford.ai/emotion/v1.0/operations/e7ef2ee1-ce75-41e0-bb64-e33ce71b1668"
该协议用于抓取"operation-location" url的末尾,即操作id,并将其发送回结果API url,如下所示:
# parse operation ID from url and add it to results API url
url = 'https://api.projectoxford.ai/emotion/v1.0/operations/' + oid
uri = URI(url)
uri.query = URI.encode_www_form({})
request = Net::HTTP::Get.new(uri.request_uri)
request['Ocp-Apim-Subscription-Key'] = ENV['MEA_SubscriptionKey1']
response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
http.request(request)
end
# Get response headers
response.each_header do |key, value|
p "#{key} => #{value}"
end
我得到的结果是:
"{\"error\":{\"code\":\"Unspecified\",\"message\":\"Operation not found.\"}}"
当我使用通过我的应用创建的操作的操作 ID 查询 Microsoft 在线 API 控制台时,我得到了相同的结果。
有人对此有任何想法或经验吗?我将不胜感激。
【问题讨论】:
-
如果我正确理解了 Ruby 文档,File.Read 将像文本一样读取文件,除非您指定
mode: "rb"。因此,根据您的内容,您可能上传了无效的电影文件。
标签: ruby-on-rails json api net-http microsoft-cognitive