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.

  • If I understood Ruby docs correctly, File.Read will read the file as if text unless you specify `mode: "rb"`. So depending on your content, you may have uploaded and invalid movie file. – cthrash May 18 '16 at 03:32

1 Answers1

0

You do not need parse the "oid" out of "operation-location" header, as it is already the URL you should GET the status. The following code works for me. Use it to see if you still see the issue.

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
Felix Wang
  • 11
  • 3