{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"name":"python","version":"3.10.13","mimetype":"text/x-python","codemirror_mode":{"name":"ipython","version":3},"pygments_lexer":"ipython3","nbconvert_exporter":"python","file_extension":".py"},"kaggle":{"accelerator":"none","dataSources":[{"sourceId":70203,"databundleVersionId":8068726,"sourceType":"competition"},{"sourceId":8106133,"sourceType":"datasetVersion","datasetId":4787716}],"dockerImageVersionId":30684,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"code","source":"import numpy as np\nimport pandas as pd\nimport librosa\nimport cv2\nimport librosa.display\nimport matplotlib.pyplot as plt","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2024-04-26T00:48:39.961823Z","iopub.execute_input":"2024-04-26T00:48:39.962288Z","iopub.status.idle":"2024-04-26T00:48:39.968665Z","shell.execute_reply.started":"2024-04-26T00:48:39.962256Z","shell.execute_reply":"2024-04-26T00:48:39.966796Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"file_path = '/kaggle/input/birdclef-2024/train_audio/ashwoo2/XC186765.ogg'\nwaveform, sample_rate = librosa.load(file_path, sr=None)\nprint(waveform.shape, sample_rate)","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:48:39.973674Z","iopub.execute_input":"2024-04-26T00:48:39.974174Z","iopub.status.idle":"2024-04-26T00:48:40.016225Z","shell.execute_reply.started":"2024-04-26T00:48:39.974134Z","shell.execute_reply":"2024-04-26T00:48:40.014973Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"df = pd.read_csv('/kaggle/input/birdclef-2024/train_metadata.csv')\ndf.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:48:40.018733Z","iopub.execute_input":"2024-04-26T00:48:40.019232Z","iopub.status.idle":"2024-04-26T00:48:40.163719Z","shell.execute_reply.started":"2024-04-26T00:48:40.019182Z","shell.execute_reply":"2024-04-26T00:48:40.162462Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def get_center_5_seconds_waveform(waveform, sample_rate):\n    total_duration_seconds = waveform.shape[0] / sample_rate\n    center_duration_seconds = 5\n    center_start_index = int((total_duration_seconds - center_duration_seconds) / 2 * sample_rate)\n    center_end_index = center_start_index + int(center_duration_seconds * sample_rate)\n    center_waveform = waveform[center_start_index:center_end_index]\n\n    return center_waveform\n\ndef get_ith_5_seconds_waveform(waveform, sample_rate, j):\n    start = j * sample_rate * 5\n    end = start + sample_rate * 5\n    return waveform[start:end].copy()","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:48:40.165648Z","iopub.execute_input":"2024-04-26T00:48:40.166597Z","iopub.status.idle":"2024-04-26T00:48:40.175416Z","shell.execute_reply.started":"2024-04-26T00:48:40.166480Z","shell.execute_reply":"2024-04-26T00:48:40.174169Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"waveform = get_ith_5_seconds_waveform(waveform, sample_rate, 0)\nS = librosa.feature.melspectrogram(y=waveform, sr=sample_rate)\nS_db = librosa.power_to_db(S, ref=np.max)\nprint(S.shape)","metadata":{"execution":{"iopub.status.busy":"2024-04-26T01:38:56.909058Z","iopub.execute_input":"2024-04-26T01:38:56.909900Z","iopub.status.idle":"2024-04-26T01:38:56.932978Z","shell.execute_reply.started":"2024-04-26T01:38:56.909861Z","shell.execute_reply":"2024-04-26T01:38:56.930322Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"plt.figure(figsize=(10, 4))\nlibrosa.display.specshow(S_db, sr=sample_rate, x_axis='time', y_axis='mel')\nplt.colorbar(format='%+2.0f dB')\nplt.title('Default Mel-frequency spectrogram')\nplt.tight_layout()\nplt.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-26T00:48:40.223470Z","iopub.execute_input":"2024-04-26T00:48:40.224326Z","iopub.status.idle":"2024-04-26T00:48:40.794999Z","shell.execute_reply.started":"2024-04-26T00:48:40.224281Z","shell.execute_reply":"2024-04-26T00:48:40.793658Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"waveforms = {}\nprint('LEN DF: ', len(df))\nfor i in range(len(df)):\n    if i % 1000 == 0:\n        print(i)\n    row = df.iloc[i]\n    file_path = f'/kaggle/input/birdclef-2024/train_audio/{row[\"filename\"]}'\n    waveform, sample_rate = librosa.load(file_path, sr=None)\n    if waveform.shape[0] < 160000:\n        aa = np.zeros(160000)\n        aa[:waveform.shape[0]] = waveform\n        waveform = aa\n    num_5sec_batches = waveform.shape[0] // (32000 * 5)\n    for k in range(min(3, num_5sec_batches)):\n        aux = get_ith_5_seconds_waveform(waveform, 32000, k)\n        if aux.shape[0] < 160000:\n            continue\n        S = librosa.feature.melspectrogram(y=aux, sr=32000)\n        S = librosa.power_to_db(S, ref=np.max)\n        waveforms[(row['filename'], k*5)] = S","metadata":{"execution":{"iopub.status.busy":"2024-04-26T01:37:41.435590Z","iopub.execute_input":"2024-04-26T01:37:41.436038Z","iopub.status.idle":"2024-04-26T01:37:41.493178Z","shell.execute_reply.started":"2024-04-26T01:37:41.436003Z","shell.execute_reply":"2024-04-26T01:37:41.491999Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"np.save('birdclef_specs.npy', waveforms)","metadata":{"execution":{"iopub.status.busy":"2024-04-24T02:34:30.447292Z","iopub.execute_input":"2024-04-24T02:34:30.449454Z","iopub.status.idle":"2024-04-24T02:35:05.565012Z","shell.execute_reply.started":"2024-04-24T02:34:30.449418Z","shell.execute_reply":"2024-04-24T02:35:05.563434Z"},"trusted":true},"execution_count":null,"outputs":[]}]}