R: управлять транзакцией в минуту при подключении к API Azure Cognitive Service через HTTR - PullRequest
0 голосов
/ 22 февраля 2019

Azure Cognitive Service позволяет выполнять до 20 транзакций в минуту в бесплатной пробной версии.Я подготовил код для анализа списка изображений по Face API - V1.0 Давайте посмотрим код:

library(httr)
library("readxl")
library("XML")

destination <- "C:/Users/uzytkownik/Pictures/image/"
myfiles_jpg <- list.files(path = destination, pattern = "jpg",  full.names = TRUE)

face_api_url <- 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect?returnFaceId=true&returnFaceAttributes=age,gender,smile,facialHair,glasses,emotion,hair,makeup,occlusion,accessories,blur,exposure,noise'

key1 <- 'XXX'
table <-c()

for (i in 1:10){
Sys.sleep(4)
img <- myfiles_jpg[i]

body_image = upload_file(img)
# Below is the POST method (Adding Request headers using add_headers)
Sys.sleep(3.3)
result = POST(face_api_url,
              body = body_image, content_type('application/json'),encode = "json",
              add_headers(.headers = c("Content-Type"="application/octet-stream",
                                       "Ocp-Apim-Subscription-Key"="XXX")))
outcome = httr::content(result)[[1]]
names(outcome)
### f -face attributes
f <- outcome$faceAttributes
ifelse(f$glasses == 'NoGlasses', glasses <-0, glasses <-1)
ifelse(f$makeup$`eyeMakeup`==FALSE, eyemakeup <- 0, eyemakeup <-1)
ifelse(f$makeup$lipMakeup == FALSE, lipmakeup <-0, lipmakeup<-1)
ifelse(max(f$emotion$`anger`,f$emotion$contempt,f$emotion$disgust,f$emotion$fear,f$emotion$happiness,f$emotion$neutral,
           f$emotion$sadness,f$emotion$surprise) ==f$emotion$`anger`, topanger<-1, topanger<-0 )
ifelse(max(f$emotion$`anger`,f$emotion$contempt,f$emotion$disgust,f$emotion$fear,f$emotion$happiness,f$emotion$neutral,f$emotion$sadness,f$emotion$surprise) ==f$emotion$contempt, topcontempt<-1, topcontempt<-0 )
ifelse(max(f$emotion$`anger`,f$emotion$contempt,f$emotion$disgust,f$emotion$fear,f$emotion$happiness,f$emotion$neutral,f$emotion$sadness,f$emotion$surprise) ==f$emotion$disgust, topdisgust<-1, topdisgust<-0 )
ifelse(max(f$emotion$`anger`,f$emotion$contempt,f$emotion$disgust,f$emotion$fear,f$emotion$happiness,f$emotion$neutral,f$emotion$sadness,f$emotion$surprise) ==f$emotion$fear, topfear<-1, topfear<-0 )
ifelse(max(f$emotion$`anger`,f$emotion$contempt,f$emotion$disgust,f$emotion$fear,f$emotion$happiness,f$emotion$neutral,f$emotion$sadness,f$emotion$surprise) ==f$emotion$happiness, tophappiness<-1, tophappiness<-0 )
ifelse(max(f$emotion$`anger`,f$emotion$contempt,f$emotion$disgust,f$emotion$fear,f$emotion$happiness,f$emotion$neutral,f$emotion$sadness,f$emotion$surprise) ==f$emotion$neutral, topneutral<-1, topneutral<-0 )
ifelse(max(f$emotion$`anger`,f$emotion$contempt,f$emotion$disgust,f$emotion$fear,f$emotion$happiness,f$emotion$neutral,f$emotion$sadness,f$emotion$surprise) ==f$emotion$sadness, topsadness<-1, topsadness<-0 )
ifelse(max(f$emotion$`anger`,f$emotion$contempt,f$emotion$disgust,f$emotion$fear,f$emotion$happiness,f$emotion$neutral,f$emotion$sadness,f$emotion$surprise) ==f$emotion$surprise, topsurprise<-1, topsurprise<-0 )
new_row <- data.frame(myfiles_jpg[i],f$'smile',
                      f$age,
                      f$facialHair$`moustache`,
                      f$facialHair$beard,
                      glasses,
                      f$emotion$`anger`,
                      f$emotion$contempt,
                      f$emotion$disgust,
                      f$emotion$fear,
                      f$emotion$happiness,
                      f$emotion$neutral,
                      f$emotion$sadness,
                      f$emotion$surprise,
                      f$blur$value,
                      f$exposure$value,
                      eyemakeup,
                      lipmakeup,
                      topanger,
                      topcontempt,
                      topdisgust,
                      topfear,
                      tophappiness,
                      topneutral,
                      topsadness,
                      topsurprise)
colnames(new_row)<- c("image_url","smile", "age","moustache", "beard", "glasses", "anger", "contempt", "disgust", "fear", "happiness", "neutral", "sadness", "surprise", "blur_value", "exposure_value", "eyemakeup", "lipmakeup", 'topanger', 'topcontempt', 'topdisgust', 'topfear', 'tophappiness','topneutral','topsadness','topsurprise')
table <- rbind(table,new_row)
}

Однако, после 6/10 изображений я получил остановку и ошибку:

Error in httr::content(result)[[1]] : subscript out of bounds

(. Csv содержит анализ только шести граней!)

Я реализовал Sys.sleep, но, похоже, я превысил предел.Как я могу управлять кодом для выполнения до 20 транзакций в минуту и ​​не превышать лимит бесплатной пробной версии?

...