首页 > 解决方案 > 从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 对我来说不是万能的,我以它为例进行实践,如果您知道更简单的方法来获得所需的输出,我将非常感谢您。

标签: razuredataframeface-recognition

解决方案


看起来你正在粘贴一个字符串。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) ...)

推荐阅读