{"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"}],"dockerImageVersionId":30673,"isInternetEnabled":true,"language":"python","sourceType":"notebook","isGpuEnabled":false}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Published on April 03, 2024. By Marília Prata, mpwolke","metadata":{}},{"cell_type":"code","source":"# This Python 3 environment comes with many helpful analytics libraries installed\n# It is defined by the kaggle/python Docker image: https://github.com/kaggle/docker-python\n# For example, here's several helpful packages to load\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport plotly.graph_objs as go\nimport plotly.offline as py\nimport plotly.express as px\n\n#Ignore warnings\nimport warnings\nwarnings.filterwarnings('ignore')\n\n# Input data files are available in the read-only \"../input/\" directory\n# For example, running this (by clicking run or pressing Shift+Enter) will list all files under the input directory\n\nimport os\nfor dirname, _, filenames in os.walk('/kaggle/input'):\n    for filename in filenames:\n        print(os.path.join(dirname, filename))\n\n# You can write up to 20GB to the current directory (/kaggle/working/) that gets preserved as output when you create a version using \"Save & Run All\" \n# You can also write temporary files to /kaggle/temp/, but they won't be saved outside of the current session","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","_kg_hide-output":true,"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-04T01:52:19.359874Z","iopub.execute_input":"2024-04-04T01:52:19.360244Z","iopub.status.idle":"2024-04-04T01:52:40.561311Z","shell.execute_reply.started":"2024-04-04T01:52:19.360216Z","shell.execute_reply":"2024-04-04T01:52:40.560196Z"},"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Competition Citation:\n\n@misc{birdclef-2024,\n\n    author = {HCL-Rantig, Holger Klinck, Maggie, Sohier Dane, Stefan Kahl, Tom Denton},\n    \n    title = {BirdCLEF 2024},\n    publisher = {Kaggle},\n    year = {2024},\n    url = {https://kaggle.com/competitions/birdclef-2024}\n}","metadata":{}},{"cell_type":"markdown","source":"![](https://evolecol.weebly.com/uploads/3/1/4/4/31446951/picture3_orig.png)https://evolecol.weebly.com/blog/2017-western-ghats-birds","metadata":{}},{"cell_type":"markdown","source":"\"For this competition, you'll use your machine-learning skills to identify under-studied Indian bird species by sound. Specifically, you'll develop computational solutions to process continuous audio data and recognize the species by their calls. The best entries will be able to train reliable classifiers with limited training data. If successful, you'll help advance ongoing efforts to protect avian biodiversity in the Western Ghats, India, including those led by V. V. Robin's Lab at IISER Tirupati.\"\n\nhttps://www.kaggle.com/competitions/birdclef-2024/overview","metadata":{}},{"cell_type":"code","source":"#By Paulo Junqueira https://www.kaggle.com/code/paulojunqueira/pew-pew-overview-birdclef-2023/notebook\n\nimport numpy as np # linear algebra\nimport pandas as pd # data processing, CSV file I/O (e.g. pd.read_csv)\n\nimport matplotlib.pyplot as plt\nimport seaborn as sns\nimport re\nimport librosa\nimport librosa.display\n\nimport IPython.display as ipd\nfrom urllib.request import urlopen\nfrom datetime import datetime, timedelta\n\nimport plotly.graph_objects as go\nfrom scipy.interpolate import interp1d \nfrom bs4 import BeautifulSoup as bs\nimport librosa\nimport librosa.display\nimport IPython.display as ipd\n# import noisereduce as nr\n\nfrom tqdm.notebook import tqdm\n# Pytorch\nimport torch\nimport torchaudio\nimport requests\nfrom PIL import Image\n\ndef get_link(url, name):\n    res = requests.get(url)\n    soup = bs(res.content)\n    external_link = soup.find_all(['a'], href = True, text = name)\n    url_2 = re.findall(r'\\\".*?\\\"', str(external_link[0]))[0].replace('\"', '')\n    return url_2\ndef wiki_link(url, name):\n    res = requests.get(url_3)\n    soup = bs(res.content)\n    external_link = soup.find_all(\"img\", src=re.compile(name))\n    \n    img = 'https:' +  find_between(str(external_link),'src=', ' ').replace('\"', '')\n    return img\n\ndef find_between( s, first, last ):\n    try:\n        start = s.index( first ) + len( first )\n        end = s.index( last, start )\n        return s[start:end]\n    except ValueError:\n        return \"\"\n    \ndef get_text(url, len_text):\n    res = requests.get(url)\n    soup = bs(res.content)\n    text = ''\n    for paragraph in soup.find_all('p'):\n        text += paragraph.text\n        \n    return text.split('\\n')[1:len_text+1]\n    \n\ndef get_spectogram(path):\n    \n    \n    data, sample_rate = librosa.load(path)\n    \n    stft = librosa.stft(data, n_fft=CFG.n_fft, hop_length=CFG.hop_length)\n    spectrogram = np.abs(stft)\n    x = librosa.amplitude_to_db(spectrogram)\n    \n    \n    #mel spectogram\n    transfomer = torchaudio.transforms.MelSpectrogram(sample_rate = sample_rate,\n                                                     n_fft = CFG.n_fft, \n                                                     win_length = CFG.win_length,\n                                                     n_mels = CFG.n_mels,\n                                                     f_min = CFG.f_min,\n                                                     f_max = CFG.f_max ).double()\n\n\n    wave = torch.from_numpy(data.copy())\n    mel_spectrogram = transfomer(wave)\n    \n    #PCEN melspectogram\n    pcen_spectogram = librosa.pcen(np.array(mel_spectrogram) * (2 ** 31), \n                                  eps = 1e-6,\n                                  gain = 0.8,\n                                  power = 0.25,\n                                  bias = 10, \n                                  sr = sample_rate,\n                                  hop_length = CFG.hop_length)\n    \n    \n    fig, ax = plt.subplots(ncols = 2, nrows = 1, figsize = (18,5))\n    \n    librosa.display.specshow(x, sr=sample_rate, hop_length=CFG.hop_length,ax = ax[0])\n    ax[0].set_title(\"Spectrogram - STFT\")\n    librosa.display.waveshow(data,ax = ax[1])\n    plt.show()\n    \n    fig, ax1 = plt.subplots(ncols = 2, nrows = 1, figsize = (18,5))\n    ax1[0].imshow(librosa.amplitude_to_db(mel_spectrogram))\n    ax1[0].set_title(\"Melspectogram\")\n    ax1[1].imshow((pcen_spectogram))\n    ax1[1].set_title(\"PCEN-Melspectogram\")\n    \n    \n    plt.show()\n\nsns.set_style(\"darkgrid\", {\"grid.color\": \".6\", \"grid.linestyle\": \":\"})","metadata":{"_kg_hide-input":true,"execution":{"iopub.status.busy":"2024-04-04T01:54:08.284201Z","iopub.execute_input":"2024-04-04T01:54:08.285467Z","iopub.status.idle":"2024-04-04T01:54:12.928394Z","shell.execute_reply.started":"2024-04-04T01:54:08.285422Z","shell.execute_reply":"2024-04-04T01:54:12.927537Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Paulo Junqueira https://www.kaggle.com/code/paulojunqueira/pew-pew-overview-birdclef-2023/notebook\n\nclass CFG():\n    '''Configuration File'''\n    n_fft = 2048\n    frame_size = 5 # seg\n    frame_step = 5  # seg\n\n    hop_length  = 128\n    frame_size_t  = 256\n    n_mels     = 250\n    win_length = 1024\n    f_min      = 500\n    f_max      = 9000","metadata":{"execution":{"iopub.status.busy":"2024-04-04T01:29:11.448509Z","iopub.execute_input":"2024-04-04T01:29:11.449544Z","iopub.status.idle":"2024-04-04T01:29:11.456184Z","shell.execute_reply.started":"2024-04-04T01:29:11.449502Z","shell.execute_reply":"2024-04-04T01:29:11.454677Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta = pd.read_csv('../input/birdclef-2024/train_metadata.csv')\nmeta['secondary_labels'] = meta['secondary_labels'].apply(lambda x: re.findall(r\"'(\\w+)'\", x))\nmeta['len_sec_labels'] = meta['secondary_labels'].map(len)\nmeta.head(2)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T01:54:36.354338Z","iopub.execute_input":"2024-04-04T01:54:36.355201Z","iopub.status.idle":"2024-04-04T01:54:36.649925Z","shell.execute_reply.started":"2024-04-04T01:54:36.355163Z","shell.execute_reply":"2024-04-04T01:54:36.648582Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Data Overview","metadata":{}},{"cell_type":"code","source":"meta.info()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:25:09.224207Z","iopub.execute_input":"2024-04-04T00:25:09.224689Z","iopub.status.idle":"2024-04-04T00:25:09.257829Z","shell.execute_reply.started":"2024-04-04T00:25:09.224658Z","shell.execute_reply":"2024-04-04T00:25:09.256333Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"meta.shape","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:25:49.14273Z","iopub.execute_input":"2024-04-04T00:25:49.1438Z","iopub.status.idle":"2024-04-04T00:25:49.153201Z","shell.execute_reply.started":"2024-04-04T00:25:49.143759Z","shell.execute_reply":"2024-04-04T00:25:49.151823Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Paulo Junqueira https://www.kaggle.com/code/paulojunqueira/pew-pew-overview-birdclef-2023/notebook\n\ndf_plot = meta.groupby(['primary_label','latitude', 'longitude']).count().reset_index()[['primary_label','scientific_name','latitude', 'longitude']].rename(columns = {'scientific_name':'count'})\nmeta_2 = meta.merge(df_plot, on = ['primary_label','latitude', 'longitude'], how = 'left').dropna(subset = ['count'])\nmeta_2['count'] = meta_2['count'].astype('int')\n\nvalues_list = meta_2['count'].values.tolist()\n\ninterpolation = interp1d([1, max(values_list)], [3,20])\nradius = interpolation(values_list)\nfig = go.Figure(go.Densitymapbox(lat =meta_2['latitude'],lon = meta_2['longitude'], radius = radius,z = meta_2['count']))\n\nfig.update_layout(mapbox_style=\"open-street-map\",height = 800,\n                  mapbox = {\n                          'center': {'lat': 0, \n                          'lon': 0},\n                      'zoom':0\n                  })\nfig.show()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:40:56.801691Z","iopub.execute_input":"2024-04-04T00:40:56.802136Z","iopub.status.idle":"2024-04-04T00:40:57.512907Z","shell.execute_reply.started":"2024-04-04T00:40:56.802105Z","shell.execute_reply":"2024-04-04T00:40:57.511488Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Taxonomic Ranks\n\nThere are seven main taxonomic ranks:\n\nKingdom,\n\nPhylum or division,\n\nClass,\n\nOrder,\n\nFamily,\n\nGenus,\n\nSpecies.\n\nhttps://en.wikipedia.org/wiki/Taxonomic_rank","metadata":{}},{"cell_type":"code","source":"eBird = pd.read_csv(\"/kaggle/input/birdclef-2024/eBird_Taxonomy_v2021.csv\")\npd.set_option('display.max_columns', None)\neBird.head()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:46:33.135255Z","iopub.execute_input":"2024-04-04T00:46:33.135726Z","iopub.status.idle":"2024-04-04T00:46:33.232459Z","shell.execute_reply.started":"2024-04-04T00:46:33.135694Z","shell.execute_reply":"2024-04-04T00:46:33.231468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"ax = eBird['ORDER1'].value_counts()[:20].plot.barh(figsize=(16, 8), color='green')\nax.set_title('Western Ghats Species Order 1', size=18, color='orange')\nax.set_ylabel('Order 1', size=10)\nax.set_xlabel('Count', size=10);","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:48:52.747854Z","iopub.execute_input":"2024-04-04T00:48:52.748376Z","iopub.status.idle":"2024-04-04T00:48:53.356635Z","shell.execute_reply.started":"2024-04-04T00:48:52.748339Z","shell.execute_reply":"2024-04-04T00:48:53.355472Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"text_cols = ['SPECIES_CODE', 'SCI_NAME', 'ORDER1', 'FAMILY']\n\nfrom wordcloud import WordCloud, STOPWORDS\n\nwc = WordCloud(stopwords = set(list(STOPWORDS) + ['|']), random_state = 42, background_color='orange', colormap=\"Purples\")\nfig, axes = plt.subplots(2, 2, figsize=(20, 12))\naxes = [ax for axes_row in axes for ax in axes_row]\n\nfor i, c in enumerate(text_cols):\n  op = wc.generate(str(eBird[c]))\n  _ = axes[i].imshow(op)\n  _ = axes[i].set_title(c.upper(), fontsize=24)\n  _ = axes[i].axis('off')","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:49:24.757995Z","iopub.execute_input":"2024-04-04T00:49:24.758732Z","iopub.status.idle":"2024-04-04T00:49:26.597625Z","shell.execute_reply.started":"2024-04-04T00:49:24.758694Z","shell.execute_reply":"2024-04-04T00:49:26.596449Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Western Ghats Species Category","metadata":{}},{"cell_type":"code","source":"ax = eBird['CATEGORY'].value_counts()[:20].plot.barh(figsize=(16, 8), color='orange')\nax.set_title('Western Ghats Species Category', size=18, color='green')\nax.set_ylabel('Category', size=10)\nax.set_xlabel('Count', size=10);","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:50:11.281799Z","iopub.execute_input":"2024-04-04T00:50:11.282708Z","iopub.status.idle":"2024-04-04T00:50:11.725871Z","shell.execute_reply.started":"2024-04-04T00:50:11.282669Z","shell.execute_reply":"2024-04-04T00:50:11.724553Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Western Ghats Species Family","metadata":{}},{"cell_type":"code","source":"ax = eBird['FAMILY'].value_counts()[:20].plot.barh(figsize=(16, 8), color='purple')\nax.set_title('Western Ghats Species Family', size=18, color='red')\nax.set_ylabel('Family', size=10)\nax.set_xlabel('Count', size=10);","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:51:13.867483Z","iopub.execute_input":"2024-04-04T00:51:13.867999Z","iopub.status.idle":"2024-04-04T00:51:14.566069Z","shell.execute_reply.started":"2024-04-04T00:51:13.867961Z","shell.execute_reply":"2024-04-04T00:51:14.564644Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Adapted from Ben Jenkins https://www.kaggle.com/code/benjenkins96/identify-eastern-african-bird-species-by-sound\n\n# Plot a scatterplot of the latitude and longitude values\n\nax= meta.plot.scatter(x='longitude', y='latitude', alpha=0.1,figsize=(12, 8))\nax.set_title('Geographic Distribution of Recordings',size=18, color='red')\nax.set_xlabel('Longitude', size=10)\nax.set_ylabel('Latitude', size =10);","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:52:34.34307Z","iopub.execute_input":"2024-04-04T00:52:34.343584Z","iopub.status.idle":"2024-04-04T00:52:34.937739Z","shell.execute_reply.started":"2024-04-04T00:52:34.343544Z","shell.execute_reply":"2024-04-04T00:52:34.936336Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Bird Sound Recordists","metadata":{}},{"cell_type":"code","source":"ax = meta['author'].value_counts()[:10].plot.barh(figsize=(16, 8), color='green')\nax.set_title('Bird Sound Recordists', size=18, color='red')\nax.set_ylabel('Author', size=10)\nax.set_xlabel('Count', size=10);","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:53:43.515192Z","iopub.execute_input":"2024-04-04T00:53:43.515646Z","iopub.status.idle":"2024-04-04T00:53:44.039283Z","shell.execute_reply.started":"2024-04-04T00:53:43.515615Z","shell.execute_reply":"2024-04-04T00:53:44.037555Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#By Ben Jenkins https://www.kaggle.com/code/benjenkins96/identify-eastern-african-bird-species-by-sound\n\n# Set up a figure with subplots\nfig, axs = plt.subplots(2, 2, figsize=(12, 8))\n\n# Plot a histogram of the latitude values\nmeta['latitude'].hist(bins=50, ax=axs[0, 0])\naxs[0, 0].set_title('Distribution of Latitude', color='red')\naxs[0, 0].set_xlabel('Latitude', color='red')\naxs[0, 0].set_ylabel('Count', color='red')\n\n# Plot a histogram of the longitude values\nmeta['longitude'].hist(bins=50, ax=axs[0, 1])\naxs[0, 1].set_title('Distribution of Longitude', color='red')\naxs[0, 1].set_xlabel('Longitude', color='red')\naxs[0, 1].set_ylabel('Count', color='red')\n\n# Plot a scatterplot of the latitude and longitude values\nmeta.plot.scatter(x='longitude', y='latitude', alpha=0.1, ax=axs[1, 0])\naxs[1, 0].set_title('Geographic Distribution of Recordings', color='red')\naxs[1, 0].set_xlabel('Longitude', color='red')\naxs[1, 0].set_ylabel('Latitude', color='red')\n\n# Print the top 10 authors with the most recordings\nmeta['author'].value_counts().nlargest(10).plot.barh(ax=axs[1, 1])\naxs[1, 1].set_title('Top 10 Authors with the Most Recordings', color='red')\naxs[1, 1].set_xlabel('Count', color='red')\naxs[1, 1].set_ylabel('Author', color='red')\n\n# Adjust the layout of the subplots\nplt.tight_layout()","metadata":{"execution":{"iopub.status.busy":"2024-04-04T00:54:59.012275Z","iopub.execute_input":"2024-04-04T00:54:59.012811Z","iopub.status.idle":"2024-04-04T00:55:00.945148Z","shell.execute_reply.started":"2024-04-04T00:54:59.012778Z","shell.execute_reply":"2024-04-04T00:55:00.943785Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sayantan Mazumdar https://www.kaggle.com/swaralipibose/converting-audio-to-spectogram-noise-image-data/notebook\n\n!pip install soundfile -q","metadata":{"_kg_hide-input":false,"execution":{"iopub.status.busy":"2024-04-04T00:59:57.115184Z","iopub.execute_input":"2024-04-04T00:59:57.115721Z","iopub.status.idle":"2024-04-04T01:00:11.018516Z","shell.execute_reply.started":"2024-04-04T00:59:57.11569Z","shell.execute_reply":"2024-04-04T01:00:11.016671Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sayantan Mazumdar https://www.kaggle.com/swaralipibose/converting-audio-to-spectogram-noise-image-data/notebook\n\n!pip install noisereduce","metadata":{"execution":{"iopub.status.busy":"2024-04-04T01:00:30.103702Z","iopub.execute_input":"2024-04-04T01:00:30.104164Z","iopub.status.idle":"2024-04-04T01:00:43.306418Z","shell.execute_reply.started":"2024-04-04T01:00:30.10413Z","shell.execute_reply":"2024-04-04T01:00:43.305072Z"},"_kg_hide-output":true,"collapsed":true,"jupyter":{"outputs_hidden":true},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Convert Audio to Frequency","metadata":{}},{"cell_type":"code","source":"#Code by Sayantan Mazumdar https://www.kaggle.com/swaralipibose/converting-audio-to-spectogram-noise-image-data/notebook\n\n#Convert Audio to Frequency\n\nimport soundfile as sf\nfreq,rate=sf.read('../input/birdclef-2024/train_audio/commoo3/XC149725.ogg')\nimport plotly.express as px\nimport numpy as np\npx.line(x=np.array(list(range(len(freq)))),y=freq)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T01:04:00.077746Z","iopub.execute_input":"2024-04-04T01:04:00.078306Z","iopub.status.idle":"2024-04-04T01:04:01.880515Z","shell.execute_reply.started":"2024-04-04T01:04:00.078267Z","shell.execute_reply":"2024-04-04T01:04:01.878151Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Display ogg Audio","metadata":{}},{"cell_type":"code","source":"#Code by Sayantan Mazumdar https://www.kaggle.com/swaralipibose/converting-audio-to-spectogram-noise-image-data/notebook\n\nimport IPython\nIPython.display.Audio(\"../input/birdclef-2024/train_audio/commoo3/XC149725.ogg\")","metadata":{"execution":{"iopub.status.busy":"2024-04-04T01:05:49.489775Z","iopub.execute_input":"2024-04-04T01:05:49.490275Z","iopub.status.idle":"2024-04-04T01:05:49.50449Z","shell.execute_reply.started":"2024-04-04T01:05:49.490244Z","shell.execute_reply":"2024-04-04T01:05:49.503352Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sayantan Mazumdar https://www.kaggle.com/swaralipibose/converting-audio-to-spectogram-noise-image-data/notebook\n\n!pip install tensorflow-io","metadata":{"execution":{"iopub.status.busy":"2024-04-04T01:06:18.538328Z","iopub.execute_input":"2024-04-04T01:06:18.539121Z","iopub.status.idle":"2024-04-04T01:06:31.024309Z","shell.execute_reply.started":"2024-04-04T01:06:18.539081Z","shell.execute_reply":"2024-04-04T01:06:31.022503Z"},"_kg_hide-output":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"#Code by Sayantan Mazumdar https://www.kaggle.com/swaralipibose/converting-audio-to-spectogram-noise-image-data/notebook\n\nimport tensorflow_io as tfio\nimport tensorflow as tf\n\nimport noisereduce as nr\nreduced_noise=nr.reduce_noise(y=freq,sr=rate)","metadata":{"execution":{"iopub.status.busy":"2024-04-04T01:07:05.814767Z","iopub.execute_input":"2024-04-04T01:07:05.8153Z","iopub.status.idle":"2024-04-04T01:07:35.909224Z","shell.execute_reply.started":"2024-04-04T01:07:05.815263Z","shell.execute_reply":"2024-04-04T01:07:35.907468Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Convert an ogg file to spectogram","metadata":{}},{"cell_type":"code","source":"#Code by Sayantan Mazumdar https://www.kaggle.com/swaralipibose/converting-audio-to-spectogram-noise-image-data/notebook\n\ndef read(pth,reduce_noise):\n    freq,rate=sf.read(pth)\n    if reduce_noise:\n        freq=nr.reduce_noise(y=freq,sr=rate)\n    # Convert to spectrogram\n    spectrogram = tfio.audio.spectrogram(\n    reduced_noise, nfft=3600, window=256, stride=256)\n    return tf.math.log(spectrogram).numpy()\nplt.imshow(read('../input/birdclef-2024/train_audio/commoo3/XC149725.ogg',True));","metadata":{"execution":{"iopub.status.busy":"2024-04-04T01:08:14.016371Z","iopub.execute_input":"2024-04-04T01:08:14.017767Z","iopub.status.idle":"2024-04-04T01:08:15.094112Z","shell.execute_reply.started":"2024-04-04T01:08:14.01771Z","shell.execute_reply":"2024-04-04T01:08:15.092539Z"},"_kg_hide-input":true,"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"#Acknowledgements:\n\nPaulo Junqueira https://www.kaggle.com/code/paulojunqueira/pew-pew-overview-birdclef-2023/notebook\n\nBen Jenkins https://www.kaggle.com/code/benjenkins96/identify-eastern-african-bird-species-by-sound\n\nSayantan Mazumdar https://www.kaggle.com/swaralipibose/converting-audio-to-spectogram-noise-image-data/notebook","metadata":{}}]}