0

I'm working with the Microsoft Emotion API for processing emotions in video in a Rails app. I was able to make the call to the API to submit an operation, but now I have to query another API to get the status of the operation and once it's done it will provide the emotions data.

My issue is that when I query the results API, the response is that my operation is not found. As in, it doesn't exist.

I first sent the below request through my controller, which worked great:

#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]

The response of this first call is:

"operation-location => https://api.projectoxford.ai/emotion/v1.0/operations/e7ef2ee1-ce75-41e0-bb64-e33ce71b1668"

The protocol is for one to grab the end of the "operation-location" url, which is the operation id, and send it back to the results API url like below:

    # 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

The result I get is:

"{\"error\":{\"code\":\"Unspecified\",\"message\":\"Operation not found.\"}}"

I get the same result when I query the Microsoft online API console with the operation id of an operation created through my app.

Does anyone have any ideas or experience with this? I would greatly appreciate it.

4

1 回答 1

0

您不需要从“操作位置”标头中解析“oid”,因为它已经是您应该获取状态的 URL。以下代码适用于我。使用它来查看您是否仍然看到问题。

require 'net/http'
require 'uri'

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'] = '<your key>'
request['Content-Type'] = 'application/octet-stream'
videoFile = File.open("c:\\1mb.mp4", "rb")
request.body = videoFile.read
videoFile.close

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

puts response.message
puts response.read_body

# Get response headers
response.each_header do |key, value|
  p "#{key} => #{value}"
end

# Get operation location url for subsequent calls 
operation_location = response["operation-location"]

operation_url = operation_location
uri = URI(operation_url)
uri.query = URI.encode_www_form({})

loop do
    request = Net::HTTP::Get.new(uri.request_uri)
    request['Ocp-Apim-Subscription-Key'] = '<your key>'
    response = Net::HTTP.start(uri.host, uri.port, :use_ssl => uri.scheme == 'https') do |http|
        http.request(request)
    end
    puts response.read_body
    response_msg = response.read_body
    break if response_msg.include?("Succeeded") or response_msg.include?("Failed")
    sleep 20
end

puts response.message
puts response.read_body
于 2016-05-19T09:53:37.137 回答