{"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 tensorflow as tf\nimport tensorflow_hub as hub\nimport tensorflow_io as tfio\nimport soundfile as sf\n\nimport pandas as pd\nimport numpy as np\nimport librosa\nimport glob\nfrom scipy import signal\nimport random\n\nimport csv\nimport io\nimport os\n\nimport shutil","metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","execution":{"iopub.status.busy":"2023-03-31T12:01:11.638868Z","iopub.execute_input":"2023-03-31T12:01:11.639365Z","iopub.status.idle":"2023-03-31T12:01:11.646078Z","shell.execute_reply.started":"2023-03-31T12:01:11.639323Z","shell.execute_reply":"2023-03-31T12:01:11.644751Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"def frame_audio(\n      audio_array: np.ndarray,\n      window_size_s: float = 5.0,\n      hop_size_s: float = 5.0,\n      sample_rate = 32000,\n      ) -> np.ndarray:\n    \n    \"\"\"Helper function for framing audio for inference.\"\"\"\n    \"\"\" using tf.signal \"\"\"\n    if window_size_s is None or window_size_s < 0:\n        return audio_array[np.newaxis, :]\n    frame_length = int(window_size_s * sample_rate)\n    hop_length = int(hop_size_s * sample_rate)\n    framed_audio = tf.signal.frame(audio_array, frame_length, hop_length, pad_end=True)\n    return framed_audio\n\ndef ensure_sample_rate(waveform, original_sample_rate,\n                       desired_sample_rate=32000):\n    \"\"\"Resample waveform if required.\"\"\"\n    if original_sample_rate != desired_sample_rate:\n        waveform = tfio.audio.resample(waveform, original_sample_rate, desired_sample_rate)\n    return desired_sample_rate, waveform\ndef f_high(y,sr):\n    b,a = signal.butter(10, 2000/(sr/2), btype='highpass')\n    yf = signal.lfilter(b,a,y)\n    return yf","metadata":{"execution":{"iopub.status.busy":"2023-03-31T11:57:34.363570Z","iopub.execute_input":"2023-03-31T11:57:34.364048Z","iopub.status.idle":"2023-03-31T11:57:34.374330Z","shell.execute_reply.started":"2023-03-31T11:57:34.363991Z","shell.execute_reply":"2023-03-31T11:57:34.372982Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"## load file, ensure sample rate, and frame audio\ndef frame_file(file_name)-> np.ndarray:\n    audio, sample_rate = librosa.load(file_name,sr=32000)\n    audio2 = f_high(audio,32000)\n    #print(sample_rate)\n    framed_audio =  frame_audio(audio2)\n    return framed_audio\nframe = frame_file(\"/kaggle/input/birdclef-2023/train_audio/afghor1/XC156639.ogg\")\nlen(frame[:,0])\nframe[0,:].shape\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T11:59:02.332539Z","iopub.execute_input":"2023-03-31T11:59:02.333508Z","iopub.status.idle":"2023-03-31T11:59:02.435585Z","shell.execute_reply.started":"2023-03-31T11:59:02.333464Z","shell.execute_reply":"2023-03-31T11:59:02.434428Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"import os\nimport pandas as pd\n\ndef list_files(path):\n    # Create an empty list to store file paths and parent directory names\n    file_data = []\n\n    # Loop through all files in the directory path\n    for root, dirs, files in os.walk(path):\n        for file in files:\n            # Check if the file has a .ogg extension\n            if file.endswith('.ogg'):\n                # Get the full path of the file\n                full_path = os.path.join(root, file)\n                # Get the name of the parent directory of the file\n                parent_dir = os.path.basename(root)\n                # Append the file path and parent directory name to the list\n                #file_data.append((full_path, parent_dir))\n                file_data.append((full_path, parent_dir))\n\n    # Create a pandas DataFrame from the list of file paths and parent directory names\n    df = pd.DataFrame(file_data, columns=['full_path', 'parent_dir'])\n    return df\n\n# Call the list_files function with the directory path as an argument\ndf = list_files('/kaggle/input/birdclef-2023/train_audio/')\n## print unique value\nuv = df['parent_dir'].nunique()\nuv_count = df['parent_dir'].count()\nuv_value_counts = df['parent_dir'].value_counts()\n\n\npd.set_option('display.max_rows', 500)\nprint(uv)\nprint(uv_count)\n#print(uv_value_counts)","metadata":{"execution":{"iopub.status.busy":"2023-03-31T11:57:54.324183Z","iopub.execute_input":"2023-03-31T11:57:54.325535Z","iopub.status.idle":"2023-03-31T11:57:54.704161Z","shell.execute_reply.started":"2023-03-31T11:57:54.325483Z","shell.execute_reply":"2023-03-31T11:57:54.702714Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"new_row2 = pd.DataFrame(columns=[\"pri_label\",\"file_id\",\"frame_i\",\"frame_nb\"], index=[0])\nframes_5s = pd.DataFrame(columns=[\"pri_label\",\"file_id\",\"frame_i\",\"frame_nb\"], index=[0])\n\n\n## purge 5s_train_audio\ntry:\n    shutil.rmtree(\"/kaggle/working/5s_audio_frames\")\nexcept Exception as e:\n    print(\"An error occurred while deleting the directory:\", e)\n\nos.mkdir('/kaggle/working/5s_audio_frames/')\nrow_flag = 0;\n#genrate 5s files \n#for index, row in  df.head(10).iterrows():\nfor index, row in  df.iterrows():\n    print(index)\n    # extract file id \n    file_id = row['full_path'].split(\".ogg\")[0].split(\"/\")[-1]\n    # create ouput bird's label folder like the tree in  train_audio\n    if not(os.path.isdir('/kaggle/working/5s_audio_frames/'+row['parent_dir']+'/')):\n        os.mkdir('/kaggle/working/5s_audio_frames/'+row['parent_dir']+'/')\n    # load the file from input folder\n    #audio, sample_rate = librosa.load(row['full_path'])\n    # frame the audio \n    #print(\"step1 start\")\n    frames = frame_file(row['full_path'])\n    #print(\"step1 end\")\n    #save every frame to an audio file to be used later to genrate embedded vectors\n    for frame_count in range(len(frames[:,0])-1):\n    #for frame_count in range(1):\n        sf.write('/kaggle/working/5s_audio_frames/'+row['parent_dir']+'/'+file_id+'_'+str(frame_count)+'_'+str((len(frames[:,0])-1))+'.ogg', frames[frame_count,:], 32000, format='ogg')\n        \n        new_row2.loc[0][\"pri_label\"] = row['parent_dir']\n        new_row2.loc[0][\"file_id\"] = file_id\n        new_row2.loc[0][\"frame_i\"] = frame_count\n        new_row2.loc[0][\"frame_nb\"] = (len(frames[:,0])-1)\n        \n        if (row_flag == 0):\n            frames_5s.loc[0] = new_row2.loc[0]\n            row_flag = 1\n        else:\n            frames_5s = frames_5s.append( new_row2)\n        #row_embed_vect = row_embed_vect+1\n        ","metadata":{"execution":{"iopub.status.busy":"2023-03-31T12:14:44.172498Z","iopub.execute_input":"2023-03-31T12:14:44.172936Z","iopub.status.idle":"2023-03-31T12:14:47.189511Z","shell.execute_reply.started":"2023-03-31T12:14:44.172899Z","shell.execute_reply":"2023-03-31T12:14:47.188030Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frames_5s.to_csv(\"5s_frames_list.csv\")\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T12:14:51.616792Z","iopub.execute_input":"2023-03-31T12:14:51.617206Z","iopub.status.idle":"2023-03-31T12:14:51.629939Z","shell.execute_reply.started":"2023-03-31T12:14:51.617171Z","shell.execute_reply":"2023-03-31T12:14:51.628799Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"frames_5s\n","metadata":{"execution":{"iopub.status.busy":"2023-03-31T12:15:05.857050Z","iopub.execute_input":"2023-03-31T12:15:05.857515Z","iopub.status.idle":"2023-03-31T12:15:05.888992Z","shell.execute_reply.started":"2023-03-31T12:15:05.857473Z","shell.execute_reply":"2023-03-31T12:15:05.887713Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"","metadata":{},"execution_count":null,"outputs":[]}]}