This repository has been archived by the owner on Mar 22, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 501
/
VisualSearchBoxHelper.js
96 lines (94 loc) · 3.8 KB
/
VisualSearchBoxHelper.js
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
({
readFile: function (component, helper, file) {
if (!file.type.match(/(image.*)/)) {
return alert('Image file not supported');
}
var reader = new FileReader();
reader.onloadend = function () {
console.log('readfile onloadend: ' + new Date());
var dataURL = reader.result;
component.set('v.pictureSrc', dataURL);
//helper.upload(component, file, dataURL.match(/,(.*)$/)[1]);
helper.simulateUpload(component, file, dataURL.match(/,(.*)$/)[1]);
};
reader.readAsDataURL(file);
},
upload: function (component, file, base64Data) {
console.log('upload: ' + new Date());
var action = component.get('c.predict');
var modelId = component.get('v.modelId');
action.setParams({
fileName: file.name,
content: base64Data,
modelId: modelId
});
action.setCallback(this, function (a) {
console.log('upload callback: ' + new Date());
component.set('v.waiting', false);
var state = a.getState();
if (state === 'ERROR') {
console.log(a.getError());
alert('An error has occurred');
}
var result = a.getReturnValue();
var predictions = [];
if (result && result.length) {
for (var i = 0; i < result.length; i++) {
predictions.push({
label: result[i].label,
formattedProbability:
'' + Math.round(result[i].probability * 100) + '%'
});
}
component.set('v.predictions', predictions);
var predictionEvent = component.getEvent('onPrediction');
predictionEvent.setParams({
predictions: result
});
predictionEvent.fire();
}
});
component.set('v.predictions', null);
component.set('v.waiting', true);
//$A.enqueueAction(action);
},
simulateUpload: function (component, file, base64Data) {
component.set('v.waiting', true);
window.setTimeout(
$A.getCallback(function () {
component.set('v.waiting', false);
var predictions;
if (file.name == 'house1.jpg') {
predictions = [
{ label: 'victorian', formattedProbability: '88.3%' },
{ label: 'colonial', formattedProbability: '11.7%' },
{ label: 'contemporary', formattedProbability: '0%' }
];
} else if (file.name == 'house2.jpg') {
predictions = [
{
label: 'contemporary',
formattedProbability: '96.7%'
},
{ label: 'colonial', formattedProbability: '3.1%' },
{ label: 'victorian', formattedProbability: '0.2%' }
];
} else {
predictions = [
{ label: 'colonial', formattedProbability: '66.2%' },
{ label: 'victorian', formattedProbability: '31.7%' },
{ label: 'victorian', formattedProbability: '2.1%' }
];
}
console.log(predictions);
component.set('v.predictions', predictions);
var predictionEvent = component.getEvent('onPrediction');
predictionEvent.setParams({
predictions: predictions
});
predictionEvent.fire();
}),
1500
);
}
});