Py.Cafe

Letshadow/

dash-face-tracker

Interactive Face with Mouse-Tracking in Dash

DocsPricing
  • app.py
  • requirements.txt
app.py
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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
import dash
from dash import html, dcc, Input, Output, clientside_callback
import dash_mantine_components as dmc

# Inicializar la app
app = dash.Dash(__name__)

# ============================================
# ESTILOS ORGANIZADOS EN DICCIONARIOS
# ============================================

# Estilo del contenedor principal (body)
BODY_STYLE = {
    "minHeight": "100vh",
    "background": "linear-gradient(135deg, #2d2d2d 0%, #1a1a1a 100%)",
    "display": "flex",
    "justifyContent": "center",
    "alignItems": "center"
}

# Estilo del contenedor completo del personaje
CHARACTER_CONTAINER_STYLE = {
    "position": "relative",
    "background":"white",
    "width": "450px",
    "height": "450px",
    "display": "flex",
    "justifyContent": "center",
    "alignItems": "center"
}






# # Estilo del borde negro interno
# HEAD_INNER_BORDER_STYLE = {
#     "width": "320px",
#     "height": "270px",
#     "background": "#000",
#     "borderRadius": "170px 170px 150px 150px",
#     "clipPath": "polygon(11% 0%, 89% 0%, 100% 100%, 0% 100%)",
#     "display": "flex",
#     "justifyContent": "center",
#     "alignItems": "center",
#     "paddingTop": "5px"
# }

# Estilo del rostro interior (gris claro, trapecio)
FACE_STYLE = {
    "position": "relative",
    "width": "305px",
    "height": "255px",
    "backgroundColor": "#efe1e1",
    "borderRadius": "165px 165px 145px 145px",
    "border": "5px solid red",
    "clipPath": "polygon(11.5% 0%, 88.5% 0%, 100% 100%, 0% 100%)",
    "display": "flex",
    "flexDirection": "column",
    "alignItems": "center",
    "justifyContent": "center",
    "zIndex": "3"
}

# Estilo de la diadema negra
HEADBAND_OUTER_STYLE = {
    "position": "absolute",
    "width": "380px",
    "height": "50px",
    "background": "#000",
    "top": "calc(50% - 110px)",
    "left": "50%",
    "transform": "translateX(-50%)",
    "zIndex": "10",
    "borderRadius": "25px",
    "display": "flex",
    "justifyContent": "center",
    "alignItems": "center"
}

# Rectángulos decorativos en la diadema
HEADBAND_RECT_STYLE = {
    "width": "25px",
    "height": "15px",
    "background": "#555",
    "margin": "0 8px"
}

# Estilo del auricular izquierdo
HEADPHONE_LEFT_STYLE = {
    "position": "absolute",
    "width": "50px",
    "height": "60px",
    "background": "#1a1a1a",
    "borderRadius": "50%",
    "border": "8px solid #000",
    "left": "-25px",
    "top": "50%",
    "transform": "translateY(-50%)",
    "zIndex": "9",
    "boxShadow": "inset 5px 5px 10px rgba(255,255,255,0.1)"
}

# Estilo del auricular derecho
HEADPHONE_RIGHT_STYLE = {
    "position": "absolute",
    "width": "50px",
    "height": "60px",
    "background": "#1a1a1a",
    "borderRadius": "50%",
    "border": "8px solid #000",
    "right": "-25px",
    "top": "50%",
    "transform": "translateY(-50%)",
    "zIndex": "9",
    "boxShadow": "inset -5px 5px 10px rgba(255,255,255,0.1)"
}

# Estilo del soporte del micrófono
MIC_ARM_STYLE = {
    "position": "absolute",
    "width": "4px",
    "height": "70px",
    "background": "#333",
    "right": "15px",
    "top": "50%",
    "transform": "translateY(-50%) rotate(-5deg)",
    "transformOrigin": "top",
    "zIndex": "11"
}

# Estilo del micrófono
MIC_HEAD_STYLE = {
    "position": "absolute",
    "width": "22px",
    "height": "28px",
    "background": "#1a1a1a",
    "borderRadius": "50%",
    "right": "7px",
    "bottom": "-5px",
    "border": "3px solid #000"
}

# Estilo del contenedor de ojos
EYES_CONTAINER_STYLE = {
    "position": "relative",
    "display": "flex",
    "gap": "70px",
    "marginTop": "-20px",
    "marginBottom": "25px"
}


# Estilo del ojo (BLANCO, no gris)
EYE_WHITE_STYLE = {
    "background": "#000000",
    "width": "40px",
    "height": "56px",
    "position": "relative",
    "borderRadius": "50%",
    "display": "flex",
    "justifyContent": "center",
    "alignItems": "center"
}

# Estilo de la pupila (parte negra del ojo)
PUPIL_STYLE = {
    "background": "#efe1e1",
    "width": "10px",
    "height": "20px",
    "position": "absolute",
    "borderRadius": "50%",
    "top": "50%",
    "left": "50%",
    "transform": "translate(-50%, -50%)"
}

# Estilo del brillo en el ojo
EYE_SHINE_STYLE = {
    "position": "absolute",
    "width": "6px",
    "height": "9px",
    "background": "#fff",
    "borderRadius": "50%",
    "top": "32%",
    "left": "38%",
    "zIndex": "1"
}

# Estilo del contenedor de la boca
MOUTH_CONTAINER_STYLE = {
    "position": "relative",
    "display": "flex",
    "alignItems": "flex-end",
    "marginTop": "5px"
}

# Estilo de la parte izquierda de la boca (rectángulo negro)
MOUTH_LEFT_STYLE = {
    "padding":"0px",
    "margin":"0px",
    "width": "45px",
    "height": "32px",
    "background": "#000",
    "borderRadius": "0 0 0 8px"
}

# Estilo de la parte derecha de la boca (rectángulo negro)
MOUTH_RIGHT_STYLE = {
    "padding":"0px",
    "margin":"0px",
    "width": "45px",
    "height": "32px",
    "background": "#000",
    "borderRadius": "0 0 8px 0"
}

# Estilo de la lengua/parte rosada
TONGUE_STYLE = {
    "position": "absolute",
    "width": "28px",
    "height": "18px",
    "background": "#ffb3ba",
    "borderRadius": "50%",
    "bottom": "3px",
    "left": "50%",
    "transform": "translateX(-50%)"
}

# ============================================
# LAYOUT DE LA APLICACIÓN
# ============================================

app.layout = html.Div(
    style=BODY_STYLE,
    children=[
        html.Div(
            style=CHARACTER_CONTAINER_STYLE,
            children=[                            
                # Diadema con auriculares
                # html.Div(
                #     style=HEADBAND_OUTER_STYLE,
                #     children=[
                #         # Auricular izquierdo
                #         html.Div(style=HEADPHONE_LEFT_STYLE),
                        
                #         # Rectángulos decorativos
                #         html.Div(style=HEADBAND_RECT_STYLE),
                #         html.Div(style=HEADBAND_RECT_STYLE),
                #         html.Div(style=HEADBAND_RECT_STYLE),
                        
                #         # Auricular derecho con micrófono
                #         html.Div(
                #             style=HEADPHONE_RIGHT_STYLE,
                #             children=[
                #                 html.Div(
                #                     style=MIC_ARM_STYLE,
                #                     children=[
                #                         html.Div(style=MIC_HEAD_STYLE)
                #                     ]
                #                 )
                #             ]
                #         )
                #     ]
                # ),
                
                # Cabeza (trapecio con bordes)
                
                        
            
                    html.Div(
                        id='face',
                        style=FACE_STYLE,
                        children=[
                            # Contenedor de ojos
                            html.Div(
                                style=EYES_CONTAINER_STYLE,
                                children=[
                                    # Ojo izquierdo
                            
                                    html.Div(
                                        className='eye',
                                        style=EYE_WHITE_STYLE,
                                        children=[
                                            html.Div(
                                                className='pupil',
                                                style=PUPIL_STYLE,
                                            ),
                                            
                                        ]
                                    ),

                                    
                                    # Ojo derecho
                                    html.Div(
                                        className='eye',
                                        style=EYE_WHITE_STYLE,
                                        children=[
                                            html.Div(
                                                className='pupil',
                                                style=PUPIL_STYLE,
                                            ),
                                            
                                        ]
                                    ),
                                ]
                            ),
                            
                            # Contenedor de boca
                            html.Div(
                               
                                id='mouth-container',
                                style=MOUTH_CONTAINER_STYLE,
                                children=[
                                    html.Div(id='mouth-left', style=MOUTH_LEFT_STYLE),
                                    html.Div(id='mouth-right', style=MOUTH_RIGHT_STYLE),
                                    html.Div(style=TONGUE_STYLE),
                                ]
                            )
                        ]
                    )
                
                        

            ]
        ),
        
        # Store para inicializar
        dcc.Store(id='init-store')
    ]
)

# ============================================
# CALLBACKS
# ============================================

# Callback para el movimiento de pupilas
clientside_callback(
    """
    function(n) {
        function eyeball(event) {
            let pupils = document.querySelectorAll(".pupil");
            
            pupils.forEach(function(pupil) {
                let eye = pupil.closest('.eye');
                
                // Obtener las coordenadas del centro del ojo
                let x = eye.getBoundingClientRect().left + eye.clientWidth/2;
                let y = eye.getBoundingClientRect().top + eye.clientHeight/2;
                
                // Calcular la distancia y dirección
                let deltaX = -event.pageX + x;
                let deltaY = -event.pageY + y;
                let distance = Math.sqrt(deltaX * deltaX + deltaY * deltaY);
                
                // Limitar el movimiento de la pupila
                let maxDistance = 10;
                let moveX = (deltaX / distance) * Math.min(distance / 15, maxDistance);
                let moveY = (deltaY / distance) * Math.min(distance / 15, maxDistance);
                
                pupil.style.transform = `translate(calc(-50% + ${moveX}px), calc(-50% + ${moveY}px))`;
            });
        }
        
        // Agregar el event listener al body
        document.body.addEventListener('mousemove', eyeball);
        
        return window.dash_clientside.no_update;
    }
    """,
    Output('init-store', 'data'),
    Input('init-store', 'id')
)

# Callback para el efecto hover en la boca
clientside_callback(
    """
    function(n) {
        const face = document.getElementById('face');
        const mouthLeft = document.getElementById('mouth-left');
        const mouthRight = document.getElementById('mouth-right');
        
        if (face && mouthLeft && mouthRight) {
            face.addEventListener('mouseenter', function() {
                mouthLeft.style.width = '55px';
                mouthLeft.style.height = '40px';
                mouthRight.style.width = '55px';
                mouthRight.style.height = '40px';
                mouthLeft.style.transition = 'all 0.3s ease';
                mouthRight.style.transition = 'all 0.3s ease';
            });
            
            face.addEventListener('mouseleave', function() {
                mouthLeft.style.width = '45px';
                mouthLeft.style.height = '32px';
                mouthRight.style.width = '45px';
                mouthRight.style.height = '32px';
            });
        }
        
        return window.dash_clientside.no_update;
    }
    """,
    Output('face', 'data-hover'),
    Input('face', 'id')
)

if __name__ == '__main__':
    app.run_server(debug=True, port=8050)