Skip to content

Instantly share code, notes, and snippets.

@savage69kr
Forked from marsam/voice.py
Created April 1, 2013 14:03
Show Gist options
  • Select an option

  • Save savage69kr/5285135 to your computer and use it in GitHub Desktop.

Select an option

Save savage69kr/5285135 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# -*- coding: utf-8 -*-
# vim: ai ts=4 sts=4 et sw=4 ft=python cc=80
"""
Voice Recognition
=================
Voice recognition using google webservice
See: http://src.chromium.org/viewvc/chrome/trunk/src/content/browser/speech/
* google_one_shot_remote_engine.cc
To convert audio to flac::
$ sox audiofile.wav audiofile.flac rate 16k
"""
try:
from urllib.parse import urlencode, urljoin
from urllib.error import URLError, HTTPError
from urllib.request import Request, urlopen
except ImportError: # python2
from urllib import urlencode # noqa
from urlparse import urljoin # noqa
from urllib2 import urlopen, Request, URLError, HTTPError # noqa
def process(audiofile, *args, **kwargs):
params = urlencode({
'lang': kwargs.pop('lang', 'es-ES'), # (necessary) language
'xjerr': '1', # TODO: document
'output': 'pb', # TODO: document
'pfilter': kwargs.pop('pfilter', '1'), # profanity filter
'client': kwargs.pop('client', 'chromium'), # browser
'maxresults': kwargs.pop('maxresults', '4'), # max results
})
headers = {
'Content-Type': 'audio/x-flac; rate=16000',
}
url = 'https://www.google.com/speech-api/v1/recognize?{0}'.format(params)
with open(audiofile, 'rb') as audio:
data = audio.read()
req = Request(url, data, headers)
try:
response = urlopen(req)
except HTTPError as e:
print('We failed to reach a server.')
print('Reason: ', e.reason)
except URLError as e:
print('The server couldn\'t fulfill the request.')
print('Error code: ', e.code)
else:
print(response.read())
def main():
import argparse
parser = argparse.ArgumentParser(description='Voice recognition command line.') # nopep8
parser.add_argument('audiofile', metavar='audiofile', type=str, help='Audio file in flac to process') # nopep8
parser.add_argument('-l', '--lang', dest='lang', default='es-ES', help='Language to recognize voice') # nopep8
parser.add_argument('-p', '--pfilter', dest='pfilter', default='1', help='Profanity filter') # nopep8
parser.add_argument('-r', '--maxresults', dest='maxresults', default='4', help='Number of results') # nopep8
args = vars(parser.parse_args())
process(**args)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment