upload_file(x) 中的错误:is.character(path) 在循环中不为真

Error in upload_file(x) : is.character(path) is not TRUE in loop

我正在使用 Microsoft Face API 来检测面部情绪。经过反复试验,我有以下适用于单个文件的代码:

local <- "/mypath/image.jpg"

x_recognition <- function (x) {

  y = POST(Oxford,
           body = upload_file(x),
           add_headers(.headers = c("Content-Type"="application/octet-stream",
                                    "Ocp-Apim-Subscription-Key"=csAPI))
           )

      do.call(rbind,content(y)[[1]]$faceAttributes['emotion'])

}

x_recognition(local)

        anger contempt disgust fear happiness neutral sadness surprise
emotion 0     0        0       0    0         1       0       0       

但我的目标是处理一个文件夹中包含的多个图像。所以,我写了下面的代码但没有成功:

Image_list <- list.files(path = "/mypath", pattern="*.jpg", full.names=TRUE)
append_list <- data.frame()

for (x in 1:length(Image_list)) {

  y = POST(Oxford,
           body = upload_file(x),
           add_headers(.headers = c("Content-Type"="application/octet-stream",
                                    "Ocp-Apim-Subscription-Key"=csAPI))
  )

  emotionID = do.call(rbind, content(y)[[1]]$faceAttributes['emotion'])

  append_list <- rbind(append_list, emotionID)

}

以上代码出现以下错误:

Error in upload_file(x) : is.character(path) is not TRUE

过去一个小时左右,我一直在寻找解决方案。 list.files 好像不是问题。

> Image_List[1]
[1] "/mypath/image1.jpg"

>  Image_List
[1] "/mypath/image1.jpg"
[2] "/mypath/image2.jpg"
[3] "/mypath/image3.jpg"

当我尝试 运行 一个选定的图像时,它起作用了:

x_recognition(Image_List[1])
        anger contempt disgust fear happiness neutral sadness surprise
emotion 0     0        0       0    0         1       0       0       

Error in upload_file(x) : is.character(path) is not TRUE 仅在我尝试循环时出现。任何帮助将不胜感激。

在你的循环中,x 是一个从 1 到 length(image_list) 的数字。你可能想要 image_list[x].