{"cells":[{"metadata":{"_uuid":"8f2839f25d086af736a60e9eeb907d3b93b6e0e5","_cell_guid":"b1076dfc-b9ad-4769-8c92-a6c4dae69d19","trusted":true},"cell_type":"code","source":"import matplotlib.pyplot as plt\n# import pylab as plt\nfrom matplotlib.pyplot import specgram\nimport librosa\nimport numpy as np\nfrom IPython.display import Audio\nimport librosa\nimport librosa.display\nimport matplotlib.pyplot as plt\nimport pandas as pd\nimport os\nfrom pathlib import Path\nimport shutil\nimport sys\nimport warnings\nif not sys.warnoptions:\n    warnings.simplefilter(\"ignore\")\nfrom tqdm import tqdm\nimport gc\nfrom matplotlib import figure","execution_count":null,"outputs":[]},{"metadata":{"_uuid":"d629ff2d2480ee46fbb7e2d37f6b5fab8052498a","_cell_guid":"79c7e3d0-c299-4dcb-8224-4455121ee9b0","trusted":true},"cell_type":"code","source":"os.listdir('../input/birdsong-recognition/')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"def create_fold_spectrograms(audio_file_path,save_img_path ):\n    audio, sr = librosa.load(audio_file_path, sr =None)\n#     fig = figure.Figure(figsize=[0.72,0.72])\n    fig = plt.figure(figsize=[0.72,0.72])\n    ax = fig.add_subplot(111)\n    ax.axes.get_xaxis().set_visible(False)\n    ax.axes.get_yaxis().set_visible(False)\n    ax.set_frame_on(False)\n    filename  = Path(audio_file_path).name.replace('.mp3','.png').replace('.mp2','.png').replace('.aac','.png').replace('.wav','.png')\n    S = librosa.feature.melspectrogram(y=audio, sr=sr)\n    librosa.display.specshow(librosa.power_to_db(S, ref=np.max))\n    fig.savefig(os.path.join(save_img_path, filename), dpi=None, bbox_inches='tight',pad_inches=0)\n    fig.clear()\n    ax.cla()\n    plt.close(fig)\n    plt.close()\n    plt.close('all')\n    plt.cla()\n    fig.clf()\n    plt.clf()\n    plt.close()\n    gc.collect()\n    del S\n    ### Audio Sample\n#     Audio(audio, rate =sr)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"os.mkdir('train')","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"trainDir = Path('../input/birdsong-recognition/train_audio/')\nimgFolders = ['amegfi','amepip','amered','amerob','amewig','amtspa','annhum','astfly','baleag','barswa','belkin1','bewwre','bkbcuc','bkbmag1','bkpwar','bktspa','blujay','bongul','brdowl','brespa','brncre','brnthr','btywar','bushti','calgul','calqua','camwar','cangoo','casfin','casvir','cedwax','chispa','chiswi','chukar','comgol','comloo','commer','comnig','comred','comter','cowscj1','doccor','dusfly','easkin','easmea','eucdov','fiespa','fiscro','foxspa','gadwal','gcrfin','gnttow','gnwtea','gockin','gocspa','grcfly','greegr','greroa','grhowl','grnher','grycat','hamfly','hergul','horgre','houfin','juntit1','labwoo','larspa','leabit','lecthr','lesgol','lesyel','lewwoo','linspa','lobdow','louwat','mallar3','moublu','mouchi','moudov','norcar','normoc','norpin','nrwswa','olsfly','orcwar','ovenbi1','pasfly','perfal','phaino','pilwoo','pinsis','plsvir','pygnut','rebnut','redcro','redhea','renpha','reshaw','rethaw','rthhum','rudduc','rufgro','sagspa1','sagthr','savspa','saypho','scatan','scoori','sheowl','snobun','sonspa','swahaw','swaspa','swathr','tuftit','tunswa','veery','vigswa','wessan','westan','whfibi','whtswi','wilsni1','wooscj2','woothr','y00475','yebfly','yebsap','yelwar','yerwar']\nlen(imgFolders)","execution_count":null,"outputs":[]},{"metadata":{"trusted":true},"cell_type":"code","source":"%time\ntrainDir = Path('../input/birdsong-recognition/train_audio/')\nfor ebird_code in imgFolders:\n    gc.collect()\n    print(ebird_code)\n    if not os.path.exists(os.path.join('train',ebird_code)):\n        os.makedirs(os.path.join('train',ebird_code))\n    for audio in os.listdir(Path(trainDir/ebird_code)):\n        print(audio)\n        create_fold_spectrograms(audio_file_path = Path(trainDir/ebird_code/audio),\\\n                                 save_img_path = os.path.join('/kaggle/working/train',ebird_code))\n        gc.collect()","execution_count":null,"outputs":[]},{"metadata":{"_kg_hide-input":false,"trusted":true},"cell_type":"code","source":"shutil.make_archive('train_zipped', 'zip', 'train')","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}