-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
61 lines (53 loc) · 2.1 KB
/
index.html
File metadata and controls
61 lines (53 loc) · 2.1 KB
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
<!DOCTYPE html>
<html lang="de">
<head>
<meta charset="UTF-8">
<title>TensorFlow.js Beispiel</title>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@4.18.0/dist/tf.min.js"></script> <!-- TensorFlow.js -->
<script src="https://cdn.jsdelivr.net/npm/@tensorflow-models/mobilenet"></script> <!-- Vorgefertigtes Modell -->
<style>
body {
font-family: Arial, sans-serif;
text-align: center;
margin-top: 50px;
}
img {
max-width: 300px;
}
</style>
</head>
<body>
<h1>TensorFlow.js - Bild Klassifikation</h1>
<input type="file" id="upload" accept="image/*">
<br><br>
<img id="image" src="" alt="Bild wird hier angezeigt" style="display:none;">
<h2 id="ergebnis"></h2>
<script>
let net;
async function app() {
console.log('Modell wird geladen...');
net = await mobilenet.load(); // Vorgefertigtes Mobilenet-Modell laden
console.log('Modell geladen.');
const upload = document.getElementById('upload');
const imgElement = document.getElementById('image');
const ergebnisElement = document.getElementById('ergebnis');
upload.addEventListener('change', async (event) => {
const file = event.target.files[0];
const reader = new FileReader();
reader.onload = function(e) {
imgElement.src = e.target.result;
imgElement.style.display = 'block';
};
reader.readAsDataURL(file);
reader.onloadend = async function() {
await new Promise(resolve => setTimeout(resolve, 100)); // Kleiner Trick, damit das Bild vollständig geladen ist
const result = await net.classify(imgElement);
console.log(result);
ergebnisElement.innerText = `Erkannt: ${result[0].className} (${(result[0].probability * 100).toFixed(2)}%)`;
};
});
}
app();
</script>
</body>
</html>