对着电脑吼一声,自动打开谷歌网站或者自动打开命令行终端--使用google speech 语音识别程序操作电脑
2013-04-08 23:16
661 查看
1)更新源文件:@ubuntu:~$ sudo vim /etc/apt/sources.list
deb http://cn.archive.ubuntu.com/ubuntu precise main restricted universe
2)更新源文件后,要 update:sudo aptitude update
3)我自己数据库有问题,重新安装mysql数据库:
@ubuntu:~$ aptitude search mysql-server
sudo aptitude reinstall mysql-server
@ubuntu:~$ aptitude install mysql-server
@ubuntu:~$ aptitude purge mysql-server
4)安装相关的包以及依赖环境:
sudo easy_install wave
~/pyvoice$ sudo aptitude install flac 此工具将 wav 转换成 flac
~/pyvoice$ sudo aptitude install python-alsaaudio
5)思路:
输入--处理--输出
1:获取电脑录音-->WAV文件
python record wav
2:录音文件-->文本
STT: Speech to Text
STT API Google API
TTS: Text to Speech
3:文本-->电脑命令
6)代码:
采样的库:recordtest.py
SpeechToText:stt_google.py
runcmd.py
1.txt
采样的库:recordtest.py
SpeechToText: stt_google.py
runcmd.py
7)运行:
jiangge@ubuntu:~/pycode/pyvoice$ python recordtest.py 1.wav;python stt_google.py ;python runcmd.py
然后对着电脑喊"新浪"
会看到命令行变化:
flac 1.2.1, Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
welcome to redistribute it under certain conditions. Type `flac' for details.
1.wav: wrote 23004 bytes, ratio=0.621
新浪网
新浪吗
新浪
如果正常的话,你就会在浏览器上看到已经打开新浪网首页了.不正常的情况...嗯,Fuck GFW
----------------------------------------------------------------
参考文献:
http://uliweb.clkg.org/forum/3/210
代码作者为@hejiasheng
deb http://cn.archive.ubuntu.com/ubuntu precise main restricted universe
2)更新源文件后,要 update:sudo aptitude update
3)我自己数据库有问题,重新安装mysql数据库:
@ubuntu:~$ aptitude search mysql-server
sudo aptitude reinstall mysql-server
@ubuntu:~$ aptitude install mysql-server
@ubuntu:~$ aptitude purge mysql-server
4)安装相关的包以及依赖环境:
sudo easy_install wave
~/pyvoice$ sudo aptitude install flac 此工具将 wav 转换成 flac
~/pyvoice$ sudo aptitude install python-alsaaudio
5)思路:
输入--处理--输出
1:获取电脑录音-->WAV文件
python record wav
2:录音文件-->文本
STT: Speech to Text
STT API Google API
TTS: Text to Speech
3:文本-->电脑命令
6)代码:
jiangge@ubuntu:~/pycode/pyvoice$ tree . ├── 1.flac ├── 1.txt ├── 1.wav ├── recordtest.py ├── runcmd.py └── stt_google.py
采样的库:recordtest.py
SpeechToText:stt_google.py
runcmd.py
1.txt
采样的库:recordtest.py
#!/usr/bin/env python ## recordtest.py ## ## This is an example of a simple sound capture script. ## ## The script opens an ALSA pcm forsound capture. Set ## various attributes of the capture, and reads in a loop, ## writing the data to standard out. ## ## To test it out do the following: ## python recordtest.py out.raw # talk to the microphone ## aplay -r 8000 -f S16_LE -c 1 out.raw # Footnote: I'd normally use print instead of sys.std(out|err).write, # but we're in the middle of the conversion between python 2 and 3 # and this code runs on both versions without conversion import sys import time import getopt import alsaaudio import wave def usage(): sys.stderr.write('usage: recordtest.py [-c <card>] <file>\n') sys.exit(2) if __name__ == '__main__': card = 'default' opts, args = getopt.getopt(sys.argv[1:], 'c:') for o, a in opts: if o == '-c': card = a if not args: usage() f = wave.open(args[0], 'wb') f.setnchannels(1) f.setsampwidth(2) f.setframerate(8000) # Open the device in nonblocking capture mode. The last argument could # just as well have been zero for blocking mode. Then we could have # left out the sleep call in the bottom of the loop inp = alsaaudio.PCM(alsaaudio.PCM_CAPTURE, alsaaudio.PCM_NONBLOCK, card) # Set attributes: Mono, 44100 Hz, 16 bit little endian samples inp.setchannels(1) inp.setrate(8000) inp.setformat(alsaaudio.PCM_FORMAT_S16_LE) # The period size controls the internal number of frames per period. # The significance of this parameter is documented in the ALSA api. # For our purposes, it is suficcient to know that reads from the device # will return this many frames. Each frame being 2 bytes long. # This means that the reads below will return either 320 bytes of data # or 0 bytes of data. The latter is possible because we are in nonblocking # mode. inp.setperiodsize(160) loops = 2000000 while loops > 0: loops -= 1 # Read data from device l, data = inp.read() if l: f.writeframes(data) time.sleep(.001)
SpeechToText: stt_google.py
#coding=utf-8 import os import urllib2 import urllib import time import json def writetofile(list_data): f = open("1.txt","w") for n in list_data: print n['utterance'] f.write(n['utterance'].encode("utf-8")) f.close() def stt_google_wav(filename): #Convert to flac os.system(FLAC_CONV+ filename+'.wav') f = open(filename+'.flac','rb') flac_cont = f.read() f.close() #post it lang_code='zh-CN' googl_speech_url = 'https://www.google.com.ua/speech-api/v1/recognize?xjerr=1&client=chromium&pfilter=2&lang=%s&maxresults=6'%(lang_code) hrs = {"User-Agent": "Mozilla/5.0 (X11; Linux i686) AppleWebKit/535.7 (KHTML, like Gecko) Chrome/16.0.912.63 Safari/535.7",'Content-type': 'audio/x-flac; rate=8000'} req = urllib2.Request(googl_speech_url, data=flac_cont, headers=hrs) p = urllib2.urlopen(req) data = p.read() list_data = json.loads(data)["hypotheses"] writetofile(list_data) return FLAC_CONV = 'flac -f ' # We need a WAV to FLAC converter. if(__name__ == '__main__'): stt_google_wav("1")
runcmd.py
#coding=utf-8 import os f = open("1.txt","r") cmds = f.read() f.close() def run_browser(cmds): if cmds.find("谷歌") > -1: os.system("firefox www.google.com") if cmds.find("百度") > -1: os.system("firefox www.baidu.com") if cmds.find("新浪") > -1: os.system("firefox www.sina.com.cn") def run_term(cmds): if cmds.find("终端") > -1: os.system("gnome-terminal") def run_gedit(cmds): if cmds.find("编程") > -1: os.system("gedit test.py") def run_jeap(cmds): count = 0 if cmds.find("智") > -1: count += 1 if cmds.find("志") > -1: count += 1 if cmds.find("只") > -1: count += 1 if cmds.find("支") > -1: count += 1 if cmds.find("普") > -1: count += 1 if cmds.find("扑") > -1: count += 1 if cmds.find("谱") > -1: count += 1 if count > 1: os.system("firefox www.jeapedu.com") run_browser(cmds) run_jeap(cmds) run_term(cmds) run_gedit(cmds)
7)运行:
jiangge@ubuntu:~/pycode/pyvoice$ python recordtest.py 1.wav;python stt_google.py ;python runcmd.py
然后对着电脑喊"新浪"
会看到命令行变化:
flac 1.2.1, Copyright (C) 2000,2001,2002,2003,2004,2005,2006,2007 Josh Coalson
flac comes with ABSOLUTELY NO WARRANTY. This is free software, and you are
welcome to redistribute it under certain conditions. Type `flac' for details.
1.wav: wrote 23004 bytes, ratio=0.621
新浪网
新浪吗
新浪
如果正常的话,你就会在浏览器上看到已经打开新浪网首页了.不正常的情况...嗯,Fuck GFW
----------------------------------------------------------------
参考文献:
http://uliweb.clkg.org/forum/3/210
代码作者为@hejiasheng
相关文章推荐
- 使用 Python 实现:对着电脑吼一声,自动打开浏览器中的默认网站。
- 使用.NET程序操作已打开的excel文件
- Linux中安装使用RXVT命令行终端窗口程序的教程
- android语音识别方法一:使用intent调用语音识别程序
- 如何通过命令行或者使用代码打开一个apk文件
- Windows Server 2008中用管理员的权限使用命令行来打开程序
- powerpoint 2010 打开ppt时才是提示错误,提示“Microsoft powerpoint 无法执行语言识别,此功能不可用。若要使用此功能 ,请再次运行安装程序,选择“添加或删除功能”,
- Mac下使用终端命令行导入MySQL数据库.sql文件的操作
- 教教大家如何使用php程序开发一个自动识别移动设备
- 网站使用UTF-8编码不能自动识别问题的解决
- 关于在Ubuntu的终端上自动编译程序,使用GNU make.
- linux操作之:设置系统启动时,自动启动一些程序,或者执行一些命令
- 出现的问题: A、使用命令行无法登录oracle或者显示ORA-01109:数据库未打开; B、使用PL/SQL登录出现 ORA-01033:ORACLE initialization or
- Win7系统如何通过口令操作电脑 设置使用语音操作Win7系统图文教程
- 如何把使用命令行或者Eclipse将Java程序打包为jar文件
- 解决Linux关闭终端(关闭SSH等)后运行的程序或者服务自动停止【后台运行程序】
- Linux关闭终端(或者关闭SSH等)后运行的程序自动停止
- Mac环境下svn的使用详解1(通过终端命令行操作)
- Android 自动识别TextView或者EditView里的url并在自己的应用程序里打开
- 使用VC6.0 自动识别可用串口 添加至combox 并打开。