-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathpredictor.xml
268 lines (232 loc) · 8.08 KB
/
predictor.xml
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
<?xml version="1.0" encoding="UTF-8"?>
<?xml-stylesheet
href="xsltforms-1.7/xsltforms.xsl"
type="text/xsl"?>
<?xsltforms-options
debug="no"?>
<html
xmlns="http://www.w3.org/1999/xhtml"
xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:xf="http://www.w3.org/2002/xforms"
xmlns:ev="http://www.w3.org/2001/xml-events">
<head>
<meta
charset="utf-8"/>
<meta
name="viewport"
content="width=device-width, initial-scale=1"/>
<meta charset="utf-8" />
<title>Testing mobilenet JS tensorflow</title>
<style type="text/css">
<![CDATA[
.table { display: table; border-collapse: collapse; }
.tr { display: table-row; }
.td, .th { float: left; display: table-cell; padding: 2px; border: 1px solid #555 }
.th { font-weight: bold; background-color: #aaa; }
.td, .th { height: 20px }
.title { width: 300px; height: auto }
.author { width: 350px; height: auto }
.licence { width: 200px; height: auto }
.href { width: 500px; height: auto }
.classname { width: 300px; height: auto }
.probability { width: 500px; height: auto }
xforms-input[xf-readonly] > xforms-body > input { color: grey; }
xforms-body > img { border : 5px solid black; height: 224px; object-fit: contain }
#r_images > xforms-repeat-item[xf-selected='true'] { background-color : #ddd }
.grid-container { display: grid; grid-template-columns: auto auto ; }
.grid-item { }
/]]>
</style>
<link
rel="stylesheet"
href="https://fonts.googleapis.com/css2?family=Material+Symbols+Outlined:opsz,wght,FILL,GRAD@24,400,0,0" />
<script
src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js"
type="text/javascript">
</script>
<script
src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet@1.0.0"
type="text/javascript">
</script>
<script type="text/javascript">
<![CDATA[
let xforms_model;
let tf_model;
function dispatch(event_str) {
XsltForms_xmlevents.dispatch(xforms_model, event_str);
}
function load_model() {
xforms_model=document.querySelector('#tensorflow');
dispatch('tensorflow-model-loading');
mobilenet.load().then(
m => { tf_model=m;
dispatch('tensorflow-model-loaded');
}
)
.catch(
err => { console.error(err); dispatch('tensorflow-load-error'); }
)
}
function infer() {
var img=document.querySelector('#img_1 > xforms-body > img');
tf_model.classify(img).then(
predicts => {
console.log('Predictions: ');
console.log(predicts);
dispatch('tensorflow-clear-predictions');
const inst=xforms_model.getInstanceDocument('shared').querySelector('prediction');
for (var i = 0; i < predicts.length; i++) {
inst.setAttribute('classname', predicts[i].className);
inst.setAttribute('probability',predicts[i].probability);
dispatch('tensorflow-prediction-entry');
}
dispatch('tensorflow-predictions-complete');
}
)
.catch(
err => { console.error(err); dispatch('tensorflow-prediction-error'); }
)
}
]]>
</script>
<xf:model id="default"/>
<xf:model id="tensorflow">
<xf:instance id="images" src="images/images.xml"/>
<xf:instance id="shared"> <!-- acts as a buffer between JS and XForms -->
<shared xmlns="">
<prediction classname="" probability=""/>
</shared>
</xf:instance>
<xf:instance id="predictions">
<data xmlns="">
<predictions>
<prediction classname="" probability=""/>
</predictions>
</data>
</xf:instance>
<xf:instance id="message">
<data xmlns="">
<message/>
</data>
</xf:instance>
<xf:action ev:event="xforms-ready">
<xf:setvalue ref="instance('message')/message">Click button to load TensorFlow.js model</xf:setvalue>
</xf:action>
<xf:action ev:event='tensorflow-model-loading'>
<xf:setvalue ref="instance('message')/message">Loading TensorFlow.js model</xf:setvalue>
</xf:action>
<xf:action ev:event="tensorflow-model-loaded">
<xf:toggle case="model_loaded"/>
<xf:setvalue ref="instance('message')/message">Loaded TensorFlow.js model: Select Image and click button to run inference</xf:setvalue>
<xf:toggle case="show_predictions"/>
</xf:action>
<xf:action ev:event="tensorflow-load-error">
<xf:toggle case="model_not_loaded"/>
<xf:setvalue ref="instance('message')/message">Failed to load TensorFlow.js model</xf:setvalue>
</xf:action>
<xf:action ev:event="tensorflow-prediction-error">
<xf:setvalue ref="instance('message')/message">Inference failed: see console</xf:setvalue>
</xf:action>
<xf:action ev:event="tensorflow-clear-predictions">
<xf:delete
ref="instance('predictions')/predictions/prediction[position() > 1]"/>
</xf:action>
<xf:action ev:event="tensorflow-predictions-complete">
<xf:setvalue ref="instance('message')/message">Select Image and click button to run inference</xf:setvalue>
</xf:action>
<xf:action ev:event="tensorflow-prediction-entry">
<xf:insert
ref="instance('predictions')/predictions/prediction"
origin="instance('shared')/prediction"/>
</xf:action>
</xf:model>
</head>
<body>
<xf:group model="tensorflow">
<div float="left">
<xf:switch>
<xf:case id="model_not_loaded">
<xf:trigger id="t_load_model">
<xf:label><span class="material-symbols-outlined"> download </span></xf:label>
<xf:hint>Load Tensorflow Model</xf:hint>
<xf:action ev:event="DOMActivate">
<xf:toggle case="model_loading"/>
<xf:load resource="javascript:load_model()"/>
</xf:action>
</xf:trigger>
</xf:case>
<xf:case id="model_loading">
<xf:trigger id="t_loading">
<xf:label><span class="material-symbols-outlined"> hourglass </span></xf:label>
<xf:hint>Tensorflow Model Loading</xf:hint>
</xf:trigger>
</xf:case>
<xf:case id="model_loaded">
<xf:trigger id="t_infer">
<xf:hint>Run Tensorflow Model Inference</xf:hint>
<xf:label><span class="material-symbols-outlined"> smart_toy </span></xf:label>
<xf:action ev:event="DOMActivate">
<xf:load resource="javascript:infer()"/>
</xf:action>
</xf:trigger>
</xf:case>
</xf:switch>
</div>
<div>
<xf:output ref="instance('message')/message"/>
</div>
<div>
<xf:switch>
<xf:case id="hide_predictions"/>
<xf:case id="show_predictions">
<div class="table">
<div class="tr">
<div class="th classname">classname</div>
<div class="th probability">probability</div>
</div>
<xf:repeat id="r_predictions" ref="instance('predictions')/predictions/prediction[position() > 1]">
<div class="tr">
<div class="td classname"><xf:output ref="@classname"/></div>
<div class="td probability"><xf:output ref="@probability"/></div>
</div>
</xf:repeat>
</div>
</xf:case>
</xf:switch>
</div>
<br/>
<div class="grid-container">
<div class="grid-item">
<div class="table">
<div class="tr">
<div class="th title">title</div>
<div class="th author">author</div>
</div>
<xf:repeat id="r_images" ref="instance('images')/img">
<div class="tr">
<div class="td title"><xf:output value="@title"/></div>
<div class="td author"><xf:output value="@author"/></div>
</div>
</xf:repeat>
</div>
</div>
<div class="grid-item">
<xf:output
id="img_1"
ref="instance('images')/img[index('r_images')]/@src"
mediatype="image/*"/>
</div>
</div>
<div class="table">
<div class="tr">
<div class="th href">href</div>
<div class="th licence">licence</div>
</div>
<div class="tr">
<div class="td href"><xf:output ref="instance('images')/img[index('r_images')]/@href"/></div>
<div class="td licence"><xf:output ref="instance('images')/img[index('r_images')]/@licence"/></div>
</div>
</div>
</xf:group>
</body>
</html>