【发布时间】:2017-02-20 09:01:38
【问题描述】:
这个闪亮的应用程序来自:here 它基本上在 R 闪亮中使用 tensorflow python。我的主要问题是让 py 代码在 R 中运行。
编辑:我设法通过进行一些更改使其运行。一切运行。但是,没有 wordcloud,我也无法将输出打印在闪亮上。上传图片后,输出将在 Rstudio 的控制台中。
library(wordcloud)
shinyServer(function(input, output) {
PYTHONPATH <- "C:/Program Files/Anaconda3" #should look like /Users/yourname/anaconda/bin if you use anaconda python distribution in OS X
CLASSIFYIMAGEPATH <- "C:/Program Files/Anaconda3/Lib/site-packages/tensorflow/models/image/imagenet" #should look like ~/anaconda/lib/python2.7/site-packages/tensorflow/models/image/imagenet
outputtext <- reactive({
###This is to compose image recognition template###
inFile <- input$file1 #This creates input button that enables image upload
template <- paste0("python"," ", "classify_image.py") #Template to run image recognition using Python
if (is.null(inFile))
{system(paste0(template," --image_file /tmp/imagenet/cropped_panda.jpg"))} else { #Initially the app classifies cropped_panda.jpg, if you download the model data to a different directory, you should change /tmp/imagenet to the location you use.
system(paste0(template," --image_file ",inFile$datapath)) #Uploaded image will be used for classification
}
})
output$answer <- renderPrint({outputtext()})
output$plot <- renderPlot({
###This is to create wordcloud based on image recognition results###
df <- data.frame(gsub(" *\\(.*?\\) *", "", outputtext()),gsub("[^0-9.]", "", outputtext())) #Make a dataframe using detected objects and scores
names(df) <- c("Object","Score") #Set column names
df$Object <- as.character(df$Object) #Convert df$Object to character
df$Score <- as.numeric(as.character(df$Score)) #Convert df$Score to numeric
s <- strsplit(as.character(df$Object), ',') #Split rows by comma to separate rows
df <- data.frame(Object=unlist(s), Score=rep(df$Score, sapply(s, FUN=length))) #Allocate scores to split words
# By separating long categories into shorter terms, we can avoid "could not be fit on page. It will not be plotted" warning as much as possible
wordcloud(df$Object, df$Score, scale=c(4,2),
colors=brewer.pal(6, "RdBu"),random.order=F) #Make wordcloud
})
output$outputImage <- renderImage({
###This is to plot uploaded image###
if (is.null(input$file1)){
outfile <- "/tmp/imagenet/cropped_panda.jpg"
contentType <- "image/jpg"
#Panda image is the default
}else{
outfile <- input$file1$datapath
contentType <- input$file1$type
#Uploaded file otherwise
}
list(src = outfile,
contentType=contentType,
width=300)
}, deleteFile = TRUE)
})
Rstudio 控制台上的输出示例:
哈巴狗,哈巴狗(得分 = 0.89841)公牛獒(得分 = 0.01825) Brabancon griffon (分数 = 0.01114) 法国斗牛犬 (分数 = 0.00161) 北京人、北京人、北京人(分数 = 0.00091)W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_def_util.cc:332] 操作 BatchNormWithGlobalNormalization 已弃用。它将停止 在 GraphDef 版本 9 中工作。使用 tf.nn.batch_normalization()。
有谁知道发生了什么?我尝试了各种方法,但我无法打印输出(使用了 renderPrint、rendertext... 等)
【问题讨论】:
-
你在 Windows 上吗?
-
是的,我很遗憾:/