這個閃亮的應用程序是從:here 它基本上使用tensorflow python在R閃亮。我的主要問題是讓py代碼在R中運行。Tensorflow classify_image
編輯:我設法通過進行一些更改來使其運行。一切都運行。然而,沒有wordcloud,我也不能得到打印在閃亮的輸出。上傳圖像後,輸出將在Rstudio的控制檯中。輸出的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)
})
例子:
pug, pug-dog (score = 0.89841) bull mastiff (score = 0.01825) Brabancon griffon (score = 0.01114) French bulldog (score = 0.00161) Pekinese, Pekingese, Peke (score = 0.00091) W c:\tf_jenkins\home\workspace\release-win\device\cpu\os\windows\tensorflow\core\framework\op_def_util.cc:332] Op BatchNormWithGlobalNormalization is deprecated. It will cease to work in GraphDef version 9. Use tf.nn.batch_normalization().
有誰知道這是怎麼回事?我嘗試了各種方法,但我不能得到的輸出打印(使用renderPrint,rendertext ...等)
你在Windows上嗎? –
是的,我很傷心:/ – Germ