-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathemotion_detector.html
104 lines (96 loc) · 4.54 KB
/
emotion_detector.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
<!DOCTYPE html>
<html lang="en">
<head>
<title>MindfulAI - Your AI therapist</title>
<link href="output.css" rel="stylesheet">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
</head>
<body>
<div class="p-20 bg-gradient-to-t from-blue-400 to-white flex flex-col justify-center items-center w-screen h-screen">
<button onclick="location.href='index.html'" class="mb-5 px-3 py-1 bg-green-500 hover:bg-green-600 rounded-lg">Back to Home</button>
<div class="flex justify-between items-center">
<div>
<div class='text-7xl font-bold flex justify-center items-center text-transparent bg-clip-text bg-gradient-to-l from-red-500 to bg-orange-300'>MindfulAI - Your AI therapist</div>
<div class="my-10">
<button class="px-2 py-1 bg-blue-400 w-40 rounded-lg border-2 border-blue-500 hover:bg-green-500 text-white text-lg" type="button" onclick="init()">Start</button>
</div>
<div id="label-container" class="text-2xl font-light text-white"></div>
</div>
<div class="h-full">
<div id="webcam-container" class="border-8 border-blue-500 rounded-lg"></div>
</div>
</div>
<script src="https://cdn.jsdelivr.net/npm/@tensorflow/tfjs@latest/dist/tf.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/@teachablemachine/image@latest/dist/teachablemachine-image.min.js"></script>
<script type="text/javascript">
const URL = "https://teachablemachine.withgoogle.com/models/0pMyTmHUr/";
let model, webcam, labelContainer, maxPredictions;
let lastSentTime = 0;
async function init() {
const modelURL = URL + "model.json";
const metadataURL = URL + "metadata.json";
model = await tmImage.load(modelURL, metadataURL);
maxPredictions = model.getTotalClasses();
const flip = true;
webcam = new tmImage.Webcam(500, 500, flip);
await webcam.setup();
await webcam.play();
window.requestAnimationFrame(loop);
document.getElementById("webcam-container").appendChild(webcam.canvas);
labelContainer = document.getElementById("label-container");
for (let i = 0; i < maxPredictions; i++) {
labelContainer.appendChild(document.createElement("div"));
}
}
async function loop() {
webcam.update();
await predict();
window.requestAnimationFrame(loop);
}
async function predict() {
const prediction = await model.predict(webcam.canvas);
let maxProbability = -1;
let mostLikelyEmotion = '';
for (let i = 0; i < maxPredictions; i++) {
const className = prediction[i].className;
const probability = prediction[i].probability;
let emotion = "";
switch (className) {
case "Class 1": emotion = "Angry"; break;
case "Class 2": emotion = "Disgust"; break;
case "Class 3": emotion = "Fear"; break;
case "Class 4": emotion = "Happy"; break;
case "Class 5": emotion = "Neutral"; break;
case "Class 6": emotion = "Sad"; break;
case "Class 7": emotion = "Surprise"; break;
}
if (probability > maxProbability) {
maxProbability = probability;
mostLikelyEmotion = emotion;
}
const classPrediction = `${emotion}: ${probability.toFixed(2)}`;
labelContainer.childNodes[i].innerHTML = classPrediction;
}
const currentTime = Date.now();
if (currentTime - lastSentTime > 5000) {
sendToServer(mostLikelyEmotion);
lastSentTime = currentTime;
}
}
async function sendToServer(emotion) {
try {
const response = await fetch(`http://127.0.0.1:8000/api/${emotion}`);
const responseData = await response.text();
var text = JSON.parse(responseData)["result"];
document.getElementById("server-output").innerHTML = "AI therapist response: Try to" + text;
} catch (error) {
console.error('There was a problem sending the data to the server:', error);
}
}
</script>
<div id="webcam-container"></div>
<div id="label-container"></div>
<div id="server-output" class="text-white text-5xl font-bold">Server Response:</div>
</div>
</body>
</html>