-
Notifications
You must be signed in to change notification settings - Fork 32
/
app.R
241 lines (187 loc) · 5.92 KB
/
app.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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
#### Description ###############################################################
##
## Skeleton Shiny app using miniUI
## This will look just better on mobile devices
##
#### Libraries needed #########################################################
library(shiny)
library(miniUI)
library(jpeg)
library(keras)
library(DT)
library(text2vec)
##### Initals / startup code #################################################
vgg16_notop = application_vgg16(weights = 'imagenet', include_top = FALSE)
# Read in flattened tensors, they are stored as a matrix, each image is a row
# each row corresponds to an ikea image
# normally not needed, but the matrix is split into two matrices so
# that I can stay below the github file size restriction
ImageFeatures1 = readRDS("data/ImageFeaturesVGG16_1.RDs")
ImageFeatures2 = readRDS("data/ImageFeaturesVGG16_2.RDs")
ImageFeatures = rbind(ImageFeatures1, ImageFeatures2)
# each image corresponds to a product at IKEA, this data set
# stores the link to the product at the ikea website, the price and name of the product.
ImageMetaData = readRDS("data/Allimages.RDs")
##### Addittional Helper Functions ############################################
calcIkeaSimilarity = function(x)
{
M1 <- as(matrix(x, ncol = length(x)), "dgCMatrix")
out = 1-text2vec::dist2(M1,ImageFeatures)
out
}
#### MINIPAGE #################################################################
ui <- miniPage(
gadgetTitleBar(
left = NULL,
right = NULL,
"Check Ikea First"
),
miniTabstripPanel(
#### introduction tab ############
miniTabPanel(
"introduction", icon = icon("area-chart"),
miniContentPanel(
htmlOutput("intro")
)
),
#### parameters tab ##############
miniTabPanel(
"Take_picture", icon = icon("sliders"),
miniContentPanel(
fileInput('file1', 'Choose an image (max 5MB)'),
numericInput("input_topN", "Show top N matches", value=7),
selectInput("typeselect", "help me out here", choices = list(), selected = 1),
img(SRC="IkeaNew.jpg", height = 340)
)
),
#### image tab ##################
miniTabPanel(
"Image_taken", icon = icon("file-image-o"),
miniContentPanel(
padding = 0,
imageOutput("plaatje")
)
),
#### Resultaat tab ############
# miniTabPanel(
# "Resultaat", icon = icon("table"),
# miniContentPanel(
# verbatimTextOutput("ResultaatOut")
# )
# ),
#### Tabel resultaat ###########
miniTabPanel(
"Best_Matches", icon = icon("table"),
miniContentPanel(
dataTableOutput("ResultaatTabel")
)
)
)
)
################################################################################
#### SERVER FUNCTION ###########################################################
server <- function(input, output, session) {
#### observe functions ####################
observe({
# Create a list of new options, where the name of the items is something
# like 'option label x 1', and the values are 'option-x-1'.
s_options <- as.list(c("I feel Lucky", names(table(ImageMetaData$type2))))
updateSelectInput(session, "typeselect", choices = s_options)
})
#### reactive functions ###################
ProcessImage <- reactive({
progress <- Progress$new(session, min=1, max=15)
on.exit(progress$close())
progress$set(
message = 'in progress',
detail = 'This may take a few seconds...'
)
inFile = input$file1
if(is.null(inFile)){
imgfile = "www/kast.png"
}else{
imgfile = inFile$datapath
}
img = image_load(imgfile, target_size = c(224,224))
x = image_to_array(img)
dim(x) <- c(1, dim(x))
x = imagenet_preprocess_input(x)
# extract features
features = vgg16_notop %>% predict(x)
IkeaDistance = calcIkeaSimilarity(features)
IkeaDistance
})
###### OUTPUT ELEMENTS ######################################################
#### intro ####
output$intro <- renderUI({
list(
h4("When NOT at an IKEA store, take a picture of what you see, check if Ikea has something similair and then GO TO Ikea"),
img(SRC="InstructiesIkea.PNG", height = 340)
)
})
#### plaatje ####
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/kast.png")
}
},
deleteFile = FALSE
)
#### ResultaatOut ####
output$ResultaatOut = renderPrint({
pp = ProcessImage()
print(pp)
})
#### ResultaatTabel ####
output$ResultaatTabel = renderDataTable({
simies = ProcessImage()
ImageMetaData2 = ImageMetaData
ImageMetaData2$match = as.numeric(simies)
ImageMetaData2$image = paste0(
"<img src='",
"http://www.ikea.com/nl/nl/images/products/",
ImageMetaData2$imagefile,
"'",
"height='80' width='90' </img>"
)
ImageMetaData2$link = paste0(
"<a href='",
ImageMetaData2$link,
"' target='_blank'>",
"'",
ImageMetaData2$name,
"'</a>"
)
finaltable = dplyr::arrange(ImageMetaData2,desc(match)) %>%
dplyr::slice(1:input$input_topN) %>%
dplyr::select(image, link, type2, match, price)
datatable(
options = list(
dom = 't',
autoWidth = FALSE,
columnDefs = list(list(width = '200px', targets = c(1, 3)))
),
escape = FALSE,
rownames = FALSE,
data = finaltable
) %>% formatPercentage('match', 1)
})
observeEvent(input$done, {
stopApp(TRUE)
})
}
##### SHINY APP CALL ###########################################################
shinyApp(ui, server)