make mel spectogram and image cut.ipynb 5.15 KB
{"nbformat":4,"nbformat_minor":0,"metadata":{"colab":{"name":"make mel spectogram and image cut.ipynb","provenance":[],"collapsed_sections":[]},"kernelspec":{"name":"python3","display_name":"Python 3"},"accelerator":"GPU"},"cells":[{"cell_type":"code","metadata":{"id":"ZvJROk8j0qup","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":125},"executionInfo":{"status":"ok","timestamp":1591441994343,"user_tz":-540,"elapsed":25504,"user":{"displayName":"‍김선우[ 대학원석·박사통합과정재학 / 컴퓨터학과 ]","photoUrl":"","userId":"10839284711926757044"}},"outputId":"b05a9479-9768-4c12-90c6-32fdeec84942"},"source":["from google.colab import drive\n","drive.mount('/content/drive')"],"execution_count":null,"outputs":[{"output_type":"stream","text":["Go to this URL in a browser: https://accounts.google.com/o/oauth2/auth?client_id=947318989803-6bn6qk8qdgf4n4g3pfee6491hc0brc4i.apps.googleusercontent.com&redirect_uri=urn%3aietf%3awg%3aoauth%3a2.0%3aoob&response_type=code&scope=email%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdocs.test%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive%20https%3a%2f%2fwww.googleapis.com%2fauth%2fdrive.photos.readonly%20https%3a%2f%2fwww.googleapis.com%2fauth%2fpeopleapi.readonly\n","\n","Enter your authorization code:\n","··········\n","Mounted at /content/drive\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"1InEz1YyLetk","colab_type":"text"},"source":["Sound data로부터 Mel_spectogram plot"]},{"cell_type":"code","metadata":{"id":"N5FoxNMJ51w8","colab_type":"code","colab":{}},"source":["import os\n","import librosa\n","import librosa.display\n","import IPython.display\n","import numpy as np\n","import matplotlib.pyplot as plt\n","\n","%matplotlib inline\n","\n","audio_path = '../content/drive/Shared drives/반려견/데이터/sound_data'\n","specto_path = '../content/drive/Shared drives/반려견/데이터/mel_spectogram'\n","\n","barking_path = '/barking'\n","growling_path = '/growling'\n","howling_path = '/howling'\n","whimpering_path = '/whimpering'"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"U2FIwn9055SX","colab_type":"code","colab":{}},"source":["def create_spectogram_image(sound_path):\n","\taudio_data_path = audio_path + sound_path\n","\tspecto_data_path = specto_path + sound_path\n","\taudio_list = os.listdir(audio_data_path)\n","\tlistdir_number = len(audio_list)\n","\n","\tFIG_SIZE = (5, 4)\n","\tframe_length = 0.025\n","\tframe_stride = 0.010\n","\n","\t\n","\tprint('-'*50)\n","\tprint('Creating spectogram images...'+sound_path)\n","\tprint('-'*50)\n","\t\n","\tfor number in range(4000):\n","\t\twav_file_path = audio_data_path + sound_path + '_' + str(number+1) + '.wav'\n","\t\tspecto_sava_path = specto_data_path + sound_path + '_' + str(number+1) + '.png'\n","\n","\t\tif os.path.isfile(wav_file_path):\n","\t\t\ty, sr = librosa.load(wav_file_path, sr=22500)\n","\t\t\tinput_nfft = int(round(sr*frame_length))\n","\t\t\tinput_stride = int(round(sr*frame_stride))\n","\n","\t\t\tS = librosa.feature.melspectrogram(y=y, n_mels=40, n_fft=input_nfft, hop_length=input_stride)\n","\n","\t\t\tplt.figure(figsize=FIG_SIZE)\n","\t\t\tlibrosa.display.specshow(librosa.power_to_db(S, ref=np.max), y_axis='mel', sr=sr, hop_length=input_stride, x_axis='time')\n","\t\t\tplt.axis('off'), plt.xticks([]), plt.yticks([])\n","\t\t\tplt.tight_layout()\n","\t\t\tplt.savefig(specto_sava_path,\n","\t                bbox_inces='tight',\n","\t                pad_inches=0\n","\t                )\n","\t\t\tplt.close()\n","\t\t\tif((number+1)%100 == 0):\n","\t\t\t\tprint(str(number+1) + ' is finished')"],"execution_count":null,"outputs":[]},{"cell_type":"code","metadata":{"id":"Pimf_eZ0LV_a","colab_type":"code","colab":{"base_uri":"https://localhost:8080/","height":738},"outputId":"90778a3d-72cd-4640-ab97-3fb700b6c0a6"},"source":["if __name__ == \"__main__\":\n","\tcreate_spectogram_image(barking_path)\n","\tcreate_spectogram_image(growling_path)\n","\tcreate_spectogram_image(howling_path)\n","\tcreate_spectogram_image(whimpering_path)"],"execution_count":null,"outputs":[{"output_type":"stream","text":["--------------------------------------------------\n","Creating spectogram images.../barking\n","--------------------------------------------------\n","100 is finished\n","200 is finished\n","300 is finished\n","400 is finished\n","500 is finished\n","600 is finished\n","700 is finished\n","800 is finished\n","900 is finished\n","1000 is finished\n","1100 is finished\n","1200 is finished\n","1300 is finished\n","1400 is finished\n","1500 is finished\n","1600 is finished\n","1700 is finished\n","1800 is finished\n","1900 is finished\n","2000 is finished\n","2100 is finished\n","2200 is finished\n","2300 is finished\n","2400 is finished\n","2500 is finished\n","2600 is finished\n","2700 is finished\n","2800 is finished\n","2900 is finished\n","3000 is finished\n","3100 is finished\n","3200 is finished\n","3300 is finished\n","3400 is finished\n","3500 is finished\n","3600 is finished\n","3700 is finished\n","3800 is finished\n"],"name":"stdout"}]},{"cell_type":"markdown","metadata":{"id":"m7LQ9yBRaVWW","colab_type":"text"},"source":["Plot한 Melspectogram 양 옆 공백 Cutting"]}]}