{"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":"code","source":"import numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        (os.path.join(dirname, filename))\n        \nimport seaborn as sns\nimport matplotlib.pyplot as plt\nfrom IPython.display import Audio\nimport librosa\nimport IPython.display as ipd","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-08-11T19:06:22.969398Z","iopub.execute_input":"2023-08-11T19:06:22.969805Z","iopub.status.idle":"2023-08-11T19:06:23.280271Z","shell.execute_reply.started":"2023-08-11T19:06:22.969774Z","shell.execute_reply":"2023-08-11T19:06:23.279426Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"**EDA & Data Preparation**","metadata":{}},{"cell_type":"code","source":"Dir = '/kaggle/input/birdclef-2023/'\ntaxonomy = pd.read_csv(Dir + 'eBird_Taxonomy_v2021.csv')\nmetadata = pd.read_csv(Dir + 'train_metadata.csv')\nsample   = pd.read_csv(Dir + 'sample_submission.csv')","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.282540Z","iopub.execute_input":"2023-08-11T19:06:23.283688Z","iopub.status.idle":"2023-08-11T19:06:23.463418Z","shell.execute_reply.started":"2023-08-11T19:06:23.283623Z","shell.execute_reply":"2023-08-11T19:06:23.462144Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"taxonomy.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.464852Z","iopub.execute_input":"2023-08-11T19:06:23.465207Z","iopub.status.idle":"2023-08-11T19:06:23.483471Z","shell.execute_reply.started":"2023-08-11T19:06:23.465163Z","shell.execute_reply":"2023-08-11T19:06:23.482429Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"taxonomy['SPECIES_CODE'].nunique()","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.485093Z","iopub.execute_input":"2023-08-11T19:06:23.485722Z","iopub.status.idle":"2023-08-11T19:06:23.504048Z","shell.execute_reply.started":"2023-08-11T19:06:23.485678Z","shell.execute_reply":"2023-08-11T19:06:23.502602Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"taxonomy.info()","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.508095Z","iopub.execute_input":"2023-08-11T19:06:23.508443Z","iopub.status.idle":"2023-08-11T19:06:23.563606Z","shell.execute_reply.started":"2023-08-11T19:06:23.508415Z","shell.execute_reply":"2023-08-11T19:06:23.562321Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"path = Dir + '/train_audio/abethr1/XC128013.ogg'\naudio_abe, sr_abe = librosa.load(path)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.565018Z","iopub.execute_input":"2023-08-11T19:06:23.565346Z","iopub.status.idle":"2023-08-11T19:06:23.652522Z","shell.execute_reply.started":"2023-08-11T19:06:23.565319Z","shell.execute_reply":"2023-08-11T19:06:23.651519Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"# Play the audio\nAudio(data=audio_abe, rate=sr_abe)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.653707Z","iopub.execute_input":"2023-08-11T19:06:23.654004Z","iopub.status.idle":"2023-08-11T19:06:23.716780Z","shell.execute_reply.started":"2023-08-11T19:06:23.653978Z","shell.execute_reply":"2023-08-11T19:06:23.715723Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"taxonomy.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.718722Z","iopub.execute_input":"2023-08-11T19:06:23.719116Z","iopub.status.idle":"2023-08-11T19:06:23.736437Z","shell.execute_reply.started":"2023-08-11T19:06:23.719083Z","shell.execute_reply":"2023-08-11T19:06:23.735433Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.737764Z","iopub.execute_input":"2023-08-11T19:06:23.738140Z","iopub.status.idle":"2023-08-11T19:06:23.757741Z","shell.execute_reply.started":"2023-08-11T19:06:23.738108Z","shell.execute_reply":"2023-08-11T19:06:23.756354Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"base_dir = '../input/birdclef-2023/train_audio'\nmetadata['full_path'] = base_dir+ '/' + metadata['filename']\nastfly = metadata[metadata['primary_label'] == \"abethr1\"].sample(1, random_state = 33)['full_path'].values[0]\ncasvir = metadata[metadata['primary_label'] == 'wfbeat1'].sample(1, random_state = 33)['full_path'].values[0]\nbirds= [\"astfly\", \"casvir\"]","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.759964Z","iopub.execute_input":"2023-08-11T19:06:23.760478Z","iopub.status.idle":"2023-08-11T19:06:23.794175Z","shell.execute_reply.started":"2023-08-11T19:06:23.760431Z","shell.execute_reply":"2023-08-11T19:06:23.792853Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ipd.Audio(astfly)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.795871Z","iopub.execute_input":"2023-08-11T19:06:23.796475Z","iopub.status.idle":"2023-08-11T19:06:23.812998Z","shell.execute_reply.started":"2023-08-11T19:06:23.796433Z","shell.execute_reply":"2023-08-11T19:06:23.811994Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"y_astfly, sr_astfly = librosa.load(astfly)\naudio_astfly, _ = librosa.effects.trim(y_astfly)\n\ny_casvir, sr_casvir = librosa.load(casvir)\naudio_casvir, _ = librosa.effects.trim(y_casvir)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:23.814275Z","iopub.execute_input":"2023-08-11T19:06:23.814601Z","iopub.status.idle":"2023-08-11T19:06:24.103844Z","shell.execute_reply.started":"2023-08-11T19:06:23.814573Z","shell.execute_reply":"2023-08-11T19:06:24.102586Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"fig, ax = plt.subplots(2, figsize = (16, 12))\nfig.suptitle('Sound Waves', fontsize=16)\n\nlibrosa.display.waveshow(y = audio_astfly, sr = sr_astfly, color = \"#A300F9\", ax=ax[0])\nlibrosa.display.waveshow(y = audio_casvir, sr = sr_casvir, color = \"#A300F9\", ax=ax[1])","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:24.105259Z","iopub.execute_input":"2023-08-11T19:06:24.105730Z","iopub.status.idle":"2023-08-11T19:06:25.560819Z","shell.execute_reply.started":"2023-08-11T19:06:24.105675Z","shell.execute_reply":"2023-08-11T19:06:25.559707Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"metadata.head()","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:25.565014Z","iopub.execute_input":"2023-08-11T19:06:25.565392Z","iopub.status.idle":"2023-08-11T19:06:25.588045Z","shell.execute_reply.started":"2023-08-11T19:06:25.565360Z","shell.execute_reply":"2023-08-11T19:06:25.586699Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Plotting the bird locations on the globe ","metadata":{}},{"cell_type":"code","source":"import plotly.express as px\nfig = px.scatter_geo(metadata ,lat='latitude',lon='longitude', hover_name=\"common_name\")\nfig.update_layout(title = 'World map', title_x=0.5)\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:25.590653Z","iopub.execute_input":"2023-08-11T19:06:25.591355Z","iopub.status.idle":"2023-08-11T19:06:25.754804Z","shell.execute_reply.started":"2023-08-11T19:06:25.591317Z","shell.execute_reply":"2023-08-11T19:06:25.753631Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Most common type of bird in the metadata","metadata":{}},{"cell_type":"code","source":"temp = pd.DataFrame(metadata.groupby('common_name').count()['type']).sort_values(by = 'type',ascending = False)[:50]\ntemp.head(20)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:25.756464Z","iopub.execute_input":"2023-08-11T19:06:25.756857Z","iopub.status.idle":"2023-08-11T19:06:25.824272Z","shell.execute_reply.started":"2023-08-11T19:06:25.756824Z","shell.execute_reply":"2023-08-11T19:06:25.823191Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"data = {\n        \"labels\": [],\n        \"mfcc\": []\n    }","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:25.825432Z","iopub.execute_input":"2023-08-11T19:06:25.825920Z","iopub.status.idle":"2023-08-11T19:06:25.831115Z","shell.execute_reply.started":"2023-08-11T19:06:25.825889Z","shell.execute_reply":"2023-08-11T19:06:25.830122Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import math\nnum_segments = 5\nSAMPLE_RATE = 22050\nTRACK_DURATION = 30\nnum_mfcc=13\nn_fft=2048\nhop_length=512\nSAMPLES_PER_TRACK = SAMPLE_RATE * TRACK_DURATION\nsamples_per_segment = int(SAMPLES_PER_TRACK / num_segments)\nnum_mfcc_vectors_per_segment = math.ceil(samples_per_segment / hop_length)","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:25.832356Z","iopub.execute_input":"2023-08-11T19:06:25.832711Z","iopub.status.idle":"2023-08-11T19:06:25.846002Z","shell.execute_reply.started":"2023-08-11T19:06:25.832684Z","shell.execute_reply":"2023-08-11T19:06:25.844998Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Extracting the Mel Freqquency Cepstrum Coefficients(MFCCs) for each category in the train audio, I will use these features to train the ML model. The mel frequency cepstral coefficients (MFCCs) of a signal are a small set of features (usually about 10–20) which concisely describe the overall shape of a spectral envelope. ","metadata":{}},{"cell_type":"code","source":"DIR_AUDIO = Dir + 'train_audio'\nx = os.listdir(DIR_AUDIO)\nx.sort()\nfor i in range(0,len(x)):\n    DIR_FILES = DIR_AUDIO +\"/\"+ x[i]\n    for j in os.listdir(DIR_FILES):\n        File = DIR_FILES + \"/\" +j\n        signal, sample_rate = librosa.load(File, mono=True, duration=30)\n        for d in range(num_segments):\n            start = samples_per_segment * d\n            finish = start + samples_per_segment\n            segment_signal = signal[start:finish]\n            mfcc = librosa.feature.mfcc(y = segment_signal, sr = sample_rate, n_mfcc=num_mfcc, n_fft=n_fft, hop_length=hop_length)\n            mfcc = mfcc.T\n            if len(mfcc) == num_mfcc_vectors_per_segment:\n                data[\"mfcc\"].append(mfcc.tolist())\n                data[\"labels\"].append(x[i])\n                #print(\"{}, segment:{}\".format(file_path, d+1))","metadata":{"execution":{"iopub.status.busy":"2023-08-11T19:06:25.846950Z","iopub.execute_input":"2023-08-11T19:06:25.847296Z","iopub.status.idle":"2023-08-11T20:07:10.236776Z","shell.execute_reply.started":"2023-08-11T19:06:25.847254Z","shell.execute_reply":"2023-08-11T20:07:10.235185Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"Saving the data to a csv file","metadata":{}},{"cell_type":"code","source":"df_category = pd.DataFrame(data)\ndf_category.to_csv(\"MFCCs_For_Birds.csv\")","metadata":{"execution":{"iopub.status.busy":"2023-08-11T20:07:10.239261Z","iopub.execute_input":"2023-08-11T20:07:10.240366Z","iopub.status.idle":"2023-08-11T20:12:59.079617Z","shell.execute_reply.started":"2023-08-11T20:07:10.240308Z","shell.execute_reply":"2023-08-11T20:12:59.078076Z"},"trusted":true},"execution_count":null,"outputs":[]}]}