Created
April 1, 2013 11:56
-
-
Save nobonobo/5284573 to your computer and use it in GitHub Desktop.
「音声認識クライアント」
依存:websocket-client==0.10.0
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #/usr/bin/env python | |
| # encoding: utf-8 | |
| import sys | |
| import os | |
| import atexit | |
| import time | |
| import json | |
| import traceback | |
| from websocket import create_connection, WebSocketConnectionClosedException | |
| import autopy | |
| # requirements: websocket-client, autopy | |
| def proc(ws): | |
| while 1: | |
| try: | |
| msg = ws.recv() | |
| if msg is None: | |
| break | |
| params = json.loads(msg) | |
| if 'rel' in params : | |
| dx, dy = params['rel'] | |
| gx, gy = autopy.mouse.get_pos() | |
| autopy.mouse.move(gx+dx, gy+dy) | |
| if 'click' in params: | |
| print 'click!' | |
| autopy.mouse.click(params['click']) | |
| if 'toggle' in params: | |
| toggle = toggle = params.get('toggle') | |
| for i in range(3): | |
| autopy.mouse.toggle(i in toggle, i) | |
| if 'hypotheses' in params: | |
| for i in params['hypotheses']: | |
| print i['confidence'], i['utterance'] | |
| # TODO: 文言に応じたアクションを書く。 | |
| except KeyboardInterrupt: | |
| raise KeyboardInterrupt() | |
| except WebSocketConnectionClosedException: | |
| break | |
| except: | |
| traceback.print_exc() | |
| break | |
| def main(url): | |
| while 1: | |
| try: | |
| ws = create_connection(url) | |
| except KeyboardInterrupt: | |
| return | |
| except: | |
| time.sleep(1) | |
| continue | |
| print 'connected:', url | |
| try: | |
| proc(ws) | |
| finally: | |
| ws.close() | |
| if __name__=='__main__': | |
| main('ws://127.0.0.1:8080/') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment