R/computer_vision.R

key <- "17946efb2b894ba3a09aad07c885059e"

tiger <- image_read('http://jeroen.github.io/images/tiger.svg')
frink <- image_read("https://jeroen.github.io/images/frink.png")
logo <- image_read("https://www.r-project.org/logo/Rlogo.png")
im <- "https://previews.123rf.com/images/anatols/anatols1104/anatols110400057/9330580-Kids-group-playing-with-ball-in-the-backyard-Stock-Photo-soccer.jpg"
hand <- "http://joshldavis.com/img/handwriting/dijkstras.png"
ss <- "http://ezstreetsigns.com/services/imagecache/7b7c898b-cb3f-41c4-b182-aa6269d6300b.png"

describe_image <- function(image_url, max_candidates = 1) {
  check_numeric(max_candidates, "max_candidates")
  api_url <- paste("https://westus.api.cognitive.microsoft.com/vision/v1.0/describe?maxCandidates=", max_candidates, sep = "")

  #Setting key value, will throw error if env var not set
  key <- get_cv_key()

  #Coverting the request body to JSON
  request_body_json <- jsonlite::toJSON(list(url = image_url), auto_unbox = TRUE)

  #Sending request
  result <- httr::POST(api_url, body = request_body_json, httr::add_headers(.headers = c("Ocp-Apim-Subscription-Key" = key)))

  #Throwing error if request does not return json
  if (httr::http_type(result) != "application/json") {
    stop("API did not return json", call. = FALSE)
  }

  #Converting JSON to R-usable object
  output <- httr::content(result)

  #Throwing error if status code != 200
  if (httr::status_code(result) != 200) {
    stop(
      paste(
        "API request failed.\nStatus code:",
        output$statusCode, "\nMessage:",
        output$message
      ),
      call. = FALSE
    )
  }

  n <- NROW(output)
  #Coverting output to a dataframe
  #output <- data.frame(matrix(unlist(output), nrow = n, byrow = T))
  #names(output) <- c("Id", "Language", "Kind", "Specification", "Implementation")
  output
}

analyze_image <- function(image_url) {
  api_url <- "https://westus.api.cognitive.microsoft.com/vision/v1.0/analyze"

  #Setting key value, will throw error if env var not set
  key <- get_cv_key()

  #Coverting the request body to JSON
  request_body_json <- jsonlite::toJSON(list(url = image_url), auto_unbox = TRUE)

  #Sending request
  result <- httr::POST(api_url, body = request_body_json, httr::add_headers(.headers = c("Ocp-Apim-Subscription-Key" = key)))

  #Throwing error if request does not return json
  if (httr::http_type(result) != "application/json") {
    stop("API did not return json", call. = FALSE)
  }

  #Converting JSON to R-usable object
  output <- httr::content(result)

  #Throwing error if status code != 200
  if (httr::status_code(result) != 200) {
    stop(
      paste(
        "API request failed.\nStatus code:",
        output$statusCode, "\nMessage:",
        output$message
      ),
      call. = FALSE
    )
  }

  n <- NROW(output)
  #Coverting output to a dataframe
  #output <- data.frame(matrix(unlist(output), nrow = n, byrow = T))
  #names(output) <- c("Id", "Language", "Kind", "Specification", "Implementation")
  output
}

recognize_handwriting <- function(image_url) {
  api_url <- "https://westus.api.cognitive.microsoft.com/vision/v1.0/recognizeText?handwriting=TRUE"

  #Setting key value, will throw error if env var not set
  key <- get_cv_key()

  #Coverting the request body to JSON
  request_body_json <- jsonlite::toJSON(list(url = image_url), auto_unbox = TRUE)

  #Sending request
  result1 <- httr::POST(api_url, body = request_body_json, httr::add_headers(.headers = c("Ocp-Apim-Subscription-Key" = key)))

  #Throwing error if status code != 202
  if (httr::status_code(result1) != 202) {
    stop(
      paste(
        "API request failed.\nStatus code:",
        output$statusCode, "\nMessage:",
        output$message
      ),
      call. = FALSE
    )
  }

  result2 <- get_operation_status_cv(result1$headers$`operation-location`)
  output <- httr::content(result2)
  while (output$status == "Running") {
    result2 <- get_operation_status_cv(result1$headers$`operation-location`)
    output <- httr::content(result2)
  }
  #Throwing error if request does not return json
  if (httr::http_type(result2) != "application/json") {
    stop("API did not return json", call. = FALSE)
  }

  #Converting JSON to R-usable object
  output <- httr::content(result2)

  #Throwing error if status code != 200
  if (httr::status_code(result2) != 200) {
    stop(
      paste(
        "API request failed.\nStatus code:",
        output$statusCode, "\nMessage:",
        output$message
      ),
      call. = FALSE
    )
  }

  n <- NROW(output)
  #Coverting output to a dataframe
  #output <- data.frame(matrix(unlist(output), nrow = n, byrow = T))
  #names(output) <- c("Id", "Language", "Kind", "Specification", "Implementation")
  output
}

