this is the first time I'm attempting to build a function using R. Basically my intended goal are as follows.
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!
Thanks in advance!
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))
}
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))
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With