gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-04 12:02:48 31 4
gpt4 key购买 nike

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

关于r - 在循环中创建变量和数据集? (右),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55155238/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com