Skip to content

Commit

Permalink
add try except for stability
Browse files Browse the repository at this point in the history
  • Loading branch information
CharlesPikachu committed Mar 1, 2022
1 parent 46ebb22 commit 5c8c69d
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 8 deletions.
2 changes: 1 addition & 1 deletion pytools/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
'''url'''
__url__ = 'https://github.com/CharlesPikachu/pytools'
'''version'''
__version__ = '0.1.13'
__version__ = '0.1.14'
'''author'''
__author__ = 'Charles'
'''email'''
Expand Down
19 changes: 12 additions & 7 deletions pytools/modules/desktoppet/desktoppet.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,15 @@ def record(self, sample_rate=16000):
fp.write(audio.get_wav_data())
'''识别'''
def recognition(self):
assert os.path.exists(self.speech_path)
with open(self.speech_path, 'rb') as fp:
content = fp.read()
result = self.aipspeech_api.asr(content, 'wav', 16000, {'dev_pid': 1536})
text = result['result'][0]
return text
try:
assert os.path.exists(self.speech_path)
with open(self.speech_path, 'rb') as fp:
content = fp.read()
result = self.aipspeech_api.asr(content, 'wav', 16000, {'dev_pid': 1536})
text = result['result'][0]
return text
except:
return None
'''合成并说话'''
def synthesisspeak(self, text=None, audiopath=None):
assert text is None or audiopath is None
Expand Down Expand Up @@ -156,7 +159,7 @@ def __init__(self, pet_type='pikachu', parent=None, **kwargs):
# 每隔一段时间检测一次语音
self.timer_speech = QTimer()
self.timer_speech.timeout.connect(self.talk)
self.timer_speech.start(5000)
self.timer_speech.start(2000)
self.running_talk = False
'''对话功能实现'''
def talk(self):
Expand All @@ -167,12 +170,14 @@ def _talk(self):
while True:
self.speech_api.record()
user_input = self.speech_api.recognition()
if user_input is None: return
if valid_names[self.pet_type] in user_input: break
else: return
self.speech_api.synthesisspeak('你好呀, 主人')
while True:
self.speech_api.record()
user_input = self.speech_api.recognition()
if user_input is None: continue
if '再见' in user_input:
self.speech_api.synthesisspeak('好的, 主人再见')
self.running_talk = False
Expand Down

0 comments on commit 5c8c69d

Please sign in to comment.