-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathscript.js
189 lines (163 loc) · 5.92 KB
/
script.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
const video = document.getElementById('video');
const canvas = document.getElementById('canvas');
const ctx = canvas.getContext('2d');
let model;
let emotion = 'neutral'; // Start with neutral emotion
async function setupCamera() {
const stream = await navigator.mediaDevices.getUserMedia({ video: true });
video.srcObject = stream;
await new Promise(resolve => video.onloadedmetadata = resolve);
video.play();
}
async function setupModel() {
model = await facemesh.load(); // load facemesh model
}
async function initialize() {
await setupCamera();
await setupModel();
setInterval(analyzeEmotion, 1);
}
initialize();
async function analyzeEmotion() {
const predictions = await model.estimateFaces(video, false);
if (predictions.length > 0) {
emotion = detectEmotion(predictions[0]);
updateBackgroundColor(emotion);
displayMessage(emotion);
drawFaceMesh(predictions[0]);
}
}
function detectEmotion(face) {
const mesh = face.scaledMesh;
if (!mesh || mesh.length < 300) {
console.error('Mesh or mesh length is insufficient:', mesh);
return 'neutral'; // if no mesh or mesh is too short, return 'neutral'
}
// Indices for facial features
const leftEyeIndices = [33, 30, 35, 34]; // Left eye
const rightEyeIndices = [263, 260, 265, 264]; // Right eye
const leftEyebrowIndices = [70, 63, 105, 66]; // Left eyebrow
const rightEyebrowIndices = [296, 293, 334, 300]; // Right eyebrow
const mouthIndices = [78, 82, 86, 88]; // Mouth corners
// Get features
const leftEye = leftEyeIndices.map(index => mesh[index] || [0, 0]);
const rightEye = rightEyeIndices.map(index => mesh[index] || [0, 0]);
const leftEyebrow = leftEyebrowIndices.map(index => mesh[index] || [0, 0]);
const rightEyebrow = rightEyebrowIndices.map(index => mesh[index] || [0, 0]);
const mouth = mouthIndices.map(index => mesh[index] || [0, 0]);
// Feature extraction
const leftEyeHeight = Math.abs(leftEye[1][1] - leftEye[3][1]);
const rightEyeHeight = Math.abs(rightEye[1][1] - rightEye[3][1]);
const mouthOpen = Math.abs(mouth[1][1] - mouth[3][1]);
const mouthWidth = Math.sqrt(
Math.pow(mouth[1][0] - mouth[3][0], 2) +
Math.pow(mouth[1][1] - mouth[3][1], 2)
);
const leftEyebrowHeight = Math.abs(leftEyebrow[0][1] - leftEyebrow[2][1]);
const rightEyebrowHeight = Math.abs(rightEyebrow[0][1] - rightEyebrow[2][1]);
// Distance between eyes
const eyeDistance = Math.sqrt(
Math.pow(leftEye[0][0] - rightEye[0][0], 2) +
Math.pow(leftEye[0][1] - rightEye[0][1], 2)
);
// Threshold values for parameters
const eyeHeightThreshold = 10; // Eye height threshold
const mouthOpenThreshold = 5; // Mouth open threshold
const mouthWidthThreshold = 15; // Mouth width threshold
const eyebrowHeightThreshold = 12; // Eyebrow height threshold
const eyeDistanceThreshold = 55; // Eye distance threshold
// Perception by emotions
if (leftEyeHeight > eyeHeightThreshold && rightEyeHeight > eyeHeightThreshold) {
return 'angry'; // Angry
} else if (mouthOpen > mouthOpenThreshold && mouthWidth > mouthWidthThreshold &&
leftEyebrowHeight < eyebrowHeightThreshold && rightEyebrowHeight < eyebrowHeightThreshold) {
return 'happy'; // Happy
} else if (mouthOpen < 4 && leftEyebrowHeight < eyebrowHeightThreshold &&
rightEyebrowHeight < eyebrowHeightThreshold) {
return 'sad'; // Sad
} else if (eyeDistance > eyeDistanceThreshold) {
return 'surprised'; // Suprised
} else {
return 'neutral'; // Neutral
}
}
function updateBackgroundColor(emotion) {
let color;
switch (emotion) {
case 'happy':
color = 'green';
break;
case 'angry':
color = 'red';
break;
case 'sad':
color = 'lightblue';
break;
default:
color = 'black';
break;
}
document.body.style.transition = 'background-color 1s ease';
document.body.style.backgroundColor = color;
}
function displayMessage(emotion) {
const messageElement = document.getElementById('message');
let message;
switch (emotion) {
case 'happy':
message = 'You look happy! Feel free to explore.';
break;
case 'angry':
message = 'You seem angry. Be careful, you might break something!';
break;
case 'sad':
message = 'You look sad. Maybe try a happy button to cheer up!';
break;
default:
message = '';
break;
}
messageElement.textContent = message;
}
function drawFaceMesh(face) {
const mesh = face.scaledMesh;
if (!mesh || mesh.length < 300) {
console.error('Mesh or mesh length is insufficient:', mesh);
return;
}
canvas.width = window.innerWidth;
canvas.height = window.innerHeight;
ctx.clearRect(0, 0, canvas.width, canvas.height);
// To draw the face mesh
ctx.strokeStyle = 'black';
ctx.lineWidth = 2;
// To draw the facial features
const facialFeatures = [
[30, 30, 35, 34], // Left eye
[263, 264, 265, 264], // Right eye
[70, 63, 105, 66], // Left eyebrow
[296, 293, 334, 300], // Right eyebrow
[78, 82, 86, 88], // Mouth corners
];
facialFeatures.forEach(feature => {
ctx.beginPath();
feature.forEach((index, idx) => {
const [x, y] = mesh[index];
if (idx === 0) {
ctx.moveTo(x, y);
} else {
ctx.lineTo(x, y);
}
});
ctx.closePath();
ctx.stroke();
});
// Plotting the coordinates of each point
ctx.fillStyle = 'blue';
mesh.forEach(point => {
const [x, y] = point;
ctx.beginPath();
ctx.arc(x, y, 3, 0, 2 * Math.PI);
ctx.fill();
});
}