{"cells":[{"metadata":{},"cell_type":"markdown","source":"### Reference\n\nIdea is from this notebook : https://www.kaggle.com/raghaw/panda-medium-resolution-dataset-25x256x256\n\nJoblib Parallel form : https://www.youtube.com/watch?v=Ny3O4VpACkc&list=PL98nY_tJQXZnoCDfHLo58tRHUyNvrRVzn&index=4","execution_count":null},{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import os\nimport cv2\nimport skimage.io\nfrom tqdm.notebook import tqdm\nimport zipfile\nimport pandas as pd\nimport numpy as np\nimport shutil\n\nfrom pydub import AudioSegment\nfrom joblib import Parallel, delayed","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"!mkdir -p /root/.kaggle/\n!cp ../input/mykaggleapi/kaggle.json /root/.kaggle/\n!chmod 600 /root/.kaggle/kaggle.json","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!mkdir -p /tmp/birdcall_dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls /tmp","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"data = '''{\n  \"title\": \"birdsong_recognition_wav_16000\",\n  \"id\": \"gopidurgaprasad/birdsong-recognition-wav-16000\",\n  \"licenses\": [\n    {\n      \"name\": \"CC0-1.0\"\n    }\n  ]\n}\n'''\ntext_file = open(\"/tmp/birdcall_dataset/dataset-metadata.json\", 'w+')\nn = text_file.write(data)\ntext_file.close()","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"TRAIN_CSV = \"../input/birdsong-recognition/train.csv\"\nROOT_PATH = \"../input/birdsong-recognition/train_audio\"\nOUTPUT_DIR = \"/tmp/birdcall_dataset/train_audio_wav_16000\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.mkdir(OUTPUT_DIR)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def save_fn(bird_code, filename):\n    \n    path = f\"{ROOT_PATH}/{bird_code}/{filename}\"\n    save_path = f\"{OUTPUT_DIR}/{bird_code}\"\n    if not os.path.exists(save_path):\n        os.makedirs(save_path, exist_ok=True)\n    \n    if os.path.exists(path):\n        try:\n            sound = AudioSegment.from_mp3(path)\n            sound = sound.set_frame_rate(16000)\n            sound.export(f\"{save_path}/{filename[:-4]}.wav\", format=\"wav\")\n        except:\n            print(path)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"train = pd.read_csv(TRAIN_CSV)\nbird_code_list = list(train.ebird_code.values)\nfilename_list = list(train.filename.values)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"Parallel(n_jobs=8, backend=\"multiprocessing\")(\n    delayed(save_fn)(bird_code, filename) for bird_code, filename in tqdm(zip(bird_code_list, filename_list), total=len(bird_code_list))\n)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!sleep 10","execution_count":null,"outputs":[]},{"metadata":{"trusted":true,"_kg_hide-output":true},"cell_type":"code","source":"!zip -r \"/tmp/birdcall_dataset/train_audio_wav_16000.zip\" \"/tmp/birdcall_dataset/train_audio_wav_16000/\"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!ls -l /tmp/birdcall_dataset/","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!kaggle datasets create -p /tmp/birdcall_dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"!rm -rf /tmp/birdcall_dataset","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"","execution_count":null,"outputs":[]}],"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":4,"nbformat_minor":4}