在循环中创建变量和数据集? (右) [英] Create variable and dataset in a loop? (R)
本文介绍了在循环中创建变量和数据集? (右)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我第一次尝试使用R构建函数.基本上我的预期目标如下.
this is the first time I'm attempting to build a function using R. Basically my intended goal are as follows.
- 使用RoogleVision软件包与Google Cloud Vision API通讯
- 该功能遍历目录中的图像
- 从每张照片的Google Vision功能中检索所需的信息
- 将它们保存在单个聚合数据集中
下面是我正在使用的示例代码.我认为我正在苦苦挣扎的唯一部分是适当地迭代"图片并不断创建数据集.
Below is the sample code I'm using. The only part I think I'm struggling is properly "iterating" through the pictures and continuously creating a dataset.
任何帮助和建议都将受到赞赏!
Any helps and advice are appreciated!
提前谢谢!
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
在每次迭代中存储数据框:
Try to use create a list
to store your data frame in each iteration:
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))
}
这篇关于在循环中创建变量和数据集? (右)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文