2017-02-20 101 views
0

這個閃亮的應用程序是從: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 ...等)

+0

你在Windows上嗎? –

+0

是的,我很傷心:/ – Germ

回答

0

確定這裏有兩種方式 -

一個。安裝RPython for windows [來自CRAN的Linux/Mac]。下載爲zip [克隆/下載爲zip選項] - https://github.com/cjgb/rPython-win

b。解壓縮,重命名文件夾爲rPython,將configure.win更改爲指向您的python安裝[它肯定是我猜的那個Anaconda]

c。 R中執行的代碼 -

library(devtools) # devtools needs to be installed - install.packages("devtools") 
install("C:/Users/username/Downloads/rPython") # location where you have downloaded rPython 

我的輸出 -

Installing rPython 
"C:/PROGRA~1/R/R-33~1.2/bin/x64/R" --no-site-file --no-environ --no-save --no-restore --quiet CMD \ 
    INSTALL "C:/Users/vk046010/Downloads/rPython" \ 
    --library="C:/Users/vk046010/Documents/R/win-library/3.3" --install-tests 

* installing *source* package 'rPython' ... 
** libs 
Warning: this package has a non-empty 'configure.win' file, 
so building only the main architecture 

c:/Rtools/mingw_64/bin/gcc -I"C:/PROGRA~1/R/R-33~1.2/include" -DNDEBUG  -I"d:/Compiler/gcc-4.9.3/local330/include" -I"C:/Anaconda2/include" -O2 -Wall -std=gnu99 -mtune=core2 -c pycall.c -o pycall.o 
c:/Rtools/mingw_64/bin/gcc -shared -s -static-libgcc -o rPython.dll tmp.def pycall.o -LC:/Anaconda2/libs -lpython27 -Ld:/Compiler/gcc-4.9.3/local330/lib/x64 -Ld:/Compiler/gcc-4.9.3/local330/lib -LC:/PROGRA~1/R/R-33~1.2/bin/x64 -lR 
installing to C:/Users/vk046010/Documents/R/win-library/3.3/rPython/libs/x64 
** R 
** inst 
** preparing package for lazy loading 
** help 
*** installing help indices 
** building package indices 
** testing if installed package can be loaded 
* DONE (rPython) 

d。現在執行此從RStudio -

# tensorflow caller 
library("rPython") 


py_code <- " 
def classify_image(image_loc): 
    # load tensorflow and predict here 
    return image_loc+'person' # return the result here 
" 
python.exec(py_code) 

python.call("classify_image", 'path/loc/to/image/') 

python.call的輸出應該是你正在尋找的東西。

我沒有以前那麼沒有個人擔保做到了這一點。你可以在R本身安裝張量流 - https://github.com/rstudio/tensorflow

+0

嗨vivek,我怎麼去改變config.win。我得到了這個錯誤編譯終止。 make:*** [pycall.o]錯誤1 警告:運行命令'make -f「Makevars.win」-f「D:/R-3.3.2/etc/x64/Makeconf」-f「D: /R-3.3.2/share/make/winshlib.mk「SHLIB =」rPython.dll「WIN = 64 TCLBIN = 64 OBJECTS =」pycall.o「'有狀態2 錯誤:編譯包'rPython'失敗 *刪除'D:/R-3.3.2/library/rPython' 錯誤:命令失敗(1) – Germ

+0

更改'echo'PKG_LIBS = -LC:/ python27/libs -lpython27'> src/makevars.win echo' PKG_CFLAGS = -I「C:/ Python27/include」'>> src/makevars.win'到python安裝的位置 –