-
Notifications
You must be signed in to change notification settings - Fork 11
/
server.R
116 lines (87 loc) · 2.35 KB
/
server.R
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
# This is the server logic for a Shiny web application.
# You can find out more about building applications with Shiny here:
#
# http://shiny.rstudio.com
#
library(shiny)
shinyServer(function(input, output, session) {
extractedText <- reactive({
progress <- Progress$new(session, min=1, max=15)
on.exit(progress$close())
progress$set(
message = 'OCR in progress',
detail = 'This may take 5-10 sec...'
)
inFile = input$file1
if (!is.null(inFile))
{
Extext <- ocr(inFile$datapath)
}
else
{
Extext <- ocr("www/ocr-test.jpg")
}
Extext
})
output$intro <- renderUI({
list(
img(SRC="ocrplaatje.jpg"),
h4("This shiny app uses the tesseract R package to perform OCR on an uploaded image."),
h4("The extracted text is then used to form a wordcloud image, (English) stopwords can be removed"),
h4("If no image is selected a default ocr test image is used. The R source can be found on my ", a("github", href="https://github.com/longhowlam/OCRinShiny")),
h4("Cheers, Longhow")
)
})
output$plaatje <- renderImage({
inFile = input$file1
print(inFile)
if (!is.null(inFile))
{
width <- session$clientData$output_plaatje_width
height <- session$clientData$output_plaatje_height
list(
src = inFile$datapath,
width=width,
height=height
)
}
else
{
list(src="www/ocr-test.jpg")
}
},
deleteFile = FALSE
)
output$OCRtext = renderPrint({
cat(extractedText())
})
output$sentences = renderDataTable({
text = extractedText()
tmp = tokenize(text, what = "sentence")
DT::datatable(
data.frame(
sentence = 1:length(tmp[[1]]),
text = tmp[[1]]
),
rownames = FALSE
)
})
output$cloud = renderPlot({
text = extractedText()
cp = Corpus(VectorSource(text))
cp = tm_map(cp, content_transformer(tolower))
cp = tm_map(cp, removePunctuation)
if(input$stopwords){
cp = tm_map(cp, removeWords, stopwords('english'))
}
pal <- brewer.pal(9,"BuGn")
pal <- pal[-(1:4)]
wordcloud(
cp,
max.words = input$maxwords,
min.freq = input$minfreq,
random.order = FALSE,
colors = pal
)
})
})