【发布时间】:2019-10-30 14:44:15
【问题描述】:
我正在使用 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]。 -
谢谢!这就像一个魅力。我怎么会错过呢; p ...如果您将其发布为答案,我将选择它。我认为这对以后遇到类似问题的人会有所帮助。
-
@HongOoi 你会发布评论作为答案,以便 [Jim O] 可以将此评论标记为答案吗?
标签: r azure image-processing face-api