r - 从R中的图片数据创建数据框
问题描述
我需要从图片创建数据框,即在参数上分割图片。我使用 Azure 认知服务
q <- "?returnFaceId=true&returnFaceLandmarks=true&returnFaceAttributes=age,gender,smile,headPose,facialHair,glasses,emotion"
url1 <- paste( q, sep="")
#path to my folder with pictures
pic1="C:/pictures" #here some pictures
library("httr")
#send the request to Face API
# my keys calls FaceRecognition
response = POST(url=url1, body=pic1, add_headers(.headers =
c('Content-Type'='application/octet-stream', 'FaceRecognition'='12345...32')))
result <- content(response)
result
df <- as.data.frame(result)
# pivot the data frame...you need to add package reshape2 for this
library("reshape2")
df2 <- melt(df, id=c("faceId"))
所以在我得到错误之后
Error in curl::curl_fetch_memory(url, handle = handle) : <url> malformed
和
Error: id variables not found in data: faceId
如何获得这样的输出
笔记
Azure 对我来说不是万能的,我以它为例进行实践,如果您知道更简单的方法来获得所需的输出,我将非常感谢您。
解决方案
看起来你正在粘贴一个字符串。paste 接受多个参数并使用 sep 将它们连接成单个字符对象以分隔它们。所以 url1 只是 q,这确实是一个格式错误的 url,因为没有协议(例如 http://)或 url(即 example.com/face-recognition-endpoint)。其余的就是这样的结果。
尝试在粘贴语句中添加一个 url,例如
base <- "https://northeurope.api.cognitive.microsoft.com/face/v1.0/identify"
q <- "?returnFaceId=true&returnFaceLandmarks=true&returnFaceAttributes=age,gender,smile,headPose,facialHair,glasses,emotion"
url1 <- paste(base, q, sep = "")
然后,对于 POST 命令,您需要指定要上传文件,我猜 azure 一次只需要一个文件,所以您需要类似
pic1 <- "C:/pictures/pic1.jpeg"
response <- POST(url = url1, body = upload_file(pic1) ...)
推荐阅读
- go - 如何在`golang`中包含我的`log`包而不是内置的`log`包?
- angular - 角度过滤器问题 - 类型上不存在属性过滤器
- javascript - 有没有办法通过 javascript get 请求从网页获取动态生成的内容?
- python - 使用 fetch_file 从文件夹中调用多个文件
- html - 为什么 Html Fieldset Legend 在 safari 浏览器或在 iphone 中运行的 Ionic 应用程序中无法正确显示?
- java - Keystore.setKeyEntry() 中的 Certificate[] 链是什么意思以及如何从 JKS 或 PKCS12 获取该信息?
- excel - Vlookup - 如何处理 #n/a 错误 - office 365
- php - 如何在与 wordpress 插件中的动作挂钩相关的函数之间传递我自己的变量
- javascript - 如何在反应表上实现延迟加载数据?
- amazon-web-services - 我可以使用 CloudFront 为多个域提供服务吗?