Bahrudin commited on
Commit
a2b8e1d
·
verified ·
1 Parent(s): 0cca4bf

make more compact. Video in background, buttons overlaid. no emojies - Initial Deployment

Browse files
Files changed (2) hide show
  1. README.md +6 -4
  2. index.html +307 -19
README.md CHANGED
@@ -1,10 +1,12 @@
1
  ---
2
- title: Mood
3
- emoji:
4
- colorFrom: red
5
  colorTo: gray
6
  sdk: static
7
  pinned: false
 
 
8
  ---
9
 
10
- Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
 
1
  ---
2
+ title: mood
3
+ emoji: 🐳
4
+ colorFrom: green
5
  colorTo: gray
6
  sdk: static
7
  pinned: false
8
+ tags:
9
+ - deepsite
10
  ---
11
 
12
+ Check out the configuration reference at https://huggingface.co/docs/hub/spaces-config-reference
index.html CHANGED
@@ -1,19 +1,307 @@
1
- <!doctype html>
2
- <html>
3
- <head>
4
- <meta charset="utf-8" />
5
- <meta name="viewport" content="width=device-width" />
6
- <title>My static Space</title>
7
- <link rel="stylesheet" href="style.css" />
8
- </head>
9
- <body>
10
- <div class="card">
11
- <h1>Welcome to your static Space!</h1>
12
- <p>You can modify this app directly by editing <i>index.html</i> in the Files and versions tab.</p>
13
- <p>
14
- Also don't forget to check the
15
- <a href="https://huggingface.co/docs/hub/spaces" target="_blank">Spaces documentation</a>.
16
- </p>
17
- </div>
18
- </body>
19
- </html>
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
+ <!DOCTYPE html>
2
+ <html lang="en">
3
+ <head>
4
+ <meta charset="UTF-8">
5
+ <meta name="viewport" content="width=device-width, initial-scale=1.0">
6
+ <title>Mood Detector</title>
7
+ <script src="https://cdn.tailwindcss.com"></script>
8
+ <script src="https://cdn.jsdelivr.net/npm/[email protected]/dist/face-api.min.js"></script>
9
+ <style>
10
+ .video-overlay {
11
+ position: absolute;
12
+ top: 0;
13
+ left: 0;
14
+ width: 100%;
15
+ height: 100%;
16
+ }
17
+ .face-box {
18
+ position: absolute;
19
+ border: 2px solid #3B82F6;
20
+ background-color: rgba(59, 130, 246, 0.2);
21
+ }
22
+ .mood-text {
23
+ position: absolute;
24
+ color: white;
25
+ background-color: rgba(0, 0, 0, 0.7);
26
+ padding: 2px 5px;
27
+ border-radius: 4px;
28
+ font-size: 12px;
29
+ }
30
+ .pulse {
31
+ animation: pulse 2s infinite;
32
+ }
33
+ @keyframes pulse {
34
+ 0% {
35
+ transform: scale(1);
36
+ box-shadow: 0 0 0 0 rgba(59, 130, 246, 0.7);
37
+ }
38
+ 70% {
39
+ transform: scale(1.05);
40
+ box-shadow: 0 0 0 10px rgba(59, 130, 246, 0);
41
+ }
42
+ 100% {
43
+ transform: scale(1);
44
+ box-shadow: 0 0 0 0 rgba(59, 130, 246, 0);
45
+ }
46
+ }
47
+ </style>
48
+ </head>
49
+ <body class="bg-gray-900 text-white min-h-screen flex flex-col">
50
+ <header class="bg-gray-800 py-6 shadow-lg">
51
+ <div class="container mx-auto px-4">
52
+ <h1 class="text-3xl md:text-4xl font-bold text-center text-blue-400">Mood Detector</h1>
53
+ <p class="text-center text-gray-300 mt-2">Let your face reveal your emotions</p>
54
+ </div>
55
+ </header>
56
+
57
+ <main class="relative h-screen w-full overflow-hidden">
58
+ <div class="absolute inset-0">
59
+ <video id="video" autoplay muted class="w-full h-full object-cover bg-gray-800"></video>
60
+ <canvas id="canvas" class="absolute inset-0 w-full h-full"></canvas>
61
+ </div>
62
+
63
+ <div class="absolute bottom-4 left-0 right-0 flex justify-center gap-4">
64
+ <button id="start-btn" class="bg-blue-600 hover:bg-blue-700 text-white font-bold py-2 px-4 rounded-lg transition duration-300">
65
+ Start
66
+ </button>
67
+ <button id="stop-btn" class="bg-gray-700 hover:bg-gray-600 text-white font-bold py-2 px-4 rounded-lg transition duration-300" disabled>
68
+ Stop
69
+ </button>
70
+ </div>
71
+
72
+ <div class="absolute top-4 right-4 bg-gray-800 bg-opacity-80 rounded-lg p-4 shadow-lg">
73
+ <h2 class="text-lg font-semibold mb-2">Mood Analysis</h2>
74
+ <div id="mood-text" class="text-xl font-bold text-blue-400">Neutral</div>
75
+ <div id="confidence" class="text-gray-300 text-sm">Confidence: 0%</div>
76
+
77
+ <div class="mt-4 grid grid-cols-2 gap-2">
78
+ <div>
79
+ <div class="font-medium">Happy</div>
80
+ <div id="happy-score" class="text-gray-300 text-sm">0%</div>
81
+ </div>
82
+ <div>
83
+ <div class="font-medium">Sad</div>
84
+ <div id="sad-score" class="text-gray-300 text-sm">0%</div>
85
+ </div>
86
+ <div>
87
+ <div class="font-medium">Angry</div>
88
+ <div id="angry-score" class="text-gray-300 text-sm">0%</div>
89
+ </div>
90
+ <div>
91
+ <div class="font-medium">Fearful</div>
92
+ <div id="fearful-score" class="text-gray-300 text-sm">0%</div>
93
+ </div>
94
+ </div>
95
+ </div>
96
+ </main>
97
+
98
+ <footer class="bg-gray-800 py-4">
99
+ <div class="container mx-auto px-4 text-center text-gray-400">
100
+ <p>Mood Detector using Face API | © 2023 All Rights Reserved</p>
101
+ </div>
102
+ </footer>
103
+
104
+ <script>
105
+ // DOM Elements
106
+ const video = document.getElementById('video');
107
+ const canvas = document.getElementById('canvas');
108
+ const startBtn = document.getElementById('start-btn');
109
+ const stopBtn = document.getElementById('stop-btn');
110
+ const moodDisplay = document.getElementById('mood-display');
111
+ const moodText = document.getElementById('mood-text');
112
+ const confidenceText = document.getElementById('confidence');
113
+
114
+ // Emotion score elements
115
+ const happyScore = document.getElementById('happy-score');
116
+ const sadScore = document.getElementById('sad-score');
117
+ const angryScore = document.getElementById('angry-score');
118
+ const fearfulScore = document.getElementById('fearful-score');
119
+
120
+ let detectionInterval;
121
+ let isDetecting = false;
122
+
123
+ // Load models
124
+ async function loadModels() {
125
+ try {
126
+ await Promise.all([
127
+ faceapi.nets.tinyFaceDetector.loadFromUri('https://justadudewhohacks.github.io/face-api.js/models'),
128
+ faceapi.nets.faceLandmark68Net.loadFromUri('https://justadudewhohacks.github.io/face-api.js/models'),
129
+ faceapi.nets.faceRecognitionNet.loadFromUri('https://justadudewhohacks.github.io/face-api.js/models'),
130
+ faceapi.nets.faceExpressionNet.loadFromUri('https://justadudewhohacks.github.io/face-api.js/models')
131
+ ]);
132
+ console.log('Models loaded successfully');
133
+ } catch (err) {
134
+ console.error('Failed to load models:', err);
135
+ alert('Failed to load face detection models. Please try again later.');
136
+ }
137
+ }
138
+
139
+ // Start video stream
140
+ async function startVideo() {
141
+ try {
142
+ const stream = await navigator.mediaDevices.getUserMedia({ video: {} });
143
+ video.srcObject = stream;
144
+ return true;
145
+ } catch (err) {
146
+ console.error('Error accessing camera:', err);
147
+ alert('Could not access the camera. Please ensure you have granted camera permissions.');
148
+ return false;
149
+ }
150
+ }
151
+
152
+ // Detect faces and emotions
153
+ async function detectFaces() {
154
+ if (!isDetecting) return;
155
+
156
+ const options = new faceapi.TinyFaceDetectorOptions();
157
+ const result = await faceapi.detectAllFaces(video, options)
158
+ .withFaceLandmarks()
159
+ .withFaceExpressions();
160
+
161
+ // Clear canvas
162
+ const context = canvas.getContext('2d');
163
+ context.clearRect(0, 0, canvas.width, canvas.height);
164
+
165
+ // Resize canvas to match video dimensions
166
+ canvas.width = video.videoWidth;
167
+ canvas.height = video.videoHeight;
168
+
169
+ // Draw detections
170
+ faceapi.draw.drawDetections(canvas, result);
171
+ faceapi.draw.drawFaceLandmarks(canvas, result);
172
+ faceapi.draw.drawFaceExpressions(canvas, result);
173
+
174
+ // Update mood display if face detected
175
+ if (result.length > 0) {
176
+ const expressions = result[0].expressions;
177
+ updateMoodDisplay(expressions);
178
+ } else {
179
+ resetMoodDisplay();
180
+ }
181
+ }
182
+
183
+ // Update mood display based on expressions
184
+ function updateMoodDisplay(expressions) {
185
+ // Get the dominant emotion
186
+ const emotions = Object.entries(expressions);
187
+ const dominantEmotion = emotions.reduce((max, emotion) =>
188
+ emotion[1] > max[1] ? emotion : max
189
+ );
190
+
191
+ const [emotion, confidence] = dominantEmotion;
192
+ const confidencePercent = Math.round(confidence * 100);
193
+
194
+ // Update scores
195
+ happyScore.textContent = `${Math.round(expressions.happy * 100)}%`;
196
+ sadScore.textContent = `${Math.round(expressions.sad * 100)}%`;
197
+ angryScore.textContent = `${Math.round(expressions.angry * 100)}%`;
198
+ fearfulScore.textContent = `${Math.round(expressions.fearful * 100)}%`;
199
+
200
+ // Update main display
201
+ moodText.textContent = capitalizeFirstLetter(emotion);
202
+ confidenceText.textContent = `Confidence: ${confidencePercent}%`;
203
+
204
+ // Update mood indicator
205
+ updateMoodIndicator(emotion, confidencePercent);
206
+ }
207
+
208
+ // Reset mood display when no face is detected
209
+ function resetMoodDisplay() {
210
+ moodText.textContent = "No face detected";
211
+ confidenceText.textContent = "Confidence: 0%";
212
+
213
+ // Reset scores
214
+ happyScore.textContent = "0%";
215
+ sadScore.textContent = "0%";
216
+ angryScore.textContent = "0%";
217
+ fearfulScore.textContent = "0%";
218
+ }
219
+
220
+ // Update mood indicator
221
+ function updateMoodIndicator(emotion, confidence) {
222
+ // Adjust text color based on emotion
223
+ let colorClass = "text-blue-400";
224
+
225
+ switch(emotion) {
226
+ case 'happy':
227
+ colorClass = "text-green-400";
228
+ break;
229
+ case 'sad':
230
+ colorClass = "text-blue-400";
231
+ break;
232
+ case 'angry':
233
+ colorClass = "text-red-400";
234
+ break;
235
+ case 'fearful':
236
+ colorClass = "text-purple-400";
237
+ break;
238
+ case 'disgusted':
239
+ colorClass = "text-yellow-400";
240
+ break;
241
+ case 'surprised':
242
+ colorClass = "text-orange-400";
243
+ break;
244
+ case 'neutral':
245
+ colorClass = "text-gray-400";
246
+ break;
247
+ }
248
+
249
+ moodText.className = `text-xl font-bold ${colorClass}`;
250
+ }
251
+
252
+ // Helper function to capitalize first letter
253
+ function capitalizeFirstLetter(string) {
254
+ return string.charAt(0).toUpperCase() + string.slice(1);
255
+ }
256
+
257
+ // Start detection
258
+ async function startDetection() {
259
+ const cameraStarted = await startVideo();
260
+ if (!cameraStarted) return;
261
+
262
+ await loadModels();
263
+
264
+ isDetecting = true;
265
+ startBtn.disabled = true;
266
+ stopBtn.disabled = false;
267
+
268
+ // Start detecting faces every 500ms
269
+ detectionInterval = setInterval(detectFaces, 500);
270
+ }
271
+
272
+ // Stop detection
273
+ function stopDetection() {
274
+ isDetecting = false;
275
+ clearInterval(detectionInterval);
276
+
277
+ // Stop video stream
278
+ const stream = video.srcObject;
279
+ if (stream) {
280
+ const tracks = stream.getTracks();
281
+ tracks.forEach(track => track.stop());
282
+ video.srcObject = null;
283
+ }
284
+
285
+ // Clear canvas
286
+ const context = canvas.getContext('2d');
287
+ context.clearRect(0, 0, canvas.width, canvas.height);
288
+
289
+ // Reset UI
290
+ startBtn.disabled = false;
291
+ stopBtn.disabled = true;
292
+ resetMoodDisplay();
293
+ }
294
+
295
+ // Event listeners
296
+ startBtn.addEventListener('click', startDetection);
297
+ stopBtn.addEventListener('click', stopDetection);
298
+
299
+ // Initialize
300
+ document.addEventListener('DOMContentLoaded', () => {
301
+ // Set canvas size to match video (will be updated in detectFaces)
302
+ canvas.width = 640;
303
+ canvas.height = 480;
304
+ });
305
+ </script>
306
+ <p style="border-radius: 8px; text-align: center; font-size: 12px; color: #fff; margin-top: 16px;position: fixed; left: 8px; bottom: 8px; z-index: 10; background: rgba(0, 0, 0, 0.8); padding: 4px 8px;">Made with <img src="https://enzostvs-deepsite.hf.space/logo.svg" alt="DeepSite Logo" style="width: 16px; height: 16px; vertical-align: middle;display:inline-block;margin-right:3px;filter:brightness(0) invert(1);"><a href="https://enzostvs-deepsite.hf.space" style="color: #fff;text-decoration: underline;" target="_blank" >DeepSite</a> - 🧬 <a href="https://enzostvs-deepsite.hf.space?remix=Bahrudin/mood" style="color: #fff;text-decoration: underline;" target="_blank" >Remix</a></p></body>
307
+ </html>