add audio2mesh_ar

This commit is contained in:
Sen Liang committed 2022-03-14 18:13:13 +08:00
1 parent ded9b27f71
commit e0f24982d1
46 files changed
+3917 -325

No files matched your search

+279
View File
@@ -0,0 +1,279 @@
{
"cells": [
{
"cell_type": "code",
"execution_count": 4,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Moviepy - Building video ../../coolTextEffects.avi.\n",
"Moviepy - Writing video ../../coolTextEffects.avi\n",
"\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
" "
]
},
{
"name": "stdout",
"output_type": "stream",
"text": [
"Moviepy - Done !\n",
"Moviepy - video ready ../../coolTextEffects.avi\n"
]
},
{
"name": "stderr",
"output_type": "stream",
"text": [
"\r"
]
}
],
"source": [
"import numpy as np\n",
"\n",
"from moviepy.editor import *\n",
"from moviepy.video.tools.segmenting import findObjects\n",
"\n",
"# WE CREATE THE TEXT THAT IS GOING TO MOVE, WE CENTER IT.\n",
"\n",
"screensize = (720,460)\n",
"txtClip = TextClip('Cool effect',color='white', font=\"Amiri-Bold\",\n",
" kerning = 5, fontsize=100)\n",
"cvc = CompositeVideoClip( [txtClip.set_pos('center')],\n",
" size=screensize)\n",
"\n",
"# THE NEXT FOUR FUNCTIONS DEFINE FOUR WAYS OF MOVING THE LETTERS\n",
"\n",
"\n",
"# helper function\n",
"rotMatrix = lambda a: np.array( [[np.cos(a),np.sin(a)], \n",
" [-np.sin(a),np.cos(a)]] )\n",
"\n",
"def vortex(screenpos,i,nletters):\n",
" d = lambda t : 1.0/(0.3+t**8) #damping\n",
" a = i*np.pi/ nletters # angle of the movement\n",
" v = rotMatrix(a).dot([-1,0])\n",
" if i%2 : v[1] = -v[1]\n",
" return lambda t: screenpos+400*d(t)*rotMatrix(0.5*d(t)*a).dot(v)\n",
" \n",
"def cascade(screenpos,i,nletters):\n",
" v = np.array([0,-1])\n",
" d = lambda t : 1 if t<0 else abs(np.sinc(t)/(1+t**4))\n",
" return lambda t: screenpos+v*400*d(t-0.15*i)\n",
"\n",
"def arrive(screenpos,i,nletters):\n",
" v = np.array([-1,0])\n",
" d = lambda t : max(0, 3-3*t)\n",
" return lambda t: screenpos-400*v*d(t-0.2*i)\n",
" \n",
"def vortexout(screenpos,i,nletters):\n",
" d = lambda t : max(0,t) #damping\n",
" a = i*np.pi/ nletters # angle of the movement\n",
" v = rotMatrix(a).dot([-1,0])\n",
" if i%2 : v[1] = -v[1]\n",
" return lambda t: screenpos+400*d(t-0.1*i)*rotMatrix(-0.2*d(t)*a).dot(v)\n",
"\n",
"\n",
"\n",
"# WE USE THE PLUGIN findObjects TO LOCATE AND SEPARATE EACH LETTER\n",
"\n",
"letters = findObjects(cvc) # a list of ImageClips\n",
"\n",
"\n",
"# WE ANIMATE THE LETTERS\n",
"\n",
"def moveLetters(letters, funcpos):\n",
" return [ letter.set_pos(funcpos(letter.screenpos,i,len(letters)))\n",
" for i,letter in enumerate(letters)]\n",
"\n",
"clips = [ CompositeVideoClip( moveLetters(letters,funcpos),\n",
" size = screensize).subclip(0,5)\n",
" for funcpos in [vortex, cascade, arrive, vortexout] ]\n",
"\n",
"# WE CONCATENATE EVERYTHING AND WRITE TO A FILE\n",
"\n",
"final_clip = concatenate_videoclips(clips)\n",
"final_clip.write_videofile('../../coolTextEffects.avi',fps=25,codec='mpeg4')"
]
},
{
"cell_type": "code",
"execution_count": 27,
"metadata": {},
"outputs": [],
"source": [
"\n",
"import resampy\n",
"import random\n",
"import numpy as np\n",
"import numpy as np\n",
"from scipy import interpolate\n",
"\n",
"\n",
"def resample_by_nsteps(feat, nsteps_tgt):\n",
" \"\"\" \n",
" feat, (nframes, ... ) (with source_fps)\n",
" \"\"\"\n",
"\n",
" nsteps_src = feat.shape[0]\n",
" data_idx = np.arange(0, nsteps_src, 1)\n",
" samp_idx = np.arange(0, nsteps_src-1, (nsteps_src - 1) / nsteps_tgt)\n",
" # print(f\"data_idx = {list(data_idx)}\")\n",
" # print(f\"smap_idx = {list(samp_idx)}\")\n",
" # sample\n",
" func = interpolate.interp1d(data_idx, y=feat, axis=0)\n",
" data_interpolate = func(samp_idx)\n",
"\n",
" return data_interpolate\n",
"\n",
"\n",
"# def convert_fps(feat, source_fps = 30, target_fps=25):\n",
"# \"\"\" \n",
"# feat, (nframes, ... ) (with source_fps)\n",
"# \"\"\"\n",
"\n",
"# nsize = feat.shape[0]\n",
"# data_idx = np.arange(0, nsize, 1)\n",
"# samp_idx = np.arange(0, nsize-1, 1.0*source_fps/target_fps)\n",
"\n",
"\n",
"# print(f\"1.0*source_fps/target_fps = {1.0*source_fps/target_fps}\")\n",
"\n",
"# # get sample idx\n",
"# # if source_fps > target_fps:\n",
"\n",
"# print(f\"data_idx = {list(data_idx)}\")\n",
"# print(f\"smap_idx = {list(samp_idx)}\")\n",
"\n",
"# # sample\n",
"# func = interpolate.interp1d(data_idx, y=feat, axis=0)\n",
"# data_interpolate = func(samp_idx)\n",
"\n",
"# del func\n",
"\n",
"# return data_interpolate\n",
"\n",
"\n",
"\n",
"def interpolate_feat(feat, input_rate, output_rate, nsteps_tgt=None):\n",
" \n",
" feat_shape = feat.shape\n",
" feat = feat.reshape(feat_shape[0], -1)\n",
"\n",
" dim_feat = feat.shape[1]\n",
"\n",
" nsteps_src = feat.shape[0]\n",
"\n",
"\n",
" seq_len = nsteps_src / float(input_rate)\n",
" if nsteps_tgt is None:\n",
" nsteps_tgt = int(seq_len * output_rate)\n",
"\n",
" input_timestamps = np.arange(nsteps_src) / float(input_rate)\n",
" output_timestamps = np.arange(nsteps_tgt) / float(output_rate)\n",
" output_feat = np.zeros((nsteps_tgt, dim_feat))\n",
"\n",
" for feat in range(dim_feat):\n",
" output_feat[:, feat] = np.interp(output_timestamps,\n",
" input_timestamps,\n",
" feat[:, feat])\n",
" return output_feat"
]
},
{
"cell_type": "code",
"execution_count": 2,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"(150, 1024)"
]
},
"execution_count": 2,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"\n",
"data.shape"
]
},
{
"cell_type": "code",
"execution_count": 35,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"nsteps = 150, random speed = 0.9, nsteps_speed = 135\n",
"data_idx = [0, 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]\n",
"smap_idx = [0.0, 0.8933333333333333, 1.7866666666666666, 2.6799999999999997, 3.5733333333333333, 4.466666666666667, 5.359999999999999, 6.253333333333333, 7.1466666666666665, 8.04, 8.933333333333334, 9.826666666666666, 10.719999999999999, 11.613333333333333, 12.506666666666666, 13.4, 14.293333333333333, 15.186666666666666, 16.08, 16.973333333333333, 17.866666666666667, 18.759999999999998, 19.653333333333332, 20.546666666666667, 21.439999999999998, 22.333333333333332, 23.226666666666667, 24.12, 25.013333333333332, 25.906666666666666, 26.8, 27.69333333333333, 28.586666666666666, 29.48, 30.37333333333333, 31.266666666666666, 32.16, 33.053333333333335, 33.946666666666665, 34.839999999999996, 35.733333333333334, 36.626666666666665, 37.519999999999996, 38.413333333333334, 39.306666666666665, 40.199999999999996, 41.093333333333334, 41.986666666666665, 42.879999999999995, 43.77333333333333, 44.666666666666664, 45.56, 46.45333333333333, 47.346666666666664, 48.24, 49.13333333333333, 50.026666666666664, 50.92, 51.81333333333333, 52.70666666666666, 53.6, 54.49333333333333, 55.38666666666666, 56.28, 57.17333333333333, 58.06666666666666, 58.96, 59.85333333333333, 60.74666666666666, 61.64, 62.53333333333333, 63.42666666666666, 64.32, 65.21333333333334, 66.10666666666667, 67.0, 67.89333333333333, 68.78666666666666, 69.67999999999999, 70.57333333333334, 71.46666666666667, 72.36, 73.25333333333333, 74.14666666666666, 75.03999999999999, 75.93333333333334, 76.82666666666667, 77.72, 78.61333333333333, 79.50666666666666, 80.39999999999999, 81.29333333333334, 82.18666666666667, 83.08, 83.97333333333333, 84.86666666666666, 85.75999999999999, 86.65333333333334, 87.54666666666667, 88.44, 89.33333333333333, 90.22666666666666, 91.12, 92.01333333333334, 92.90666666666667, 93.8, 94.69333333333333, 95.58666666666666, 96.48, 97.37333333333333, 98.26666666666667, 99.16, 100.05333333333333, 100.94666666666666, 101.84, 102.73333333333333, 103.62666666666667, 104.52, 105.41333333333333, 106.30666666666666, 107.2, 108.09333333333333, 108.98666666666666, 109.88, 110.77333333333333, 111.66666666666666, 112.56, 113.45333333333333, 114.34666666666666, 115.24, 116.13333333333333, 117.02666666666667, 117.92, 118.81333333333333, 119.70666666666666, 120.6, 121.49333333333333, 122.38666666666667, 123.28, 124.17333333333333, 125.06666666666666, 125.96, 126.85333333333332, 127.74666666666667, 128.64, 129.53333333333333, 130.42666666666668, 131.32, 132.21333333333334, 133.10666666666665]\n",
"data shape = (135, 1024), data_convert = (150, 1024), nstep_speed=135\n"
]
}
],
"source": [
"data = np.random.random(size=(150, 1024))\n",
"nsteps = data.shape[0]\n",
"\n",
"speed = random.sample({0.5, 0.7, 0.8, 0.9, 1.1, 1.2, 1.3, 1.4, 1.5},1)[0]\n",
"nsteps_speed = int(nsteps * speed)\n",
"\n",
"data = np.random.random(size=(nsteps_speed, 1024))\n",
"\n",
"print(f\"nsteps = {nsteps}, random speed = {speed}, nsteps_speed = {nsteps_speed}\")\n",
"\n",
"data_convert = resample_by_nsteps(data, nsteps_tgt=nsteps)\n",
"\n",
"print(f\"data shape = {data.shape}, data_convert = {data_convert.shape}, nstep_speed={nsteps_speed}\")\n",
"\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": []
}
],
"metadata": {
"interpreter": {
"hash": "7631c3454b293d59f44fb25da873c3d73daf8a0a859dfd3174f920d540a4d0d0"
},
"kernelspec": {
"display_name": "Python 3.8.8 ('base')",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.8"
},
"orig_nbformat": 4
},
"nbformat": 4,
"nbformat_minor": 2
}