-
Notifications
You must be signed in to change notification settings - Fork 1
/
server.R
91 lines (69 loc) · 1.87 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
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 around 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(
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.")
)
})
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$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
)
})
})