{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"# Bengali One/Two Word(s) Mel_Spectrogram","metadata":{}},{"cell_type":"code","source":"import os\nimport pandas as pd\nimport numpy as np\nimport cv2\nimport matplotlib.pyplot as plt\nfrom matplotlib.font_manager import FontProperties\nfrom wordcloud import WordCloud \nfrom collections import Counter\nfrom tqdm import tqdm\nimport librosa","metadata":{"execution":{"iopub.status.busy":"2023-08-16T16:18:42.192352Z","iopub.execute_input":"2023-08-16T16:18:42.192825Z","iopub.status.idle":"2023-08-16T16:18:42.211445Z","shell.execute_reply.started":"2023-08-16T16:18:42.192792Z","shell.execute_reply":"2023-08-16T16:18:42.210235Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"train=pd.read_csv('/kaggle/input/bengaliai-speech/train.csv')\nprint(len(train))\n\nwords=[]\nfor i in tqdm(range(len(train))):\n    text=train.loc[i,'sentence']\n    tx=text.split(' ')\n    words+=tx\n    train.loc[i,'count']=len(tx)\n\nW=Counter(words)\nW2=list(W)[0:100]","metadata":{"execution":{"iopub.status.busy":"2023-08-16T16:18:42.213836Z","iopub.execute_input":"2023-08-16T16:18:42.215003Z","iopub.status.idle":"2023-08-16T16:21:16.549403Z","shell.execute_reply.started":"2023-08-16T16:18:42.214958Z","shell.execute_reply":"2023-08-16T16:21:16.548206Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(figsize=(10, 10))\nwordcloud = WordCloud(font_path='/kaggle/input/kalpurush-fonts/kalpurush-2.ttf', background_color=\"black\", width=1024, height=768).generate(\" \".join(W2))\nax.imshow(wordcloud, interpolation='bilinear')\nax.set_axis_off()\nax.set_title(\"WordCloud Top 100 Words\")\n\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-16T16:21:16.552156Z","iopub.execute_input":"2023-08-16T16:21:16.553301Z","iopub.status.idle":"2023-08-16T16:21:18.569154Z","shell.execute_reply.started":"2023-08-16T16:21:16.553230Z","shell.execute_reply":"2023-08-16T16:21:18.567916Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# One/Two Word(s) mel_spectrogram","metadata":{}},{"cell_type":"code","source":"train.to_csv('train_count.csv',index=False)\ntrain1=train[train['count']==1].reset_index(drop=True)\ntrain2=train[train['count']==2].reset_index(drop=True)\ndisplay(train1[0:5],train2[0:5])\ndir0='/kaggle/input/bengaliai-speech/train_mp3s'\nbengali_font = FontProperties(fname='/kaggle/input/kalpurush-fonts/kalpurush-2.ttf')","metadata":{"execution":{"iopub.status.busy":"2023-08-16T16:21:18.570961Z","iopub.execute_input":"2023-08-16T16:21:18.571638Z","iopub.status.idle":"2023-08-16T16:21:26.122843Z","shell.execute_reply.started":"2023-08-16T16:21:18.571595Z","shell.execute_reply":"2023-08-16T16:21:26.121627Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):#len(train1)\n    idx=train1.loc[i,'id']\n    path=os.path.join(dir0,idx+'.mp3')\n    txt=train1.loc[i,'sentence']\n    \n    y, sr = librosa.load(path)\n    mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)\n    log_mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)\n\n    img=log_mel_spectrogram\n    img=cv2.resize(np.array(img),dsize=(128,128))\n    X = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    X = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))\n\n    plt.imshow(X)\n    plt.title(txt,fontproperties=bengali_font)\n    plt.axis('off') \n    plt.savefig(idx+'.png', bbox_inches='tight', pad_inches=0)\n    plt.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-16T16:21:26.135669Z","iopub.execute_input":"2023-08-16T16:21:26.136241Z","iopub.status.idle":"2023-08-16T16:21:26.196065Z","shell.execute_reply.started":"2023-08-16T16:21:26.136110Z","shell.execute_reply":"2023-08-16T16:21:26.194492Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for i in range(5):#len(train2)\n    idx=train2.loc[i,'id']\n    path=os.path.join(dir0,idx+'.mp3')\n    txt=train2.loc[i,'sentence']\n    \n    y, sr = librosa.load(path)\n    mel_spectrogram = librosa.feature.melspectrogram(y=y, sr=sr, n_mels=128)\n    log_mel_spectrogram = librosa.power_to_db(mel_spectrogram, ref=np.max)\n\n    img=log_mel_spectrogram\n    img=cv2.resize(np.array(img),dsize=(128,128))\n    X = cv2.cvtColor(img, cv2.COLOR_BGR2RGB)\n    X = (X - X.min(axis=0)) / (X.max(axis=0) - X.min(axis=0))\n\n    plt.imshow(X)\n    plt.title(txt,fontproperties=bengali_font)\n    plt.axis('off') \n    plt.savefig(idx+'.png', bbox_inches='tight', pad_inches=0)\n    plt.show()","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}