Friday, 5 February 2021

python speech recognition

#computer speaks: what I can do for you
#user talks to microphone: how are you today
#computer talks back: I am fine

PS C:\Users\bob\python-speech> python main.py
what can i do for you?
say something!
You said: how are you today
I am fine

------------------------
#main.py
import time
import speech_recognition as sr
#from gtts import gTTS
import pyglet
import subprocess
import os
from playsound import playsound
import pyttsx3

def speak(audioString):
    print(audioString)
    #tts = gTTS(text=audioString, lang='en')
    #tts.save("audio.mp3")

    """
    wmp = r"C:\Program Files (x86)\Windows Media Player\wmplayer.exe"
    media_file = os.path.abspath(os.path.relpath("audio.mp3"))
    p = subprocess.call([wmp, media_file])
    """

    #playsound("audio.mp3")
    engine = pyttsx3.init()
    engine.say(audioString)
    engine.runAndWait()

def recordAudio():
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print("say something!")
        r.adjust_for_ambient_noise(source)
        audio = r.listen(source)

        try:
            data = r.recognize_google(audio)
            print("You said: " + data)

        except sr.UnknownValueError:
            return "Couldn't understand audio"

        except sr.RequestError as e:
            return "couldn't request results; {0}" + format(e)

        return data

speak("what can i do for you?")
#time.sleep(2)

data = recordAudio()

if "how are you" in data:
    speak("I am fine")

---------------------------
#power shell
pip install SpeechRecognition
#pip install gtts
#pip install pyglet
pip install pyttsx3

pip install PyAudio-0.2.11-cp38-cp38-win_amd64.whl

#logs
Processing c:\users\bob\python-speech\pyaudio-0.2.11-cp38-cp38-win_amd64.whl
Installing collected packages: PyAudio
Successfully installed PyAudio-0.2.11

reference:

pyaudio

text to sound

microphone to text

Factory-Built Homes

Thursday, 4 February 2021

keras 19 rnn generate music 2

code link: https://github.com/chuanshuoge6/keras-rnn-music



#main.py

import glob
import numpy as np
from music21 import *
import tensorflow as tf
from tensorflow.keras.models import Sequential
from tensorflow.keras.layers import Dense, Dropout, LSTM, Activation
from tensorflow.keras.models import load_model

#extract all notes composed in songs
notes = []
for file in glob.glob("midi_songs/*.mid"):
    midi = converter.parse(file)
    notes_to_parse = None
    parts = instrument.partitionByInstrument(midi)
    if parts: # file has instrument parts
        notes_to_parse = parts.parts[0].recurse()
    else: # file has notes in a flat structure
        notes_to_parse = midi.flat.notes
    for element in notes_to_parse:
        if isinstance(element, note.Note):
            notes.append(str(element.pitch))
        elif isinstance(element, chord.Chord):
            notes.append('.'.join(str(n) for n in element.normalOrder))

#print(notes)
#'A3', 'C4', 'D3', 'F3', 'G4', 'A3', 'C4'

sequence_length = 100
# remove mulitplication of notes, sort
pitchnames = sorted(set(item for item in notes))
#print(pitchnames)
n_vocab = len(pitchnames)

# create a dictionary to map pitches to integers
note_to_int = dict((note, number) for number, note in enumerate(pitchnames))
int_to_note = dict((number, note) for number, note in enumerate(pitchnames))
#print(note_to_int)
#there are 308 different notes

network_input = []
network_output = []

#100 notes in succession produce the 101st note,
#slide 100 notes block through music to generate input array,
#gather cosresponding output to each input block to form output array
# create input sequences and the corresponding outputs
for i in range(0, len(notes) - sequence_length, 1):
    sequence_in = notes[i:i + sequence_length]
    sequence_out = notes[i + sequence_length]
    network_input.append([note_to_int[char] for char in sequence_in])
    network_output.append(note_to_int[sequence_out])
n_patterns = len(network_input)

# reshape the input into a format compatible with LSTM layers
network_input = np.reshape(network_input, (n_patterns, sequence_length, 1))
# normalize input
network_input = network_input / float(n_vocab)
network_output = tf.keras.utils.to_categorical(network_output)

#print(network_input)
#print(network_output)
#print(network_input.shape)
#(43990, 100, 1)
#print(network_output.shape)
#(43990, 308)

# train on GPU
pysical_devices = tf.config.experimental.list_physical_devices('GPU')
# print("Num GPUs Available: ", len(pysical_devices))
tf.config.experimental.set_memory_growth(pysical_devices[0], True)
"""
model = Sequential()

model.add(LSTM(
    256,
    input_shape=(network_input.shape[1], network_input.shape[2]),
    return_sequences=True
))

model.add(Dropout(0.3))
model.add(LSTM(512, return_sequences=True))

model.add(Dropout(0.3))
model.add(LSTM(256))

model.add(Dense(256))
model.add(Dropout(0.3))

model.add(Dense(n_vocab))
model.add(Activation('softmax'))
model.summary()

model.compile(loss='categorical_crossentropy', optimizer='adam')
model.fit(network_input, network_output, epochs=200, batch_size=64)
model.save('model.h5')
"""
#start generating music
#pick random note block as a start
#start = np.random.randint(0, len(network_input)-1)
start = 40000

pattern = network_input[start]
prediction_output = []

model = load_model('model.h5')

# generate 500 notes, use 100 notes as start to generate 101st note
# then use 1-101 to generate 102...
for note_index in range(500):
    prediction_input = np.reshape(pattern, (1, len(pattern), 1))
    prediction_input = prediction_input / float(n_vocab)

    prediction = model.predict(prediction_input, verbose=0)
    index = np.argmax(prediction)

    result = int_to_note[index]
    prediction_output.append(result)

    pattern = np.append(pattern, index)
    pattern = pattern[1:len(pattern)]

#music 21 translates
offset = 0
output_notes = []
# create note and chord objects based on the values generated by the model
for pattern in prediction_output:
    # pattern is a chord
    if ('.' in pattern) or pattern.isdigit():
        notes_in_chord = pattern.split('.')
        notes = []
        for current_note in notes_in_chord:
            new_note = note.Note(int(current_note))
            new_note.storedInstrument = instrument.Piano()
            notes.append(new_note)
        new_chord = chord.Chord(notes)
        new_chord.offset = offset
        output_notes.append(new_chord)
    # pattern is a note
    else:
        new_note = note.Note(pattern)
        new_note.offset = offset
        new_note.storedInstrument = instrument.Piano()
        output_notes.append(new_note)
    # increase offset each iteration so that notes do not stack
    offset += 0.5

midi_stream = stream.Stream(output_notes)
midi_stream.write('midi', fp='test_output.mid')

reference:

How to Upload Audio to YouTube

Thursday, 28 January 2021

test flights

 expense
Jan 29 - driving 200km
Jan 30 - driving 200km, fuel, jeffery's dinner, dollarama
Jan 31 - driving 200km, fuel x 2, field dinner, hard drive
Feb 1 - driving 200km, fuel, hard drive, canadian tire
Feb 2 - driving 160km

time sheet
Jan 28 -  build mount - 8h - document review 2h
Jan 29 - install old new tabi - 14h
Jan 30 - trouble shoot new tabi shutter problem - 8h, bundle flight old, new tabi - 8h
Jan 31 - medicine hat survey 8h, documentation + backup 8h
Feb 1 - install uv 8h, bundle, water bath, power line 8h
Feb 2 - install new tabi + uv - 8h

THE GAMESTONK