在循环中创建变量和数据集? (右)

时间:2019-03-14 04:59:32

标签: r macos function for-loop if-statement

这是我第一次尝试使用R构建函数。基本上我的预期目标如下。

  • 使用RoogleVision软件包与Google Cloud Vision API通讯
  • 该功能遍历目录中的图像
  • 从每张照片的Google Vision功能中检索所需的信息
  • 将它们保存在单个聚合数据集中

下面是我正在使用的示例代码。我认为我正在苦苦挣扎的唯一部分是正确“迭代”图片并不断创建数据集。

任何帮助和建议都将受到赞赏!

谢谢!

googlevision <- function(path) {
    path <- dirname(file.choose())  # Get directory
    setwd(path)
    pic_list <- list.files(path = path, pattern = "*.png")  # Get filename lists
    vision_data <- NULL
    for (i in pic_list) {
            text <- getGoogleVisionResponse(i, feature = "TEXT_DETECTION")
            text_lang <- text[[1]][1]
            ad_text <- paste(text[[2]][2:as.numeric(length(text[[2]])-20)], sep = " ", collapse = " ")
            vision_data <- bind_rows(c("text_lang" = text[[1]][1], 
                                       "ad_text" = paste(text[[2]][2:as.numeric(length(text[[2]])-20)], sep = " ", collapse = " ")))
            if(colnames(getGoogleVisionResponse(i, feature = "FACE_DETECTION"))[1] != "error"){
                    face <- getGoogleVisionResponse(i, feature = "FACE_DETECTION")
                    face_conf <- face$detectionConfidence
                    joy <- face$joyLikelihood
                    sorrow <- face$sorrowLikelihood
                    anger <- face$angerLikelihood
                    surprise <- face$surpriseLikelihood
                    underExposed <- face$underExposedLikelihood
                    blur <- face$blurredLikelihood
                    headwear <- face$headwearLikelihood
            } 
            if(colnames(getGoogleVisionResponse(i, feature = "LABEL_DETECTION"))[1] != "error"){
                    label <- getGoogleVisionResponse(i, feature = "LABEL_DETECTION")
                    label_desc <- label$description
                    label_score <- label$score
            }
            visual_data <- bind_rows(c("face_conf" = face_conf,
                               "joy" = joy,
                               "sorrow" = sorrow,
                               "anger" = anger, "surprise" = surprise, "underExposed" = underExposed,
                               "blur" = blur, "headwear" = headwear, "text_lang" = text_lang, "ad_text" = ad_text))
    }

1 个答案:

答案 0 :(得分:1)

尝试使用创建list来存储每次迭代中的数据框:

googlevision <- function(path) {
  path <- dirname(file.choose())  # Get directory
  setwd(path)
  pic_list <- list.files(path = path, pattern = "*.png")  # Get filename lists
  vision_data_list <- list()
  for (i in pic_list) {
    text <- getGoogleVisionResponse(i, feature = "TEXT_DETECTION")
    text_lang <- text[[1]][1]
    ad_text <- paste(text[[2]][2:as.numeric(length(text[[2]])-20)], sep = " ", collapse = " ")
    vision_data <- bind_rows(c("text_lang" = text[[1]][1], 
                               "ad_text" = paste(text[[2]][2:as.numeric(length(text[[2]])-20)], sep = " ", collapse = " ")))
    if(colnames(getGoogleVisionResponse(i, feature = "FACE_DETECTION"))[1] != "error"){
      face <- getGoogleVisionResponse(i, feature = "FACE_DETECTION")
      face_conf <- face$detectionConfidence
      joy <- face$joyLikelihood
      sorrow <- face$sorrowLikelihood
      anger <- face$angerLikelihood
      surprise <- face$surpriseLikelihood
      underExposed <- face$underExposedLikelihood
      blur <- face$blurredLikelihood
      headwear <- face$headwearLikelihood
    } 
    if(colnames(getGoogleVisionResponse(i, feature = "LABEL_DETECTION"))[1] != "error"){
      label <- getGoogleVisionResponse(i, feature = "LABEL_DETECTION")
      label_desc <- label$description
      label_score <- label$score
    }
    visual_data <- data.frame("face_conf" = face_conf,
                              "joy" = joy,
                              "sorrow" = sorrow,
                              "anger" = anger, "surprise" = surprise, "underExposed" = underExposed,
                              "blur" = blur, "headwear" = headwear, "text_lang" = text_lang, "ad_text" = ad_text)
    vision_data_list<-c(vision_data_list,list(visual_data))
  }
  return (do.call(rbind,vision_data_list))
}