Python 张量流图像
此闪亮应用程序由以下内容构建: 它基本上在R中使用tensorflow python。我的主要问题是让py代码在R中运行 编辑:我通过做一些更改使其运行。一切正常。但是,没有wordcloud,我也无法将输出打印在shiny上。上传图像后,输出将在Rstudio的控制台中Python 张量流图像,python,r,shiny,Python,R,Shiny,此闪亮应用程序由以下内容构建: 它基本上在R中使用tensorflow python。我的主要问题是让py代码在R中运行 编辑:我通过做一些更改使其运行。一切正常。但是,没有wordcloud,我也无法将输出打印在shiny上。上传图像后,输出将在Rstudio的控制台中 library(wordcloud) shinyServer(function(input, output) { PYTHONPATH <- "C:/Program Files/Anaconda3" #shou
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)
})
库(wordcloud)
shinyServer(功能(输入、输出){
PYTHONPATH好的,这里有两种方法-
1
a、 安装适用于windows的RPython[Linux/Mac from CRAN]。作为zip下载[Clone/Download as zip option]-
b、 解压缩它,将文件夹重命名为rPython,更改configure.win以指向您的python安装[我猜它一定是蟒蛇]
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/')
#tensorflow调用者
图书馆(“rPython”)
py_代码你在Windows上吗?是的,我很难过:/Hi vivek,我如何更改config.win。我终止了这个错误编译。make:**[pycall.o]错误1警告:运行命令'make-f“Makevars.win”-f“D:/R-3.3.2/etc/x64/Makeconf”-D:/R-3.3.2/share/make/winshlib.mk“SHLIB=”rPython.dll“win=64 TCLBIN=64 OBJECTS=”pycall.o“had status 2 ERROR:compilation failed for package'rPython'*removing'D:/R-3.3.2/library/rPython'ERROR:Command failed(1)将echo'PKG_LIBS=-LC:/python27/LIBS-lpython27'>src/makevars.win echo'PKG_CFLAGS=-I“C:/python27/include”>>src/makevars.win
更改为安装python的位置