Skip to content
This repository has been archived by the owner on Sep 8, 2024. It is now read-only.

Commit

Permalink
Merge pull request #229 from Wolfgange3311999/feature/issues-219
Browse files Browse the repository at this point in the history
Added audio accuracy test
  • Loading branch information
aatchison authored Jun 23, 2016
2 parents a9c00d6 + 6e3fa18 commit 798daec
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
124 changes: 124 additions & 0 deletions audio-accuracy-test/audio_accuracy_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
import os
import wave
from glob import glob

import pyee
from os.path import dirname, join
from speech_recognition import AudioSource
from mycroft.client.speech.local_recognizer import LocalRecognizer
from mycroft.client.speech.mic import ResponsiveRecognizer
from mycroft.util.log import getLogger
from mycroft.client.speech.mic import logger as speech_logger

__author__ = 'wolfgange3311999'
logger = getLogger('audio_test_runner')


class FileStream(object):
def __init__(self, file_name):
self.file = wave.open(file_name, 'rb')
self.size = self.file.getnframes()
self.sample_rate = self.file.getframerate()
self.sample_width = self.file.getsampwidth()

def read(self, chunk_size):
if abs(self.file.tell() - self.size) < 10:
raise EOFError
return self.file.readframes(chunk_size)

def close(self):
self.file.close()


class FileMockMicrophone(AudioSource):
def __init__(self, file_name):
self.stream = FileStream(file_name)
self.SAMPLE_RATE = self.stream.sample_rate
self.SAMPLE_WIDTH = self.stream.sample_width
self.CHUNK = 1024

def close(self):
self.stream.close()


class AudioTester(object):
def __init__(self, samp_rate):
self.ww_recognizer = LocalRecognizer(samp_rate, 'en-us')
self.listener = ResponsiveRecognizer(self.ww_recognizer)
speech_logger.setLevel(100) # Disables logging to clean output

def test_audio(self, file_name):
source = FileMockMicrophone(file_name)
ee = pyee.EventEmitter()

class SharedData:
found = False

def on_found_wake_word():
SharedData.found = True

ee.on('recognizer_loop:record_begin', on_found_wake_word)

try:
self.listener.listen(source, ee)
except EOFError:
pass

return SharedData.found


BOLD = '\033[1m'
NORMAL = '\033[0m'
GREEN = '\033[92m'
RED = '\033[91m'


def bold_str(val):
return BOLD + str(val) + NORMAL


def get_root_dir():
return dirname(dirname(__file__))

if __name__ == "__main__":
directory = join('audio-accuracy-test', 'data', 'query_after')
query = join(directory, '*.wav')
root_dir = get_root_dir()
full_path = join(root_dir, query)
file_names = sorted(glob(full_path))

num_found = 0
total = len(file_names)

if total < 1:
print(bold_str(RED+"Error: No wav files found in " + directory))
exit(1)

# Grab audio format info from first file
ex_file = wave.open(file_names[0], 'rb')

print
tester = AudioTester(ex_file.getframerate())
print

for file_name in file_names:
short_name = os.path.basename(file_name)
was_found = tester.test_audio(file_name)

if was_found:
result_str = GREEN + "Detected "
num_found += 1
else:
result_str = RED + "Not found"

print("Wake word " + bold_str(result_str) + " - " + short_name)


def to_percent(numerator, denominator):
return "{0:.2f}".format((100.0 * numerator) / denominator) + "%"


print
print("Found " + bold_str(num_found) + " out of " + bold_str(total))
print(bold_str(to_percent(num_found, total)) + " accuracy.")
print
1 change: 1 addition & 0 deletions start.sh
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ case $1 in
"audiotest") SCRIPT=${TOP}/mycroft/util/audio_test.py ;;
"collector") SCRIPT=${TOP}/mycroft_data_collection/cli.py ;;
"unittest") SCRIPT=${TOP}/test/test_runner.py ;;
"audioaccuracytest") SCRIPT=${TOP}/audio-accuracy-test/audio_accuracy_test.py ;;
"sdkdoc") SCRIPT=${TOP}/doc/generate_sdk_docs.py ;;
"enclosure") SCRIPT=${TOP}/mycroft/client/enclosure/enclosure.py ;;
"pairing") SCRIPT=${TOP}/mycroft/pairing/client.py ;;
Expand Down

0 comments on commit 798daec

Please sign in to comment.