[PYTHON] I summarized the procedure for starting machine learning and classification using sound (Reference: Practical machine learning system) ヽ (゚ * ヽ) (ノ * ゚) No wow

Hello everybody, the game play-by-play video: tv: fun: smile: Now that we have become cold Be careful on the physical condition management: tea: Pazudora, Monsuto, Gacha: gem: Did you draw? How was Pazudora's God Festival? Green Odin is out: +1:

By the way, from O'Reilly [Practical Machine Learning System (Amazon)](http://www.amazon.co.jp/gp/product/4873116988/ref=as_li_tf_tl?ie=UTF8&camp=247&creative=1211&creativeASIN=4873116988&linkCode=as2&tag=scleen- 22) has been published. Since I was able to borrow it, I will write something that will be helpful when starting machine learning by referring to "Music genre classification": musical_note: in this chapter 9: smile: For beginners: exclamation: More difficult Leave the theory and application to great people

What is machine learning?

Roughly summarized

Let the computer recognize something! ⇒ For ⇒ For the time being, let's grasp the characteristics of the target! If the features are close, they are similar! ⇒ For ⇒ We will analyze and analyze and make the target numerical!

It's strange Japanese to make the target a number: sweat_drops: It seems to say in difficult words such as "extract features".

I will analyze the sound and give out the features

Chapter 9 of Book: Practical Machine Learning System I will take up the music genre classification in. But there are only 20 pages. You can't understand everything on page 20, so it's coming little by little with the feeling of increasing the volume.

** If you do not have peripheral knowledge, it will not work due to an error, so I will supplement it **

Game: game_die: Analyze live video: chart: and so on

Music genres are categorized in the book. I'm not interested in the music genre, so I'll try it with a live video of the game on YouTube. I don't know if it will work: scream:

Preparation

I'm sorry, it's windows: computer:

I'm sorry to all MAC users: sob:

Things to prepare

--ffmpeg (used to separate audio from video) --Videos used for analysis: Please prepare videos. * URLs for downloaders, etc. are often contaminated with adware or viruses, so I will not post them (laughs). Look for something safe: mag: --python + scipy or You can download it from here ⇒ python2.7 has been installed (as of November 2014) )

Original environment

--windows7 (64bit)

Good knowledge and tools

--Various operations using python (such as how to use Python Interpreter) --General programming knowledge (more is better) --Knowledge about audio file format (a little) --Audacity Very convenient free software to play with audio files

Get started

Now that the installation of various tools has been completed

Cut out audio from video

It ’s the turn of ffmpeg, which everyone loves.

ffmpeg -i input.mp4 -map 0:1 test.wav

Extract the audio with wav like this.

** A module called scipy used for analysis can directly read wav format files, so be sure to prepare in wav format. ** **

I think mp3, aac are probably not good

The working directory (folder) is c: / work / tmp

Save test.wav to c: / work / tmp

Analysis: Let's make a spectrogram

Use Python Interpreter

Launch Python Interpreter from the Start menu.

>>>

The prompt looks like this.

When moving directories

>>> import os
>>> os.getcwd()
>>> os.chdir("/work/tmp")

Try to make a spectrogram

I will feel like it is introduced in the book

import scipy
from matplotlib.pyplot import specgram
sample_rate, X = scipy.io.wavfile.read("test.wav")
print sample_rate, X.shape
specgram(X, Fs=sample_rate, xextent=(0,30))

↑ is a sample that appears in the book, but if I did this as it was, an error occurred.

AttributeError: 'module' object has no attribute 'io'

Or something like that. I will import what I need.

from scipy import io
from scipy.io import wavfile

After doing this, again

specgram(X, Fs=sample_rate, xextent=(0,30))

Then the numbers are messed up: + 1: It worked. However, you will surely think here, "It's not a graph! You can't make a graph even if you do as I wrote in the book: book :!"

Yes, I can't.

Now, let's draw a spectrogram

import matplotlib.pyplot as plt
pxx, freq, bing, t = plt.specgram(X, Fs=sample_rate, xextent=(0,30))
plt.show()

If you do like this, a new window will open and you will see a graph.

Error handling

pxx, freq, bing, t = plt.specgram(X, Fs=sample_rate, xextent=(0,30))

An error may occur when executing ↑

If you get a MemoryError

The wav file you are using may be too large. Cut with ffmpeg: scissors :. Audacity: sound: You can use it. Audacity may be easier because it has a GUI.

ffmpeg -ss 0 -i test.wav -t 30 -c:a copy short.wav

Let's cut it in the first 30 seconds like this. After that,

sample_rate, X = scipy.io.wavfile.read("short.wav")
pxx, freq, bing, t = plt.specgram(X, Fs=sample_rate, xextent=(0,30))
plt.show()

Did it work?

If you get a mysterious error

There is a possibility that the helper will give an error. If the wav file you are using is stereo, this code will not work, so only the left ear will be extracted. You can do it with ffmpeg, but you can also do it with Audacity.

ffmpeg -i short.wav -map_channel 0.0.0 left.wav

Since then

sample_rate, X = scipy.io.wavfile.read("left.wav")
pxx, freq, bing, t = plt.specgram(X, Fs=sample_rate, xextent=(0,30))
plt.show()

You should see the graph.

Continued again this time

If there is a lot of stock (about 30?), It will continue because there is a lot of demand.

For those who are too niche to achieve 1000 Pelica: cry:

Recommended Posts

I summarized the procedure for starting machine learning and classification using sound (Reference: Practical machine learning system) ヽ (゚ * ヽ) (ノ * ゚) No wow
"OpenCV-Python Tutorials" and "Practical Machine Learning System"
I tried to process and transform the image and expand the data for machine learning
I tried to compress the image using machine learning
I tried to verify the yin and yang classification of Hololive members by machine learning
What I learned about AI and machine learning using Python (4)
I tried using Tensorboard, a visualization tool for machine learning