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

时间:2019-06-17 02:07:55

标签: r azure image-processing face-api

我正在使用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仅在我尝试循环时出现。任何帮助将不胜感激。

1 个答案:

答案 0 :(得分:1)

在循环中,x是1到length(image_list)之间的数字。您可能需要image_list[x]