-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.html
executable file
·118 lines (105 loc) · 4.1 KB
/
index.html
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
<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8'>
<meta http-equiv='X-UA-Compatible' content='IE=edge'>
<title>Image Classification</title>
<meta name='viewport' content='width=device-width, initial-scale=1'>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css"
integrity="sha384-Gn5384xqQ1aoWXA+058RXPxPg6fy4IWvTNh0E263XmFcJlSAwiGgFAW/dAiS6JXm" crossorigin="anonymous">
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"
integrity="sha384-KJ3o2DKtIkvYIK3UENzmM7KCkRr/rE9/Qpg6aAZGJwFDMVNA/GpGFF93hXpG5KkN"
crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"
integrity="sha384-ApNbgh9B+Y1QKtv3Rn7W3mgPxhU9K/ScQsAP7hUibX39j7fakFPskvXusvfa0b4Q"
crossorigin="anonymous"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"
integrity="sha384-JZR6Spejh4U02d8jOt6vLEHfe/JQGiRRSQQxSfFWpi1MquVdAyjUar5+76PVCmYl"
crossorigin="anonymous"></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@2.3.0/dist/tf.min.js"></script>
</head>
<style>
.upload {
opacity: 0;
}
#upload-label {
position: absolute;
top: 50%;
left: 1rem;
transform: translateY(-50%);
}
body {
min-height: 100vh;
background-color: #757f9a;
background-image: linear-gradient(147deg, #757f9a 0%, #d7dde8 100%);
}
.round {
border-radius: 35px !important;
}
#img{
width: 300px;
background-size: cover;
background-repeat: no-repeat;
}
</style>
<body>
<div class="container py-5">
<header class="text-white text-center">
<h1 class="display-4">Pizza or Not Pizza</h1>
<p class="lead mb-0 mb-3">Upload image for predicting</p>
<img id="img"/>
<h4 id="result" class="py-2"></h4>
</header>
<div class="row">
<div class="col-lg-6 mx-auto">
<div class="input-group mb-3 px-1 py-1 round bg-white shadow-sm">
<input id="file" type="file" accept="image/*" name="image" onchange="loadFile(event)" class="upload form-control border-0">
<label id="upload-label" for="upload" class="font-weight-light text-muted">Choose file</label>
</div>
</div>
</div>
</div>
</body>
<script>
var loadFile = function (event) {
var image = document.getElementById('img');
image.src = URL.createObjectURL(event.target.files[0]);
predic(image)
};
</script>
<script>
// Define a model for linear regression.
async function importModel() {
const model = await tf.loadLayersModel('./tfjsmodel/model.json');
console.log("import success")
return model;
}
async function predic(image) {
src_img = image.src
console.log("importing model")
model = await importModel();
console.log("predicting")
resized_img = tf.browser.fromPixels(document.getElementById("img")).resizeBilinear([256, 256]) //resize image to new shape using bilinear. Don't remember which stackoverflow I got form
//after transform, each array of the matrix is rgb code, then we need to normalize it
let b = tf.fill([256, 256, 3], 255) // making an array fill with 255
resized_img = tf.div(resized_img, b) // array resized_img/b, so every matrix in the array will be normallize by /255. If not, after the prediction will become 0 or 1
resized_img = tf.reshape(resized_img, [1, 256, 256, 3], 'resize') // after that we have to reshape image to 3D array again, if not it will show error that image is not 3D array
predictor = await model.predict(resized_img)
score = predictor.dataSync()[0] //score result
score = Math.ceil(score * 100 * 1000) / 1000 //just want to ceil up the third digit to make it in 3 decimal
let result = document.getElementById('result');
result.innerHTML = `<p>${pizza_notpizza(score)}</p>
<p>Pizza\'s ${score}%</p>`
}
function pizza_notpizza(value) {
let ss = value
// our model is not as good as 100% predic not pizza, so this function will help us filter pizza or not pizza
if (ss >= 60) {
return 'It\'s a pizza'
}
return 'It\'s not a pizza'
}
</script>
</html>
<html>