recognize_text <- function(image_url) {
  api_url <- "https://westus.api.cognitive.microsoft.com/vision/v1.0/recognizeText?handwriting=FALSE"

  #Setting key value, will throw error if env var not set
  key <- get_cv_key()

  #Coverting the request body to JSON
  request_body_json <- jsonlite::toJSON(list(url = image_url), auto_unbox = TRUE)

  #Sending request
  result <- httr::POST(api_url, body = request_body_json, httr::add_headers(.headers = c("Ocp-Apim-Subscription-Key" = key)))

  #Throwing error if request does not return json
  if (httr::http_type(result) != "application/json") {
    stop("API did not return json", call. = FALSE)
  }

  #Converting JSON to R-usable object
  output <- httr::content(result)

  #Throwing error if status code != 200
  if (httr::status_code(result) != 200) {
    stop(
      paste(
        "API request failed.\nStatus code:",
        output$statusCode, "\nMessage:",
        output$message
      ),
      call. = FALSE
    )
  }

  n <- NROW(output)
  #Coverting output to a dataframe
  #output <- data.frame(matrix(unlist(output), nrow = n, byrow = T))
  #names(output) <- c("Id", "Language", "Kind", "Specification", "Implementation")
  output
}

tag_image <- function(image_url) {
  api_url <- "https://westus.api.cognitive.microsoft.com/vision/v1.0/tag"

  #Setting key value, will throw error if env var not set
  key <- get_cv_key()

  #Coverting the request body to JSON
  request_body_json <- jsonlite::toJSON(list(url = image_url), auto_unbox = TRUE)

  #Sending request
  result <- httr::POST(api_url, body = request_body_json, httr::add_headers(.headers = c("Ocp-Apim-Subscription-Key" = key)))

  #Throwing error if request does not return json
  if (httr::http_type(result) != "application/json") {
    stop("API did not return json", call. = FALSE)
  }

  #Converting JSON to R-usable object
  output <- httr::content(result)

  #Throwing error if status code != 200
  if (httr::status_code(result) != 200) {
    stop(
      paste(
        "API request failed.\nStatus code:",
        output$statusCode, "\nMessage:",
        output$message
      ),
      call. = FALSE
    )
  }

  n <- NROW(output)
  #Coverting output to a dataframe
  #output <- data.frame(matrix(unlist(output), nrow = n, byrow = T))
  #names(output) <- c("Id", "Language", "Kind", "Specification", "Implementation")
  output
}

get_cv_key <- function() {
  key <- Sys.getenv('KEY_CV')
  if (identical(key, "")) {
    stop("Please set env var KEY_CV to your key for the computer vision APIS. Key provided by Microsoft (https://www.microsoft.com/cognitive-services)",
         call. = FALSE)
  }

  key
}

check_numeric <- function(x, variable) {
  if (!is.numeric(x)) {
    stop(paste(variable, "must be number"), call. = FALSE)
  }
}

get_operation_status_cv <- function(url) {
  key <- get_cv_key()

  #Sending request
  result <- httr::GET(url, httr::add_headers(.headers = c("Content-Type" = "application/json",
                                                          "Ocp-Apim-Subscription-Key" = key)))
  result
}
dereklegenzoff/cognitiver documentation built on May 5, 2019, 3:49 a.m.