在R中使用Azure计算机视觉创建循环/分析多个图像的方法
我目前正在使用Microsoft Azure计算机视觉对> 10K图像进行分析,并在R编程软件中进行操作。我想看看这些10K图像中存在哪些标签,然后将最频繁的标签计算为最频繁的标签。所有这些图像当前都在我的计算机上。这不是出于商业目的,而是我的研究。
事实是,我是R中的新手,并且主要使用GGPLOT用于数据可视化。
当前,我仅设法在时间上运行1个图像的分析,我使用以下脚本
library(AzureRMR)
library(AzureCognitive)
# Create Computer Vision endpoint
endp <- cognitive_endpoint(
url = "https://xxxx.cognitiveservices.azure.com/",
service_type = "ComputerVision",
key = "xxxx")
# Add image
img_raw <- readBin("C:/Users/xxxx/Documents/Sample/trial.jpg", "raw", file.info("C:/Users/xxxx/Documents/Sample/trial.jpg")$size)
# Call the computer vision endpoint
data <- call_cognitive_endpoint(
endpoint = endp,
operation = "analyze",
body = img_raw,
encode="raw",
options = list(visualFeatures = "tags"),
http_verb = "POST")
print(data)
datatable <- data.frame(x, stringsAsFactors = TRUE)
进行了以下内容:
print(data)显示:
$tags
$tags[[1]]
$tags[[1]]$name
[1] "person"
$tags[[1]]$confidence
[1] 0.998097
$tags[[2]]
$tags[[2]]$name
[1] "indoor"
$tags[[2]]$confidence
[1] 0.9948725
$tags[[3]]
$tags[[3]]$name
[1] "toddler"
$tags[[3]]$confidence
[1] 0.9893367
$tags[[4]]
$tags[[4]]$name
[1] "human face"
$tags[[4]]$confidence
[1] 0.9281158
$tags[[5]]
$tags[[5]]$name
[1] "child"
$tags[[5]]$confidence
[1] 0.877107
$tags[[6]]
$tags[[6]]$name
[1] "boy"
$tags[[6]]$confidence
[1] 0.8087585
$tags[[7]]
$tags[[7]]$name
[1] "baby"
$tags[[7]]$confidence
[1] 0.7611696
$tags[[8]]
$tags[[8]]$name
[1] "clothing"
$tags[[8]]$confidence
[1] 0.7346113
$requestId
[1] "0deda3ab-a02c-4d81-9de7-78326eb5f593"
$metadata
$metadata$height
[1] 640
$metadata$width
[1] 640
$metadata$format
[1] "Jpeg"
数据框架显示:
我希望能够为一个文件夹中的每个10k映像运行此代码,这是“示例” “文件夹和我希望能够将结果放入数据帧表中,并将每个图像的结果分为一行。我想知道是否有人知道该怎么做?我应该创建循环吗?我将如何制作此脚本来读取我拥有的每个10K图像?
我尝试导入文件夹和图像列表,然后将img_raw更改为以下文件,但没有运气...
enter code here
# Importing folder
folder <- "C:/Users/xxxx/Documents/Sample"
# Listing files inside the folder
files <- list.files(path = folder, recursive = TRUE, pattern = "*jpg", full.names = TRUE)
enter code here
# Call the computer vision endpoint
data <- call_cognitive_endpoint(
endpoint = endp,
operation = "analyze",
body = files,
options = list(visualFeatures = "tags"),
http_verb = "POST")
print(data)
任何帮助都将不胜感激。谢谢大师!
I am currently conducting analysis of >10k images using Microsoft Azure Computer Vision and operating it in R programming software. I wanted to see what tags are present across these 10k images and would then count the most frequents tags to least frequent tags. All of these images are currently on my computer. This is not for commercial purpose and for my research study only.
The thing is, I am newbie in R and mostly used it for data visualization using ggplot.
Currently, I only managed to run analysis of 1 image at at time, which I do using the following script:
library(AzureRMR)
library(AzureCognitive)
# Create Computer Vision endpoint
endp <- cognitive_endpoint(
url = "https://xxxx.cognitiveservices.azure.com/",
service_type = "ComputerVision",
key = "xxxx")
# Add image
img_raw <- readBin("C:/Users/xxxx/Documents/Sample/trial.jpg", "raw", file.info("C:/Users/xxxx/Documents/Sample/trial.jpg")$size)
# Call the computer vision endpoint
data <- call_cognitive_endpoint(
endpoint = endp,
operation = "analyze",
body = img_raw,
encode="raw",
options = list(visualFeatures = "tags"),
http_verb = "POST")
print(data)
datatable <- data.frame(x, stringsAsFactors = TRUE)
And the results shows as follow:
print(data) shows:
$tags
$tags[[1]]
$tags[[1]]$name
[1] "person"
$tags[[1]]$confidence
[1] 0.998097
$tags[[2]]
$tags[[2]]$name
[1] "indoor"
$tags[[2]]$confidence
[1] 0.9948725
$tags[[3]]
$tags[[3]]$name
[1] "toddler"
$tags[[3]]$confidence
[1] 0.9893367
$tags[[4]]
$tags[[4]]$name
[1] "human face"
$tags[[4]]$confidence
[1] 0.9281158
$tags[[5]]
$tags[[5]]$name
[1] "child"
$tags[[5]]$confidence
[1] 0.877107
$tags[[6]]
$tags[[6]]$name
[1] "boy"
$tags[[6]]$confidence
[1] 0.8087585
$tags[[7]]
$tags[[7]]$name
[1] "baby"
$tags[[7]]$confidence
[1] 0.7611696
$tags[[8]]
$tags[[8]]$name
[1] "clothing"
$tags[[8]]$confidence
[1] 0.7346113
$requestId
[1] "0deda3ab-a02c-4d81-9de7-78326eb5f593"
$metadata
$metadata$height
[1] 640
$metadata$width
[1] 640
$metadata$format
[1] "Jpeg"
And the datatable data frame looks like follow:
I wanted to be able to run this code for each of the 10k images inside one folder, which is the "Sample" folder and I want to be able to put the results into the dataframe table with results of each image into a row. I am wondering if anyone knows how to do this? Should I create loop? How will I be able to make this script to read each of the 10k images that I have?
I have tried importing the folder and list of images, and change img_raw to files as below but no luck...
enter code here
# Importing folder
folder <- "C:/Users/xxxx/Documents/Sample"
# Listing files inside the folder
files <- list.files(path = folder, recursive = TRUE, pattern = "*jpg", full.names = TRUE)
enter code here
# Call the computer vision endpoint
data <- call_cognitive_endpoint(
endpoint = endp,
operation = "analyze",
body = files,
options = list(visualFeatures = "tags"),
http_verb = "POST")
print(data)
Any help would be much appreciated. Thank you Guru!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这种问题的模式是一种常见的模式,并使用
lapply
函数或类似方法在R中易于解决。首先,编写一个处理单个文件的函数。确保要处理的文件是函数的第一个(或唯一)参数。
现在将功能应用于文件列表。
这返回数据帧列表。要将所有数据帧结合在一起,
但您没有为您提供有关获取图像的文件的信息。
将为您提供一个索引编号以识别文件。但是更好的是修改处理函数:
因此,这
会使您作为数据框中的列本身作为列本身。
查看您的示例一行数据框架,我认为这对 tidy 将其达到长格式。
所有代码都没有测试,因为我无法访问您的C:驱动器或了解您的API密钥。
This pattern of problem is a common one and easily solved in R using the
lapply
function or similar.First, write a function that processes a single file. Ensure that the file to process is the first (or only) parameter to the function.
Now apply the function to a list of files.
This returns a list of data frames. To combine all the data frames into one you could
but this gives you no information about the file from which the image was obtained.
will give you an index number to identify the file. But better is probably to modify the processing function:
So that
gives you the file name itself as a column in the data frame.
Looking at your sample one line data frame, I think it would be helpful to tidy your data by
pivot
ing it to long format.All code untested as I don't have access to your C: drive nor knowledge of your API key.