Skip to content

Instantly share code, notes, and snippets.

@Taehun
Created January 12, 2016 01:05
Show Gist options
  • Save Taehun/c7a83b7a90641de6aa0f to your computer and use it in GitHub Desktop.
Save Taehun/c7a83b7a90641de6aa0f to your computer and use it in GitHub Desktop.
구글 음성 인식 예제
#!/usr/bin/python
#-*- coding: utf-8 -*-
from gtts import gTTS
import os
import json
import sys
owner = '브라이언'
while True:
os.system("arecord -D plughw:2,0 -f cd -t wav -d 2 -r 16000 -c 1 test.wav; curl -X POST --data-binary @test.wav --header 'Content-Type: audio/l16; rate=16000;' 'https://www.google.com/speech-api/v2/recognize?output=json&lang=ko-kr&key=AIzaSyDga3heigcuanXW1nUFEofIpDgAh8G80Mw' > result.txt")
os.system("rm test.wav")
f = open('result.txt', 'r')
for line in f:
json_str = line
print line
if len(line) > 13:
cmd = json.loads(json_str)
cmd_str = u'커맨드'
if cmd['result']:
cmd_dic = cmd['result'][0]
cmd_dic2 = cmd_dic['alternative'][0]
cmd_str = cmd_dic2['transcript']
if cmd_str == u'자비스':
anwser = '네' + owner + '님 무엇을 도와드릴까요?'
elif cmd_str == u'종료':
anwser = '프로그램을 종료합니다.'
if cmd_str != u'커맨드':
print anwser
os.system('wget -q -U Mozilla -O answer.mp3 "https://translate.google.com/translate_tts?ie=UTF-8&q=' + anwser + '&tl=ko&client=tw-ob"')
os.system("mplayer answer.mp3")
os.system("rm answer.mp3")
if cmd_str == u'종료':
sys.exit()
f.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment