File size: 30,185 Bytes
1daf4cc
 
 
 
 
 
 
 
afaee26
 
1daf4cc
 
 
 
 
 
b2466ee
1daf4cc
 
 
 
 
 
 
 
 
afaee26
 
 
1daf4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59385d6
 
 
 
20f851d
1daf4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
28a2ae8
1daf4cc
 
 
 
 
 
 
 
 
 
 
b3e105b
 
1daf4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3e105b
1daf4cc
57303f6
1daf4cc
 
 
 
57303f6
 
b3e105b
1daf4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
57303f6
1daf4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b3e105b
1daf4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
da1e67c
 
 
 
afaee26
da1e67c
 
1daf4cc
 
 
 
da1e67c
1456454
d989db2
1daf4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b2466ee
 
 
 
 
1daf4cc
 
 
b3e105b
1daf4cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
59385d6
1daf4cc
 
 
 
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
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847

from Main import wav2art
import numpy as np
import librosa 
import base64
import gc
gc.enable()

import os

import matplotlib.pyplot as plt
import soundfile as sf
from cv2 import resize, INTER_LINEAR
from PIL import Image

import scipy.signal as signal
from matplotlib.animation import FuncAnimation, FFMpegWriter

import torch
import librosa

import streamlit as st

from PIL import Image
import streamlit as st

dir_path = os.path.dirname(os.path.realpath(__file__))

im = Image.open(dir_path + "/Logo.png")
st.set_page_config(
    page_title="Watch Me Talk",
    page_icon=im,
)


st.markdown(
    """
    <style>
    .container {
        display: flex;
    }
    .logo-text {
        font-weight:700 !important;
        font-size:50px !important;
        color: #f9a01b !important;
        padding-top: 75px !important;
    }
    .logo-img {
        float:right;
    }
    </style>
    """,
    unsafe_allow_html=True
)

st.markdown(
    f"""
    <div class="container">
        <img class="logo-img" src="data:image/png;base64,{base64.b64encode(open('Logo.png', "rb").read()).decode()}">
        <p class="logo-text">Watch Me Speak</p>
    </div>
    """,
    unsafe_allow_html=True
)
# st.markdown('# Watch Me Talk')

hline = '''
---
'''

what = st.radio(
     "Select an option:",
     ('See examples', 'Upload audio file'))


st.markdown(hline)

if what == 'See examples':
    example = st.radio(
        "Select a sample sentence:",
        (
        "1. Be careful not to plow over the flower beds.",
        "2. Project development was proceeding too slowly.",
        "3. This brochure is particularly informative for a prospective buyer.",
        "4. I'd rather not buy these shoes than be overcharged.",
        "5. Those musicians harmonize marvellously."
        )
    )

    if example is not None:

        example_ind = example[0]
        example_path = str(example_ind) + '.mp4'

        video_file = open(example_path, 'rb')
        video_bytes = video_file.read()

        st.video(video_bytes)


if what == 'Upload audio file':
    uploaded_file = st.file_uploader("Choose a .wav file", type=["wav"])
    if uploaded_file is not None:
       
        wav_np, sr = librosa.load(uploaded_file, sr = 16000)
        sf.write('audio.wav', wav_np, sr, subtype='PCM_24')

        wav = wav_np.tolist()    
        
        emaTemp2 = wav2art(wav_np)
    
        my_bar = st.progress(0)

        TA = st.empty()
        text = TA.text_area("", "Firing up!")


        C_AH = np.array([0.16793381, 0.61851003, 0.3930835 , 2.73506039, 0.84535045,
               1.04448936, 1.5337297 , 1.37088293, 1.52812757, 1.00774768,
               1.84668592, 1.06159204])

        meanOfData_AH = np.array([ 10.18237309,   6.88706306,  11.9609807 ,  -4.72927955,
                -0.77401723, -44.12270889, -13.25678639,  -0.96288923,
               -23.19792409,   3.77430628, -31.07672544,   7.20676208])

        meanOfData = np.mean(emaTemp2,axis=0)
        emaTemp2 -= meanOfData
        C = 0.5*np.sqrt(np.mean(np.square(emaTemp2),axis=0))
        ema = np.divide(emaTemp2,C)

        ema = ema*C_AH + meanOfData_AH
        emaR_sub = np.reshape(ema, (len(ema), 6, 2))
        
        my_bar.progress(10)



        text = TA.text_area("", "Loading wav2vec 2.0 ...")

        model = torch.load("model.pt")

        tokenizer = torch.load('tokenizer.pt')
        feature_extractor = torch.load('feature_extractor.pt')

        my_bar.progress(15)

        text = TA.text_area("", "Estimating phonemes ...")

        input_values = feature_extractor(wav, return_tensors="pt", sampling_rate = sr).input_values

        logits = model(input_values).logits[0]
        pred_ids = torch.argmax(logits, axis=-1)
        
        # retrieve word stamps (analogous commands for `output_char_offsets`)
        outputs = tokenizer.decode(pred_ids, output_char_offsets = True)

        time_offset = model.config.inputs_to_logits_ratio

        phoneme_dict = {'m' : 0, 'ɱ' : 0, 'n' : 0, 'ŋ' : 0, 'ɴ' : 0}
        char_offsets = [
            {
                "char": d["char"],
                "start_time": round(d["start_offset"] * time_offset, 2),
                "end_time": round(d["end_offset"] * time_offset, 2),
                "nasality": phoneme_dict.get(d["char"], 1) 
            }
            for d in outputs.char_offsets
        ]

        initial = char_offsets[0]['start_time']
        final = len(wav) - char_offsets[-1]['end_time']

        phonemes = []
        toprint = []

        for p in range(initial):
            phonemes.append(0)
            toprint.append('')

        prev_phon = 0
        prev_tp = ''

        for p in char_offsets:
            for t in range(int(np.floor((p['start_time'] - final)/2))):
                phonemes.append(prev_phon)
                toprint.append(prev_tp)

            for t in range(int(np.ceil((p['start_time'] - final)/2))):
                phonemes.append(p['nasality'])
                toprint.append(p['char'])
            
            final = p['end_time']
            
            for t in range(p['end_time'] - p['start_time']):
                phonemes.append(p['nasality'])
                toprint.append(p['char'])

            prev_phon = p['nasality']
            prev_tp = p['char']

            
        final = len(wav) - char_offsets[-1]['end_time']

        for p in range(final):
            phonemes.append(0)
            toprint.append('')

        length = int(len(emaR_sub) * 0.3)
        ema = signal.resample(emaR_sub, length)

        my_bar.progress(25)

        processed = []
        to_print = []

        for d in range(0, len(phonemes), 160):
            begin = (d - 160 > 0) * (d - 160 )
            phones20 = phonemes[begin : d + 160]
            phone = max(set(phones20), key=phones20.count)
            processed.append(phone)

            phones20 = toprint[begin : d + 160]
            phone = max(set(phones20), key=phones20.count)
            to_print.append(phone)


        res = resize(np.array(processed).astype('float32'), dsize=(1, length), interpolation = INTER_LINEAR)

        processed2 = res.flatten()

        final_print = []

        i = 0

        while i < len(to_print):
            
            z = int(3 + i % 3)
            
            phones20 = to_print[i : i + z]
            phone = max(set(phones20), key=phones20.count)
            final_print.append(phone)

            i += z


        gaps = np.abs(len(processed2) - len(final_print))

        total_len = len(final_print)

        for i, j in enumerate(np.linspace(0,  total_len - 1, gaps)):
            del_index = int(j - i - 1)
            del final_print[del_index]

        kernel = [1/10 for i in range(10)]
        processed3 = np.convolve(processed2, kernel)


        ### below code is to find the average distance between certain ema points in a dataset so that we can define our offsets for
        ### the bezier curve contours accordingly later on

        def find_off(matrix):
          p = 0
          q = 0

          for i in range(len(matrix)):
            q += matrix[i][0][1] - matrix[i][1][1]
            p += matrix[i][1][0] - matrix[i][2][0]

          p = p/len(matrix)
          q = q/len(matrix)
          return np.round_([p, q])

        ### defining offsets for the t_end and the tongue base points so as to make the curves as relative as possible
        def tongue_off(matrix):
          c = 0
          d = 0

          for i in range(len(matrix)):
            d += matrix[i][3][1] - matrix[i][5][1]
            c += matrix[i][3][0] - matrix[i][4][0]
          
          c = np.abs(c/len(matrix))
          d = np.abs(d/len(matrix))
          return np.round_([c, d])

        k = find_off(ema)
        l = tongue_off(ema)


        print(ema[0])
        l = l*1.5

        global painting

        from scipy.special import comb
        def bernstein_poly(i, n, t):
                return comb(n, i) * ( t**(n-i) ) * (1 - t)**i

        def bezier_curve(xPoints, yPoints, nTimes=150):
                nPoints = len(xPoints)
                t = np.linspace(0.0, 1.0, nTimes)
                polynomial_array = np.array([bernstein_poly(i, nPoints-1, t) for i in range(0, nPoints)])
                xvals = np.dot(xPoints, polynomial_array)
                yvals = np.dot(yPoints, polynomial_array)
                return xvals, yvals, nTimes


        def contract(x):
            m = np.mean(x)
            for i in range(len(x)):
                x[i] = (2*m + 3*x[i]) / 5
            return x


        brain0 = Image.open('BrainAndSpinal.png')


        text = TA.text_area("", "Rendering frame: 0 of " + str(len(ema)))



        import time

        plt.rcParams.update({'font.size': 22})

        n = 6
        clr = np.arange(6)
        arr = np.ones((6, 2))
        plots = []

        particles = np.zeros(n,dtype=[("position", float, 2),
                                   ("size", float , 1)])

        particles["position"] = ema[0]
        particles["size"] = 0.5*np.ones(n)

        n1o = ema[0]
        ulo = n1o[0]
        llo = n1o[1]
        jawo = n1o[2]
        tto = n1o[3]
        tbo = n1o[4]
        tdo = n1o[5]

        max_tt = np.max(np.abs(ema[:, 3, 0]))

        fig = plt.figure(figsize = (6, 6))
        ax = plt.axes()


        llj0 = np.array([llo[0], llo[0]+(k[0]*4/13), llo[0]-(k[0]*25/13), jawo[0]+(k[0]*5/13), jawo[0]])
        llj1 = np.array([llo[1], llo[1]-(k[1]*12/12), llo[1]-(k[1]*10/12), jawo[1]+(k[1]*5/13), jawo[1]])


        nose0 = np.array([tto[0]+(k[0]*14/13), ulo[0]+(k[0]*7/13), ulo[0]+(k[0]*36/13), ulo[0]-(k[0]*10/13), ulo[0]])
        nose1 = np.array([ulo[1]+(k[1]*50/12), ulo[1]+(k[1]*35/12), ulo[1]-(k[1]*7/13), ulo[1]+(k[1]*35/12), ulo[1]])

        llip0 = np.array([llo[0], llo[0] - .2*(l[0]), llo[0] - .5*(l[0])])
        llip1 = np.array([llo[1],llo[1] + .1*(l[1]), llo[1] - .2*(l[1])])

        ulip0 = np.array([ulo[0], ulo[0], ulo[0]-(k[0]*5/13), ulo[0]-(k[0]*8/13)])
        ulip1 = np.array([ulo[1], ulo[1]-(k[1]*4/12), ulo[1]-(k[1]*4/12), ulo[1]+(k[1]*5/12)])

        chin0 = np.array([jawo[0], jawo[0]-(k[0]*5/13), jawo[0]-(k[0]*15/13), 2*tdo[0]- 1*tbo[0]])
        chin1 = np.array([jawo[1], jawo[1]-(k[1]*10/13), jawo[1]-(k[1]*2/13), jawo[1]-(k[1]*10/13)])

        tongue0 = np.array([tto[0], tdo[0]-(l[0]*8/13), tdo[0]-(l[0]*5/13), tdo[0] - 1.5*l[0], tdo[0] - 1*l[0] - (75/(tto[0] - tdo[0]))])
        tongue1 = np.array([tto[1], tdo[1]+(l[1]*7/13), tdo[1]-(l[1]*1/13), jawo[1]+(l[1]*36/13), jawo[1]+(l[1]*8/13)])

        tline0 = np.array([jawo[0] - .4*l[0], .5*tbo[0] + .5*tto[0] , tdo[0], tdo[0] - 1*l[0] - (75/(tto[0] - tdo[0]))])
        tline1 = np.array([jawo[1]+(l[1]*9/13), jawo[1]+(l[1]*5/13), jawo[1]+(l[1]*5/13), jawo[1]+(l[1]*8/13)])

        Xa, Ya, Na = bezier_curve(xPoints = llj0, yPoints = llj1)
        Xb, Yb, Nb = bezier_curve(xPoints = nose0, yPoints = nose1)
        Xc, Yc, Nc = bezier_curve(xPoints = tongue0, yPoints = tongue1)
        Xd, Yd, Nd = bezier_curve(xPoints = llip0, yPoints = llip1)
        Xe, Ye, Ne = bezier_curve(xPoints = ulip0, yPoints = ulip1)

        temp1 = int(Ne/5)
        temp2 = int(Nb/6)

        # palate0 = np.array([Xe[temp1], Xe[temp1]-(k[0]*4/13), Xe[temp1]-(k[0]*8/13), Xe[temp1]-(k[0]*23/13), Xe[temp1]-(k[0]*32/10)])
        # palate1 = np.array([Ye[temp1], Ye[temp1], Ye[temp1]+(k[1]*7/12), Ye[temp1]+(k[1]*20/12), Ye[temp1]+(k[1]*8/12)])

        palate0 = np.array([Xe[temp1], Xe[temp1]-(k[0]*4/13), Xe[temp1]-(k[0]*8/13), Xe[temp1]-(k[0]*23/13), Xe[temp1]-(k[0]*32/10)])
        palate1 = np.array([Ye[temp1], Ye[temp1], Ye[temp1]+(k[1]*7/12), Ye[temp1]+(k[1]*10/12), Ye[temp1]+(k[1]*8/12)])

        # nose_base0 = np.array([Xb[20], Xb[temp2]-(k[0]*3/13) , Xb[temp2]-(k[0]*9/13), Xe[temp1]-(k[0]*32/10), Xe[temp1]-(k[0]*34/10)])
        # nose_base1 = np.array([Yb[20], Yb[temp2]-(k[1]*10/12), Yb[temp2]+(k[1]*17/12), Ye[temp1]+(k[1]*15/12), Ye[temp1]+(k[1]*13/12)])

        nose_base0 = np.array([Xb[20], Xb[temp2]-(k[0]*3/13) , Xb[temp2]-(k[0]*9/13), Xe[temp1]-(k[0]*32/10), Xe[temp1]-(k[0]*34/10)])
        nose_base1 = np.array([Yb[20], Yb[temp2]-(k[1]*10/12), Yb[temp2]+(k[1]*13/12), Ye[temp1]+(k[1]*13/12), Ye[temp1]+(k[1]*13/12)])

        xref = Xe[temp1]
        yref = Ye[temp1]



        forehead0 = np.array([Xb[Nb-1], Xb[Nb-1]-(k[0]*4/13), Xb[Nb-1]-(k[0]*2/13)])
        forehead1 = np.array([Yb[Nb-1], Yb[Nb-1]+(k[1]*6/12), Yb[Nb-1]+(k[1]*11/12)])


        nose_bone0 = np.array([Xc[0] - .6*l[0], Xc[0] - .5*l[0], tdo[0] - 1.6*l[0], Xe[temp1]-(k[0]*45/13), tbo[0]-(k[0]*20/13), tto[0]+(k[0]*5/13), tto[0]+(k[0]*6/13), Xb[temp2]+(k[0]*10/13), Xb[40]])
        nose_bone1 = np.array([jawo[1], tto[1], Ye[temp1]+(k[1]*30/12),ulo[1]+(k[1]*32/12),  ulo[1]+(k[1]*32/12), ulo[1]+(k[1]*65/12), ulo[1]+(k[1]*35/12), Yb[temp2]+(k[1]*15/12), Yb[40]])

        xxx0 = tto[0]+(l[0]*5/13)
        xxx1 = tto[0]-(l[0]*1/13)
        xxx12 = tto[0]-(l[0]*1/13)
        xxx2 = tto[0]-(l[0]*1/13)
        xxx3 = tto[0]-(l[0]*2/13)

        yyy0 = tto[1]+(l[1]*0/12)
        yyy1 = tto[1]-(l[1]*15/12)
        yyy12 = tto[1]-(l[1]*5/12)
        yyy2 = jawo[1]+(l[1]*32/13)
        yyy3 = jawo[1]+(l[1]*30/13)

        tbase0 = np.array([Xc[-5], xxx0, xxx1, xxx12, xxx2, xxx3, jawo[0] - .5*l[0]])
        tbase1 = np.array([Yc[-5], yyy0, yyy1, yyy12, yyy2, yyy3, jawo[1]+(l[1]*22/13)])


        Xf, Yf, Nf = bezier_curve(palate0, palate1)
        Xg, Yg, Ng = bezier_curve(chin0, chin1)
        Xh, Yh, Nh = bezier_curve(tbase0, tbase1)
        Xi, Yi, Ni = bezier_curve(tline0, tline1)
        Xj, Yj, Nj = bezier_curve(nose_base0, nose_base1)
        Xk, Yk, Nk = bezier_curve(nose_bone0, nose_bone1)

        uv_diff = phonemes[0] * 4

        aa0 = Xj[0]
        aa1 = Xj[0] - (k[0]*7/10)
        aa2 = Xj[0] - (k[0]*(9 + 2*uv_diff)/10)
        aa3 = Xj[0] - (k[0]*(6 + uv_diff)/10)
        aa4 = Xf[0] - (k[0]*5/10)
        aan = Xf[0]

        bb0 = Yj[0]
        bb1 = Yj[0] + (k[1]*(uv_diff/2)/10)
        bb2 = Yj[0] - (k[1]*(18 - uv_diff/2)/10)
        bb3 = Yj[0] - (k[1]*(20 - uv_diff/2)/10)
        bb4 = Yf[0] + (k[1]*(1 + uv_diff/2)/10)
        bbn = Yf[0]


        uvula0 = np.array([aa0, aa1, aa2, aa3,  aa4, aan])
        uvula1 = np.array([bb0, bb1, bb2, bb3,  bb4, bbn])

        Xl, Yl, Nl = bezier_curve(uvula0, uvula1)

        xref2 = xref-(k[0]*45/13)
        yref2 = yref+(k[1]*30/12)
        head0 = np.array([tto[0]+(k[0]*14/13), tto[0]+(k[0]*5/13), tto[0]+(k[0]*7/13), xref2+(l[0]*50/11), xref2-(l[0]*50/8), xref2-(l[0]*50/6), tdo[0] - (tto[0] - tdo[0])*2.5, (9*xref2)/4])
        head1 = np.array([ulo[1]+(k[1]*50/12), ulo[1]+(k[1]*60/12), ulo[1]+(k[1]*70/12), yref2+(l[1]*10), yref2+(l[1]*8), yref2+(l[1]*2), jawo[1]+(l[1]*18/4), jawo[1] - .5*l[1]])
        Xn, Yn, Nn = bezier_curve(head0, head1)

        x1 = ulo[0] - 5*l[0]/6
        x2 = ulo[0] - 1*l[0]/6
        x4 = ulo[0] - 4*l[0]/6

        y1 = ulo[1] + 4*l[1]/4
        y2 = ulo[1] - 1*l[1]/4
        y4 = ulo[1] - 3*l[1]/4


        utooth_front0 = np.array([x1, x2, x4])
        utooth_front1 = np.array([y1, y2, y4])

        Xq, Yq, Nq = bezier_curve(utooth_front0, utooth_front1)


        x1 = ulo[0] - 10*l[0]/12
        x2 = ulo[0] - 11*l[0]/12
        x3 = ulo[0] - 7*l[0]/12
        x4 = ulo[0] - 8*l[0]/12

        y1 = ulo[1] + 4*l[1]/4
        y2 = ulo[1] - 1*l[1]/4
        y3 = ulo[1] + 2*l[1]/4
        y4 = ulo[1] - 3*l[1]/4


        utooth_back0 = np.array([x1, x2, x3, x4])
        utooth_back1 = np.array([y1, y2, y3, y4])

        Xr, Yr, Nr = bezier_curve(utooth_back0, utooth_back1)

        xx1 = jawo[0] - .6*l[0]
        xx2 = jawo[0] - .4*l[0]
        xx3 = jawo[0] + .3*l[0]
        xx4 = jawo[0] + .1*l[0]
        xx5 = jawo[0] - 1.1*l[0]
        xx6 = jawo[0] - .7*l[0]
        xx7 = jawo[0] - .6*l[0]

        yy1 = jawo[1]+(l[1]*30/13)
        yy2 = jawo[1]+(l[1]*40/13)
        yy3 = jawo[1]+(l[1]*30/13)
        yy4 = jawo[1]-(l[1]*3/13)
        yy5 = jawo[1]-(l[1]*5/13)
        yy6 = jawo[1]+(l[1]*20/13)
        yy7 = jawo[1]+(l[1]*30/13)

        ltooth_base0 = np.array([xx1, xx2, xx3, xx4, xx5, xx6, xx7])
        ltooth_base1 = np.array([yy1, yy2, yy3, yy4, yy5, yy6, yy7])

        Xs, Ys, Ns = bezier_curve(ltooth_base0, ltooth_base1)

        x1 = jawo[0] - l[0]*.45
        x2 = jawo[0] + l[0]*0.215
        x3 = jawo[0] - l[0]*0.215

        y1 = jawo[1]+(l[1]*18/13)
        y2 = jawo[1]+(l[1]*28/13)
        y3 = jawo[1]+(l[1]*39/13)

        ltooth_front0 = np.array([x1, x2, x3])
        ltooth_front1 = np.array([y1, y2, y3])

        Xt, Yt, Nt = bezier_curve(ltooth_front0, ltooth_front1)

        x1 = jawo[0] - l[0]*.45
        x2 = jawo[0] - l[0]*.45
        x3 = jawo[0] - l[0]*0.25
        x4 = jawo[0] - l[0]*0.215

        y1 = jawo[1]+(l[1]*18/13)
        y2 = jawo[1]+(l[1]*33/13)
        y3 = jawo[1]+(l[1]*28/13)
        y4 = jawo[1]+(l[1]*39/13)

        ltooth_back0 = np.array([x1, x2, x3, x4])
        ltooth_back1 = np.array([y1, y2, y3, y4])

        Xu, Yu, Nu = bezier_curve(ltooth_back0, ltooth_back1)

        ccc = 'blue'
        line1, = ax.plot(Xa, Ya, color =  ccc)
        line2, = ax.plot(Xb, Yb, color =  ccc)
        line3, = ax.plot(Xc, Yc, color =  ccc)
        line4, = ax.plot(Xd, Yd, color =  ccc)
        line5, = ax.plot(Xe, Ye, color =  ccc)
        line6, = ax.plot(Xf, Yf, color =  ccc)
        line7, = ax.plot(Xg, Yg, color =  ccc)
        line8, = ax.plot(Xh, Yh, color =  ccc)
        line9, = ax.plot(Xi, Yi, color =  ccc, zorder = 1)
        line10, = ax.plot(Xj, Yj, color = ccc)
        line11, = ax.plot(Xk, Yk, color = ccc, zorder = 5)
        line12, = ax.plot(Xl, Yl, color = ccc)
        line13, = ax.plot(Xn, Yn, color = ccc, zorder = 5)
        line14, = ax.plot(Xq, Yq, color = ccc, zorder = 5)
        line15, = ax.plot(Xr, Yr, color = ccc, zorder = 5)
        line16, = ax.plot(Xs, Ys, color = ccc, zorder = 3)
        line17, = ax.plot(Xt, Yt, color = ccc, zorder = 5)
        line18, = ax.plot(Xu, Yu, color = ccc, zorder = 5)


        class Args():
          def __init__(self):
            self.painting1 = ax.fill(Xc, Yc, 'pink')
            self.painting2 = ax.fill(np.concatenate((Xh, Xi)), np.concatenate((Yh, Yi)), 'pink')
            self.painting3 = ax.fill(Xs, Ys, 'gainsboro', zorder = 3)
            self.painting4 = ax.fill(np.concatenate((Xq, Xr)), np.concatenate((Yq, Yr)), 'white', zorder = 4)
            self.painting5 = ax.fill(np.concatenate((Xt, Xu)), np.concatenate((Yt, Yu)), 'white', zorder = 4)
            # self.painting6 = ax.fill(np.concatenate((Xk, Xn)), np.concatenate((Yk, Yn)), 'white', zorder = 3)

        # plt.scatter(aa1, bb1)
        # plt.scatter(aa2, bb2)
        # plt.scatter(aa3, bb3)
        # plt.scatter(aa4, bb4)

        c_text = ax.text(-55, 120, '', alpha = 1)

        args = Args()

        newax = fig.add_axes([.025, .20, .63, .63], anchor='NE', zorder = 7)
        newax.imshow(brain0)
        newax.axis('off')
        ax.axis('off')


        my_bar.progress(30)

        old_phon = ''

        def remove_all(xx):
            for x in xx:
                x.remove()

        def update_st(f):

           loaded = int(30 + (54 * f) / len(ema))
           my_bar.progress(loaded)
           text = TA.text_area("", "Rendering frame: " + str(f) + " of " + str(len(ema)), key = f + time.time())


        def animate(frame_number):
           #plt.clf()

           global temp, old_phon, my_bar
           
           update_st(frame_number)

           particles["position"] = ema[frame_number]
           n1 = ema[frame_number]
           phon = processed3[frame_number]

           if old_phon != final_print[frame_number]:
               c_text.set_text(final_print[frame_number])
               old_phon = final_print[frame_number]

        #    if frame_number > 1:
        #        text1.set_text(final_print[frame_number-2])
        #        text2.set_text(final_print[frame_number-1])

        #    text3.set_text(final_print[frame_number])
           
        #    if frame_number < len(final_print) - 2:
        #        text4.set_text(final_print[frame_number + 1])
        #        text5.set_text(final_print[frame_number + 2])


           remove_all(args.painting1)
           remove_all(args.painting2)
           remove_all(args.painting3)
        #    remove_all(args.painting4)
           remove_all(args.painting5)
           
           ul = n1[0]
           ll = n1[1]
           jaw = n1[2]
           tt = n1[3]
           tb = n1[4]
           td = n1[5]
        #    scatter.set_offsets(particles["position"])

           llj_x = np.array([ll[0], ll[0]+(k[0]*4/13), ll[0]-(k[0]*25/13), jaw[0]+(k[0]*5/13), jaw[0]])
           llj_y = np.array([ll[1], ll[1]-(k[1]*12/12), ll[1]-(k[1]*10/12), jaw[1]+(k[1]*5/13), jaw[1]])

           nose_x = np.array([tto[0]+(k[0]*14/13), ulo[0]+(k[0]*5/13), ulo[0]+(k[0]*36/13), ulo[0]-(k[0]*10/13), ul[0]])
           nose_y = np.array([ulo[1]+(k[1]*50/12), ulo[1]+(k[1]*35/12), ulo[1]-(k[1]*7/13), ulo[1]+(k[1]*35/12), ul[1]])

           tongue_x = np.array([tt[0], td[0]-(l[0]*8/13), td[0]-(l[0]*5/13), td[0] - 1.5*l[0], tdo[0] - 1*l[0] - (75/(tto[0] - tdo[0]))])
           tongue_y = np.array([tt[1], td[1]+(l[1]*7/13), td[1]-(l[1]*1/13), jaw[1]+(l[1]*36/13), jaw[1]+(l[1]*8/13)])


           llip_x = np.array([ll[0], ll[0] - .2*(l[0]), ll[0] - .5*(l[0])])
           llip_y = np.array([ll[1], ll[1] + .1*(l[1]), ll[1] - .2*(l[1])])
          
           ulip_x = np.array([ul[0], ul[0], ul[0]-(k[0]*5/13), Xe[temp1]])
           ulip_y = np.array([ul[1], ul[1]-(k[1]*4/12), ul[1]-(k[1]*4/12), Ye[temp1]])

           chin_x = np.array([jaw[0], jaw[0]-(k[0]*5/13), jaw[0]-(k[0]*15/13), 2*tdo[0] - 1*tbo[0]])
           chin_y = np.array([jaw[1], jaw[1]-(k[1]*10/13), jaw[1]-(k[1]*2/13), jaw[1]-(k[1]*10/13)])

           tline_x = np.array([jaw[0] - .4*l[0], .5*tb[0] + .5*tt[0] , tdo[0], tdo[0] - 1*l[0] - (75/(tto[0] - tdo[0]))])
           tline_y = np.array([jaw[1]+(l[1]*9/13), jaw[1]+(l[1]*5/13), jaw[1]+(l[1]*5/13), jaw[1]+(l[1]*8/13)])

           X1, Y1, N1 = bezier_curve(xPoints = llj_x, yPoints = llj_y)
           X2, Y2, N2 = bezier_curve(xPoints = nose_x, yPoints = nose_y)
           X3, Y3, N3 = bezier_curve(xPoints = tongue_x, yPoints = tongue_y)
           X4, Y4, N4 = bezier_curve(xPoints = llip_x, yPoints = llip_y)
           X5, Y5, N5 = bezier_curve(xPoints = ulip_x, yPoints = ulip_y)

           for i in range(N5):
             if Y5[i] == Ye[int(N5/5)]:
               temp = i
           
        #    palate_x = np.array([Xe[temp1], Xe[temp1]-(k[0]*4/13), Xe[temp1]-(k[0]*8/13), Xe[temp1]-(k[0]*23/13), Xe[temp1]-(k[0]*32/10)])
        #    palate_y = np.array([Ye[temp1], Ye[temp1], Ye[temp1]+(k[1]*7/12), Ye[temp1]+(k[1]*10/12), Ye[temp1]+(k[1]*8/12)])

           nose_base_x = np.array([X2[20], Xb[temp2]-(k[0]*3/13) , Xb[temp2]-(k[0]*9/13), Xe[temp1]-(k[0]*32/10), Xe[temp1]-(k[0]*34/10)])
           nose_base_y = np.array([Y2[20], Yb[temp2]-(k[1]*10/12), Yb[temp2]+(k[1]*13/12), Ye[temp1]+(k[1]*13/12), Ye[temp1]+(k[1]*13/12)])

           xref = Xe[temp1]
           yref = Ye[temp1]


           nose_bone_x = np.array([Xc[0] - .6*l[0], Xc[0] - .5*l[0], tdo[0] - 1.6*l[0], Xe[temp1]-(k[0]*45/13), tbo[0]-(k[0]*20/13), tto[0]+(k[0]*5/13), tto[0]+(k[0]*6/13), X2[temp2]+(k[0]*10/13), Xb[40]])
           nose_bone_y = np.array([jawo[1], tto[1], Ye[temp1]+(k[1]*30/12),ulo[1]+(k[1]*32/12),  ulo[1]+(k[1]*32/12), ulo[1]+(k[1]*65/12), ulo[1]+(k[1]*35/12), Yb[temp2]+(k[1]*15/12), Y2[40]])

           xxx0 = tt[0]+(l[0]*5/13)
           xxx1 = tt[0]-(l[0]*1/13)
           xxx12 =tt[0]-(l[0]*1/13)
           xxx2 = tt[0]-(l[0]*1/13)
           xxx3 = tt[0]-(l[0]*2/13)
           
           yyy0 =  tt[1]+(l[1]*0/12)
           yyy1 =  tt[1]-(l[1]*15/12)
           yyy12 = tt[1]-(l[1]*5/12)
           yyy2 = jaw[1]+(l[1]*32/13)
           yyy3 = jaw[1]+(l[1]*30/13)
           
           tbase_x = np.array([X3[-5], xxx0, xxx1, xxx12, xxx2, xxx3, jaw[0] - .5*l[0]])
           tbase_y = np.array([Y3[-5], yyy0, yyy1, yyy12, yyy2, yyy3, jaw[1]+(l[1]*22/13)])

        #    X6, Y6, N6 = bezier_curve(xPoints = palate_x, yPoints = palate_y)
           X7, Y7, N7 = bezier_curve(chin_x, chin_y)
           X8, Y8, N8 = bezier_curve(tline_x, tline_y)
           X9, Y9, N9 = bezier_curve(tbase_x, tbase_y)
           X10, Y10, N10 = bezier_curve(nose_base_x, nose_base_y)
           X11, Y11, N11 = bezier_curve(nose_bone_x, nose_bone_y)

           uv_diff = phon * 4
           
           aa0 = Xj[0]
           aa1 = Xj[0] - (k[0]*7/10)
           aa2 = Xj[0] - (k[0]*(9 + 2*uv_diff)/10)
           aa3 = Xj[0] - (k[0]*(6 + uv_diff)/10)
           aa4 = Xf[0] - (k[0]*5/10)
           aan = Xf[0]
           
           bb0 = Yj[0]
           bb1 = Yj[0] + (k[1]*(uv_diff/2)/10)
           bb2 = Yj[0] - (k[1]*(18 - uv_diff/2)/10)
           bb3 = Yj[0] - (k[1]*(20 - uv_diff/2)/10)
           bb4 = Yf[0] + (k[1]*(1 + uv_diff/2)/10)
           bbn = Yf[0]


           uvula_x = np.array([aa0, aa1, aa2, aa3,  aa4, aan])
           uvula_y = np.array([bb0, bb1, bb2, bb3,  bb4, bbn])

           X12, Y12, N12 = bezier_curve(uvula_x, uvula_y)

           xref2 = xref-(k[0]*45/13)
           yref2 = yref+(k[1]*30/12)
           head_x = np.array([tto[0]+(k[0]*14/13), tto[0]+(k[0]*5/13), tto[0]+(k[0]*7/13), xref2+(l[0]*50/11), xref2-(l[0]*50/8), xref2-(l[0]*50/6), tdo[0] - (tto[0] - tdo[0])*2.5, (9*xref2)/4])
           head_y = np.array([ulo[1]+(k[1]*50/12), ulo[1]+(k[1]*60/12), ulo[1]+(k[1]*70/12), yref2+(l[1]*10), yref2+(l[1]*8), yref2+(l[1]*2), jawo[1]+(l[1]*18/4), jawo[1] - .5*l[1]])
           X13, Y13, N13 = bezier_curve(head_x, head_y)

        #    utooth_front_x = np.array([ulo[0] - 5*l[0]/6, ulo[0] - 1*l[0]/6, ulo[0] - 4*l[0]/6])
        #    utooth_front_y = np.array([ulo[1] + 4*l[1]/4, ulo[1] - 1*l[1]/4, ulo[1] - 3*l[1]/4])
        #    X14, Y14, N14 = bezier_curve(utooth_front_x, utooth_front_y)

        #    utooth_back_x = np.array([ulo[0] - 10*l[0]/12, ulo[0] - 11*l[0]/12, ulo[0] - 7*l[0]/12, ulo[0] - 8*l[0]/12])
        #    utooth_back_y = np.array([ulo[1] + 4*l[1]/4, ulo[1] - 1*l[1]/4, ulo[1] + 2*l[1]/4, ulo[1] - 3*l[1]/4])
        #    X15, Y15, N15 = bezier_curve(utooth_back_x, utooth_back_y)
           
           xx1 = jaw[0] - .6*l[0]
           xx2 = jaw[0] - .4*l[0]
           xx3 = jaw[0] + .3*l[0]
           xx4 = jaw[0] + .1*l[0]
           xx5 = jaw[0] - 1.1*l[0]
           xx6 = jaw[0] - .7*l[0]
           xx7 = jaw[0] - .6*l[0]
           
           yy1 = jaw[1]+(l[1]*30/13)
           yy2 = jaw[1]+(l[1]*40/13)
           yy3 = jaw[1]+(l[1]*30/13)
           yy4 = jaw[1]-(l[1]*3/13)
           yy5 = jaw[1]-(l[1]*5/13)
           yy6 = jaw[1]+(l[1]*20/13)
           yy7 = jaw[1]+(l[1]*30/13)

           ltooth_base_x = np.array([xx1, xx2, xx3, xx4, xx5, xx6, xx7])
           ltooth_base_y = np.array([yy1, yy2, yy3, yy4, yy5, yy6, yy7])
           X16, Y16, N16 = bezier_curve(ltooth_base_x, ltooth_base_y)

           x1 = jaw[0] - l[0]*.45
           x2 = jaw[0] + l[0]*.215
           x3 = jaw[0] - l[0]*.215

           ltooth_front_x = np.array([x1, x2, x3])
           ltooth_front_y = np.array([jaw[1]+(l[1]*18/13), jaw[1]+(l[1]*28/13), jaw[1]+(l[1]*39/13)])
           X17, Y17, N17 = bezier_curve(ltooth_front_x, ltooth_front_y)
           
           x1 = jaw[0] - l[0]*.45
           x2 = jaw[0] - l[0]*.45
           x3 = jaw[0] - l[0]*.25
           x4 = jaw[0] - l[0]*.215

           ltooth_back_x = np.array([x1, x2, x3, x4])
           ltooth_back_y = np.array([jaw[1]+(l[1]*18/13), jaw[1]+(l[1]*33/13), jaw[1]+(l[1]*28/13), jaw[1]+(l[1]*39/13)])
           X18, Y18, N18 = bezier_curve(ltooth_back_x, ltooth_back_y)

           line1.set_data(X1, Y1) 
           line2.set_data(X2, Y2)
           line3.set_data(X3, Y3)
           line4.set_data(X4, Y4)
           line5.set_data(X5, Y5)
        #    line6.set_data(X6, Y6)
           line7.set_data(X7, Y7)
           line8.set_data(X8, Y8)
           line9.set_data(X9, Y9)
           line10.set_data(X10, Y10)
           line11.set_data(X11, Y11)
           line12.set_data(X12, Y12)
           line13.set_data(X13, Y13)
        #    line14.set_data(X14, Y14)
        #    line15.set_data(X15, Y15)
           line16.set_data(X16, Y16)
           line17.set_data(X17, Y17)
           line18.set_data(X18, Y18)

           args.painting1 = ax.fill(X3, Y3, color = 'pink', label = 'tongue')
           args.painting2 = ax.fill(np.concatenate((X8, X9)), np.concatenate((Y8, Y9)), color = 'pink', label = 'tongue')
           args.painting3 = ax.fill(X16, Y16, 'gainsboro', zorder = 3)
        #    args.painting4 = ax.fill(np.concatenate((X14, X15)), np.concatenate((Y14, Y15)), 'white', zorder = 4)
           args.painting5 = ax.fill(np.concatenate((X17, X18)), np.concatenate((Y17, Y18)), 'white', zorder = 4)
           args.painting6 = ax.fill(np.concatenate((X11, X13)), np.concatenate((Y11, Y13)), 'white', zorder = 4)

           return line1, line2, line3, line4, line5, line6, line7, line8, line9, line10, line11, line14, line15, line16,
           line17, line18

        start = time.time()
        anim2 = FuncAnimation(fig, animate, frames = len(ema), interval = 10)

        # anim2.save('video.mp4', writer = 'ffmpeg', fps = 30)
                
        f = 'video.mp4'
        writervideo = FFMpegWriter(fps=30) 
        anim2.save(f, writer=writervideo)

        my_bar.progress(85)

        text = TA.text_area("", "Merging audio and video ...")

        from moviepy.editor import *

        dur = time.time()
        # loading video gfg
        clip = VideoFileClip("video.mp4")

        my_bar.progress(90)

        # loading audio file
        audioclip = AudioFileClip("audio.wav")
        
        # adding audio to the video clip
        videoclip = clip.set_audio(audioclip)

        videoclip.write_videofile("output.mp4")

        my_bar.progress(95)

        video_file = open('output.mp4', 'rb')
        video_bytes = video_file.read()

        my_bar.progress(100)

        text = TA.text_area("", "Animation Complete!")

        st.video(video_bytes)

        gc.collect()