这是我第一次尝试使用R构建函数。基本上我的预期目标如下。
下面是我正在使用的示例代码。我认为我正在苦苦挣扎的唯一部分是正确“迭代”图片并不断创建数据集。
任何帮助和建议都将受到赞赏!
谢谢!
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))
}
答案 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))
}