Nov-11-2019, 08:53 AM
hi, I tried to control raspberrypi with julius and python, but i got socket.error as follows.
Error:Traceback (most recent call last):
File "led-flashing-chess.py", line 106, in <module>
main()
File "led-flashing-chess.py", line 20, in main
client.connect((host, port)) # サーバーモードで起動したjuliusに接続
File "/usr/lib/python2.7/socket.py", line 228, in meth
return getattr(self._sock,name)(*args)
socket.error: [Errno 111] Connection refusedMy code is here.# -*- coding: utf-8 -*-
import socket
import xml.etree.ElementTree as ET
import subprocess
import time
import RPi.GPIO as GPIO
host = '192.168.11.51' # localhost
port = 10500 # juliusサーバーモードのポート
GPIO.setmode(GPIO.BCM)
def main():
p = subprocess.Popen(["sh julius-start.sh"], stdout=subprocess.PIPE, shell=True)
# julius起動スクリプトを実行→juliusはサーバー側として待ち受ける
pid = str(p.stdout.read().decode('utf-8')) # juliusのプロセスIDを取得
time.sleep(3) # 3秒間スリープ
client = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
client.connect((host, port)) # サーバーモードで起動したjuliusに接続
try:
data = '' # dataの初期化
killword = '' # 前回認識した言葉を記憶するための変数
while 1:
# print(data) # 認識した言葉を表示して確認
if '</RECOGOUT>\n.' in data:
root = ET.fromstring('<?xml version="1.0"?>\n' + data[data.find('<RECOGOUT>'):].replace('\n.', ''))
for whypo in root.findall('./SHYPO/WHYPO'):
word = whypo.get('WORD') # juliusで認識したWORDをwordに入れる
if word == u'ルーク':
# 18番ピンを出力モードで初期化する(モード指定前はどちらにもなりうる状態)
GPIO.setup(18, GPIO.OUT)
# LED点滅(2秒おき)を10回繰り返す
for x in range(5):
GPIO.output(18, True) # ON
time.sleep(2) # 2秒そのまま
GPIO.output(18, False) # OFF
time.sleep(2)
GPIO.cleanup()
killword = ('ルーク')
elif word == u'ビショップ':
GPIO.setup(25, GPIO.OUT)
for x in range(5):
GPIO.output(25, True)
time.sleep(2)
GPIO.output(25, False)
time.sleep(2)
GPIO.cleanup()
killword = ('ビショップ')
elif word == u'クイーン':
GPIO.setup(16, GPIO.OUT)
for x in range(5):
GPIO.output(16, True)
time.sleep(2)
GPIO.output(16, False)
time.sleep(2)
GPIO.cleanup()
killword = ('クイーン')
elif word == u'ナイト':
GPIO.setup(18, GPIO.OUT)
for x in range(5):
GPIO.output(18, True)
time.sleep(2)
GPIO.output(18, False)
time.sleep(2)
GPIO.cleanup()
killword = ('ナイト')
elif word == u'ポーン':
GPIO.setup(25, GPIO.OUT)
for x in range(5):
GPIO.output(25, True)
time.sleep(2)
GPIO.output(25, False)
time.sleep(2)
GPIO.cleanup()
killword = ('ポーン')
elif word == u'キング':
GPIO.setup(16, GPIO.OUT)
for x in range(5):
GPIO.output(16, True)
time.sleep(2)
GPIO.output(16, False)
time.sleep(2)
GPIO.cleanup()
killword = ('キング')
print(word) # wordを表示
data = '' # dataの初期化
else:
data += str(client.recv(1024).decode('utf-8')) # dataが空のときjuliusからdataに入れる
print('NotFound') # juliusに認識する言葉がない。認識していない。
except KeyboardInterrupt:
p.kill()
subprocess.call(["kill " + pid], shell=True) # juliusのプロセスを終了する。
client.close()
if __name__ == "__main__":
main()How can i solve it??
