forked from neuralassembly/raspi
-
Notifications
You must be signed in to change notification settings - Fork 0
/
recog-TV.py
181 lines (174 loc) · 8.28 KB
/
recog-TV.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
# -*- coding: utf-8 -*-
import socket
from io import StringIO
import re
import subprocess
try:
unicode # python2
def u(str): return str.decode('utf-8')
pass
except: # python3
def u(str): return str
pass
host = '127.0.0.1'
port = 10500
bufsize = 1024
buff = StringIO(u(''))
pattern = r'WHYPO WORD=\"(.*)\" CLASSID'
try:
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
sock.connect((host, port))
while True:
data = sock.recv(bufsize)
buff.write(data.decode('utf-8'))
data = buff.getvalue().replace('> ', '>\n ')
if '\n' in data:
lines = data.splitlines()
for i in range(len(lines)-1):
if lines[i] != '.':
#print(lines[i])
m = re.search(pattern, lines[i])
if m:
word = m.group(1)
# 認識された単語wordの中に、u('...') という文字列が含まれるかどうかを
# チェックし、文字列に応じたアクションを記述します。
# u('...')でくくるのは、python2とpython3の互換性を保つためです。
# 「対象となる文字が含まれているか」を調べていますので、
# 先に「『1』が含まれるか」をチェックすると
# 10~12がすべて「1」と判定されてしまいます。
# そのため、10~12のチェックを先に行っています。
if u('10') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch10']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('11') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch11']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('12') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch12']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('1') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch1']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('2') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch2']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('3') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch3']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('4') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch4']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('5') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch5']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('6') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch6']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('7') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch7']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('8') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch8']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('9') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'ch9']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('電源') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'power']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('入力') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'input']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('ボリュームダウン') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'vdown']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('ボリュームアップ') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'vup']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('チャンネルダウン') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'cdown']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
elif u('チャンネルアップ') in word:
print(word)
args = ['irsend', '-#', '1', 'SEND_ONCE', 'TV', 'cup']
try:
subprocess.Popen(args)
except OSError:
print('command not found.')
buff.close()
buff = StringIO(u(''))
if lines[len(lines)-1] != '.':
buff.write(lines[len(lines)-1])
except socket.error:
print('socket error')
except KeyboardInterrupt:
pass
sock.close()