0

I am trying to reproduce in R the Microsoft Emotion API program located here. I obtained my API key from Microsoft's website and plugged it into the following code (also from the above linked page:

library(httr)

apiUrl <- 'https://api.projectoxford.ai/emotion/v1.0/recognizeInVideo?outputStyle=perFrame'

key <- '2a0a91c98b57415a....'

urlVideo <- 'https://drive.google.com/file/d/0B5EuiMJDiDhWdWZSUGxvV3lJM0U/view?usp=sharing'

mybody <- list(url = urlVideo)

faceEMO <- httr::POST(
    url = apiUrl,
    httr::content_type('application/json'),
    httr::add_headers(.headers = c('Ocp-Apim-Subscription-Key' = key)),
    body = mybody,
    encode = 'json'
)

operationLocation <- httr::headers(faceEMO)[["operation-location"]]

The request appears to go through successfully as the "faceEMO" object returns a Response 202 code which according to Microsoft's website means:

The service has accepted the request and will start the process later. In the response, there is a "Operation-Location" header. Client side should further query the operation status from the URL specified in this header.

However, when I open the given URL from the 'operationLocation' object, it says:

{ "error": { "code": "Unauthorized", "message": "Access denied due to invalid subscription key. Make sure you are subscribed to an API you are trying to call and provide the right key." } }

This seems to indicate that my request didn't go through after all.

Not sure why it would say my key is invalid. I tried to regenerate the key and run it again but received the same result. Maybe it has something to do with the fact that Microsoft gives me 2 keys? Do I need to use both?

To add additional info, I also tried running the next few lines of code given from the linked web site:

while(TRUE){
    ret <- httr::GET(operationLocation,
                     httr::add_headers(.headers = c('Ocp-Apim-Subscription-Key' = key)))
    con <- httr::content(ret)
    if(is.null(con$status)){
        warning("Connection Error, retry after 1 minute")
        Sys.sleep(60)
    } else if (con$status == "Running" | con$status == "Uploading"){
        cat(paste0("status ", con$status, "\n"))
        cat(paste0("progress ", con$progress, "\n"))
        Sys.sleep(60)
    } else {
        cat(paste0("status ", con$status, "\n"))
        break()
    }
}

When I run this I'm shown a message indicating "status Running" and "progress 0" for about 30 seconds. Then it shows "status Failed" and stops running without giving any indication of what caused the failure.

John Sukup
  • 303
  • 3
  • 11

1 Answers1

0

It seems the API requires a direct download link, instead of what you supplied. Normally, you could simply change the url to...

urlVideo <- "https://drive.google.com/uc?export=download&id=0B5EuiMJDiDhWdWZSUGxvV3lJM0U"

I've confirmed that the Emotion API works on small test videos I've uploaded to Google Drive. Unfortunately, the issue now is that your specific video file is large enough that you run into the annoying Google Drive can't scan this file for viruses interruption. As far as I can tell, no workaround exists to bypass this. More on this here.

Your best bet is to host the video somewhere other than Google Drive, and make sure the body for the post call contains the direct download link.

Community
  • 1
  • 1
Chrisss
  • 3,211
  • 1
  • 16
  • 13
  • Thanks for this. I think it helped a bit, but I'm still running into an issue. I changed the storage to MSFT OneDrive and therefore the link to object "urlVideo" in the code above. Now when I run the code above I get the error "Video size is too small or too big." Seems self explanatory but I cannot seem to find documentation of what "too small" is (too large is given as 100MB on the API ref site). My file is 29.7MB. Any ideas? If not I suppose I can try and use a larger video size to see if that fixes it, but I'd like to keep the videos as small as possible for storage purposes. – John Sukup Feb 05 '17 at 07:49
  • @JohnSukup Could you provide me with the new url you are trying to use? – Chrisss Feb 05 '17 at 16:14
  • Here you go. I'm never sure how to handle file sharing for these so I wonder if that is another issue that could be causing the problem (although the error message would seem to indicate otherwise): https://1drv.ms/v/s!AiNgu_ULDmxMhHFZM8vK-519_xBF (NOTE: there is an "https://" at the beginning of this link that Stack isn't showing when I post for some reason). – John Sukup Feb 05 '17 at 17:16
  • Thanks for this. I gave it a go and it does appear to take care of the "Video size too small/big" error message, but now I'm just getting a generic "Video processing failed" message. There is no error code and nothing on MSFT's website that I can find to indicate what could be causing this. I may just have to try another video or maybe a new format. I appreciate your help, but do you have any more ideas as to what is causing the issue? Also note that I used your link above in my browser and it did, indeed, begin to download the MP4 video. – John Sukup Feb 05 '17 at 18:28
  • @JohnSukup OK, I think I got it. This is no longer about R, or the Emotion API, but about the video hosting setup you have. Basically I was lazy in making the direct download link for your oneDrive file. It only worked on my browser because I was signed into my own oneDrive account. Use this: `orig_url <- "https://1drv.ms/v/s!As_lzte5x5PDaPyuv0F3AN91nHo"; urlVideo <- RCurl::base64(orig_url) %>% gsub("\\+", "\\-", .) %>% gsub("\\/", "\\_", .) %>% sub("\\=$", "", .) %>% paste0("https://api.onedrive.com/v1.0/shares/u!", ., "/root/content")` – Chrisss Feb 05 '17 at 20:25
  • Excellent! This appears to have done it. I noticed the "orig_url" link you gave is different than the one I gave in my previous comment. Is there a reason for this? I'm just thinking about how I'll be able to reproduce this again in the future with different video clips. Great help, Chrisss! – John Sukup Feb 06 '17 at 21:46
  • @JohnSukup I hosted your video on my own OneDrive as a test-case since your link no longer points to the video file (you might have deleted it or unshared it). You should be able to change the "orig_url" once you re-share/re-upload the file. – Chrisss Feb 06 '17 at 21:56