R/azure_result_utls.R

Defines functions az_lines_to_df az_words_to_df

Documented in az_lines_to_df az_words_to_df

#'@title convert azure line words to df
#'@param line line of azure result
#'@param type word or line
#'@export
az_words_to_df <- function(line, type = 'word') {
  if (type == 'word') {
    line <- line$words
  }
  
  res <- line %>% purrr::map_df(function(x) {
    tmp_df <- tibble::tibble(text = x$text)
    bbox_df <- x$boundingBox %>% pts_to_wh() %>% t() %>%
      tibble::as_tibble(); colnames(bbox_df) <- c('x', 'y', 'w', 'h')
    return(dplyr::bind_cols(tmp_df, bbox_df) %>%
             dplyr::mutate(word_id = seq(1, dplyr::n(), 1)))
  })
  if (type == 'word') return(res) else return(res %>% dplyr::select(-word_id) %>% 
                                                dplyr::mutate(line_id = seq(1, dplyr::n(), 1)))
}

#'@title convert azure lines to df
#'@param lines result lines
#'@export
az_lines_to_df <- function(lines) {
  1:length(lines) %>%
    purrr::map_df(function(x){
      #print(x)
      l_id <- x
      res <- az_words_to_df(lines[[x]]) %>%
        dplyr::mutate(line_id = l_id)
      return(res)
    }) %>% 
    dplyr::mutate(combo_id = paste0(line_id, "_", word_id))
}
Atan1988/alvision documentation built on Dec. 15, 2020, 7:03 a.m.