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

2024-02-08

这是我第一次尝试使用 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))
    }

尝试使用创建一个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))
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

在循环中创建变量和数据集? (右) 的相关文章

随机推荐