|
a |
|
b/static/js/main.js |
|
|
1 |
$(document).ready(function () { |
|
|
2 |
// Init |
|
|
3 |
$('.image-section').hide(); |
|
|
4 |
$('.loader').hide(); |
|
|
5 |
$('#result').hide(); |
|
|
6 |
|
|
|
7 |
// Upload Preview |
|
|
8 |
function readURL(input) { |
|
|
9 |
if (input.files && input.files[0]) { |
|
|
10 |
var reader = new FileReader(); |
|
|
11 |
reader.onload = function (e) { |
|
|
12 |
$('#imagePreview').css('background-image', 'url(' + e.target.result + ')'); |
|
|
13 |
$('#imagePreview').hide(); |
|
|
14 |
$('#imagePreview').fadeIn(650); |
|
|
15 |
} |
|
|
16 |
reader.readAsDataURL(input.files[0]); |
|
|
17 |
} |
|
|
18 |
} |
|
|
19 |
$("#imageUpload").change(function () { |
|
|
20 |
$('.image-section').show(); |
|
|
21 |
$('#btn-predict').show(); |
|
|
22 |
$('#result').text(''); |
|
|
23 |
$('#result').hide(); |
|
|
24 |
readURL(this); |
|
|
25 |
}); |
|
|
26 |
|
|
|
27 |
// Predict |
|
|
28 |
$('#btn-predict').click(function () { |
|
|
29 |
var form_data = new FormData($('#upload-file')[0]); |
|
|
30 |
|
|
|
31 |
// Show loading animation |
|
|
32 |
$(this).hide(); |
|
|
33 |
$('.loader').show(); |
|
|
34 |
|
|
|
35 |
// Make prediction by calling api /predict |
|
|
36 |
$.ajax({ |
|
|
37 |
type: 'POST', |
|
|
38 |
url: '/predict', |
|
|
39 |
data: form_data, |
|
|
40 |
contentType: false, |
|
|
41 |
cache: false, |
|
|
42 |
processData: false, |
|
|
43 |
async: true, |
|
|
44 |
success: function (data) { |
|
|
45 |
// Get and display the result |
|
|
46 |
$('.loader').hide(); |
|
|
47 |
$('#result').fadeIn(600); |
|
|
48 |
$('#result').text(' Result: ' + data); |
|
|
49 |
console.log('Success!'); |
|
|
50 |
}, |
|
|
51 |
}); |
|
|
52 |
}); |
|
|
53 |
|
|
|
54 |
}); |