首页 > 解决方案 > R - 错误404与API的人脸认知识别连接

问题描述

我想通过 R 连接微软人脸识别 API。但是,我得到了 404 错误。这是我的代码(修改自: https ://www.r-bloggers.com/analyze-face-emotions-with-r/ )

library(httr)
library("XML")

#guide from: https://www.r-bloggers.com/analyze-face-emotions-with-r/
faceURL <- 'https://westcentralus.api.cognitive.microsoft.com/face/v1.0'
img.url <- 'https://images.pexels.com/photos/614810/pexels-photo-614810.jpeg?auto=compress&cs=tinysrgb&h=650&w=940'
#from: https://azure.microsoft.com/pl-pl/try/cognitive-services/my-apis/
faceKEY <- 'XXX'
# Define image
mybody = list(url = img.url)

# Request data from Microsoft
faceResponse = POST(
  url = faceURL, 
  content_type('application/json'), add_headers(.headers = c('Ocp-Apim-Subscription-Key' = faceKEY)),
  body = mybody,
  encode = 'json'
)

# Show request results (if Status=200, request is okay)
faceResponse

#Outcome:
#Response [https://westcentralus.api.cognitive.microsoft.com/face/v1.0]
#  Date: 2019-02-15 11:14
#  Status: 404
#  Content-Type: application/json
#  Size: 113 B
#
#                   { "error": { "code": #"ResourceNotFound", "message": "The requested resource was not found." } }

我应该更正什么才能正确连接人脸识别 API?

标签: razureface-api

解决方案


您收到的错误是 404 ( { "error": { "code": #"ResourceNotFound", "message": "The requested resource was not found." } }),因此您应该查看您正在执行的请求。

实际上,您在这里尝试调用“ https://westcentralus.api.cognitive.microsoft.com/face/v1.0

这是 API 的根目录,其中有多种方法,例如Detect哪个 url 是“ https://westcentralus.api.cognitive.microsoft.com/face/v1.0/detect

在此处查看有关这些方法的更多详细信息:https ://westcentralus.dev.cognitive.microsoft.com/docs/services/563879b61984550e40cbbe8d


推荐阅读