def test(evaluation): print('Loading recognizer, please wait...') speech_recognizer = SpeechRecognizer('../dictionary/') print('Loading completed.') n = len(evaluation['test']) all = 0 correct = 0 for i, example in enumerate(evaluation['test']): curr_all = 0 curr_correct = 0 example_name = example['file'] hint = example['expected_words_count'] rate, samples = wavfile.read('../wav/{0}.wav'.format(example_name)) rate, speech_words = extract_words(rate, samples, hint) if len(speech_words) != len(example['words']): print('Error in extracting words. Skipping {0}.'.format( example_name)) continue for word, sol_word in zip(speech_words, example['words']): sol, _ = try_recognition(rate, word, speech_recognizer) #print('Expected {0}, recognized {1}'.format(sol_word, sol)) all += 1 curr_all += 1 if sol == sol_word: correct += 1 curr_correct += 1 print('Completed {0}%'.format(100.0 * (i + 1) / float(n))) print('Stats on {0}, correct {1}%'.format( example_name, 100.0 * curr_correct / curr_all)) break print('--------------------------------------') print('Correct {0}%'.format(100.0 * correct / all))
def test(dirs): print('Testing on {0}'.format(dirs)) print('Loading recognizer, please wait...') speech_recognizer = SpeechRecognizer(DICTIONARY) print('Loading completed.') all = 0 correct = 0 for i, dir in enumerate(dirs): curr_all = 0 curr_correct = 0 for file in os.walk(DIR_NAME + '/' + dir).next()[2]: expected_word = file.split('_')[0] if not filter(expected_word): continue rate, samples = wavfile.read('{0}/{1}/{2}'.format( DIR_NAME, dir, file)) rate, speech_words = extract_words(rate, samples, 1) if len(speech_words) != 1: print('Error extracting {0}/{1}'.format(dir, file)) continue word, _ = try_recognition(rate, speech_words[0], speech_recognizer) all += 1 curr_all += 1 if word == expected_word: correct += 1 curr_correct += 1 else: print 'Expected {0}, got {1}'.format(expected_word, word) if curr_all > 0: print('Correct {0}% in {1}'.format( 100.0 * curr_correct / curr_all, dir)) print('Completed {0}%'.format(100.0 * (i + 1) / float(len(dirs)))) print('--------------------------------') print('Correct {0}%'.format(100.0 * correct / all)) print('Done testing.')
def test(dirs): print('Testing on {0}'.format(dirs)) print('Loading recognizer, please wait...') speech_recognizer = SpeechRecognizer(DICTIONARY) print('Loading completed.') all = 0 correct = 0 for i, dir in enumerate(dirs): curr_all = 0 curr_correct = 0 for file in os.walk(DIR_NAME + '/' + dir).next()[2]: expected_word = file.split('_')[0] if not filter(expected_word): continue rate, samples = wavfile.read('{0}/{1}/{2}'.format( DIR_NAME, dir, file)) rate, speech_words = extract_words(rate, samples, 1) if len(speech_words) != 1: print('Error extracting {0}/{1}'.format(dir, file)) continue word, _ = try_recognition(rate, speech_words[0], speech_recognizer) all += 1 curr_all += 1 if word == expected_word: correct += 1 curr_correct += 1 else: print 'Expected {0}, got {1}'.format(expected_word, word) if curr_all > 0: print('Correct {0}% in {1}'.format(100.0 * curr_correct / curr_all, dir)) print('Completed {0}%'.format(100.0 * (i + 1) / float(len(dirs)))) print('--------------------------------') print('Correct {0}%'.format(100.0 * correct / all)) print('Done testing.')
def train(evaluation): n = len(evaluation['training']) for i, example in enumerate(evaluation['training']): example_name = example['file'] hint = example['expected_words_count'] rate, samples = wavfile.read('../wav/{0}.wav'.format(example_name)) rate, speech_words = extract_words(rate, samples, hint) if len(speech_words) != len(example['words']): 'Error during training for {0}'.format(example_name) continue for word, speech in zip(example['words'], speech_words): wav_name = id_generator() + '.wav' save_wav(word, wav_name, rate, speech, '../dictionary') print('Completed {0}%'.format(100.0 * (i + 1) / float(n)))
def train(dirs): print('Training on {0}'.format(dirs)) reset_dir(DICTIONARY) for i, dir in enumerate(dirs): for file in os.walk(DIR_NAME + '/' + dir).next()[2]: word = file.split('_')[0] if not filter(word): continue rate, samples = wavfile.read('{0}/{1}/{2}'.format( DIR_NAME, dir, file)) _, speech_words = extract_words(rate, samples, 1) if len(speech_words) != 1: print('Error extracting {0}/{1}'.format(dir, file)) continue wav_name = id_generator() + '.wav' save_wav(word, wav_name, rate, speech_words[0], DICTIONARY) print('Completed {0}%'.format(100.0 * (i + 1) / float(len(dirs)))) print('Done training.')
def test_word_extractor(evaluation): n = len(evaluation['examples20']) all = 0 correct = 0 for i, test in enumerate(evaluation['examples20']): test_name = test['file'] hint = test['expected_words_count'] rate, samples = wavfile.read('../wav/{0}.wav'.format(test_name)) _, speech_words = extract_words(rate, samples, hint) #_, speech_words_no_hint = extract_words(rate, samples) speech_words_no_hint = [] print('Test {0}: expected = {1}, extracted = {2}, with no hint = {3}'. format(test_name, test['expected_words_count'], len(speech_words), len(speech_words_no_hint))) all += 1 if len(speech_words) == test['expected_words_count']: correct += 1 print('Completed {0}%'.format(100.0 * (i + 1) / float(n))) print('Extracted {0}% correctly.'.format(100.0 * float(correct) / float(all)))
def test_word_extractor(evaluation): n = len(evaluation['examples20']) all = 0 correct = 0 for i, test in enumerate(evaluation['examples20']): test_name = test['file'] hint = test['expected_words_count'] rate, samples = wavfile.read('../wav/{0}.wav'.format(test_name)) _, speech_words = extract_words(rate, samples, hint) #_, speech_words_no_hint = extract_words(rate, samples) speech_words_no_hint = [] print('Test {0}: expected = {1}, extracted = {2}, with no hint = {3}' .format(test_name, test['expected_words_count'], len(speech_words), len(speech_words_no_hint))) all += 1 if len(speech_words) == test['expected_words_count']: correct += 1 print('Completed {0}%'.format(100.0 * (i + 1) / float(n))) print('Extracted {0}% correctly.'.format( 100.0 * float(correct) / float(all)))
def main(): warnings.simplefilter('ignore') clear_tmp() print('0. Simplified') print('1. Full') option = input('Select input: ') if option == 0: dir = 'simplified_dict/' else: dir = 'dictionary/' print('Loading recognizer, please wait...') speech_recognizer = SpeechRecognizer(dir) print('Loading completed.') print('0. Record new') print('1. Use existing wav file (from wav/)') print('2. Just record wav for later use') print('3. Split existing file (from wav/)') option = input('Select input: ') if option == 0: speech_file = record_wav() elif option == 1: speech_file = raw_input('File name (without .wav): ') elif option == 2: _ = record_wav() return elif option == 3: speech_file = raw_input('File name (without .wav): ') hint = input( 'Hint maybe (enter expected number of words, 0 for no hint)? ') rate, samples = wavfile.read('wav/{0}.wav'.format(speech_file)) rate, speech_words = extract_words(rate, samples, hint) for speech in speech_words: _ = save_tmp_wav(rate, speech) print('Extracted {0} words'.format(len(speech_words))) return else: print('Invalid option') return hint = input('Hint maybe (enter expected number of words, 0 for no hint)? ') rate, samples = wavfile.read('wav/{0}.wav'.format(speech_file)) rate, speech_words = extract_words(rate, samples, hint) print('Extracted {0}'.format(len(speech_words))) correct_count = 0 count = 0 for speech in speech_words: speech_copy = speech[:] _ = raw_input('Press enter to continue...') wav_name = save_tmp_wav(rate, speech) play_wav('tmp/{0}'.format(wav_name)) word, confidence = try_recognition(rate, speech_copy, speech_recognizer) print('Recognized [{0}] with confidence {1}'.format(word, confidence)) count += 1 correct = input('Is it correct (0/1)? ') if correct == 0: add = input('Do you want to add this word in dictionary (0/1)? ') if add == 0: print(':(') else: text_word = raw_input('Write string representation: ') text_word.rstrip('\n').replace(' ', '_') save_wav(text_word, wav_name, rate, speech) print('Added new word [{0}]'.format(text_word)) else: correct_count += 1 os.rename('tmp/{0}'.format(wav_name), 'tmp/{0}.wav'.format(word)) print(':)') print('Recognized {0}% correctly.'.format( 100.0 * float(correct_count) / float(count)))