@Romane (hi kon)

Are you sure you want to delete the question?

If your question is resolved, you may close it.

Leaving a resolved question undeleted may help others!

We hope you find it useful!

pythonで音声認識とレベルバーの同時処理について

解決したいこと

音声認識のpyファイルと、音量レベルバーのpyファイルを一つにして、
認識とレベルバーを同時に処理したい。
音声認識はできているのですが、レベルバーが動かずに困っています。
解決方法を教えて下さい。

完成したら、下記のようにしたい。
「認識中」の上がレベルバー、「認識中」には、認識した文字が表示
01.jpg

試したこと

下にある「音声認識用pyファイル voice.py」と「音量レベルバー表示pyファイル bar.py」があり、voice.pyのvoice_inをbar.pyにimportし,bar.pyのapp()に追記。
この時点で、音声認識はするものの、ボタンのクリック等出来なくなります。

その他に、音声認識したものを、#ラベルを変更用のキー「key='-TEXT-'」を使って表示したいと考えています。

bar2.py
from VoicePaste import voice_in
-------省略------
def app():
    while True:     #ボタン動作  
        event, values = _VARS['window'].read(timeout=100)
        if event == sg.WIN_CLOSED or event == '終了':
            pAud.terminate()
            break
        if event == '開始':
            listen()
        if event == '停止':
            stop()
        voice_in()    #追加
        update_text = voice_text
        _VARS['window']['-TEXT-'].update(update_text)

音声認識用pyファイル

voice.py
import time
import speech_recognition as sr
import pyperclip
import pyautogui

###### 音声入力
def voice_in():
    while True:
        r = sr.Recognizer()
        with sr.Microphone() as source:
            print("何かお話しして下さい。")
            r.adjust_for_ambient_noise(source , duration = 1 ) #雑音対策
            audio = r.listen(source)

        try:
            # Google Web Speech APIで音声認識
            text =""
            text = r.recognize_google(audio, language="ja-JP")

        except sr.UnknownValueError:
            print("Google Web Speech APIは音声を認識できませんでした。")
        except sr.RequestError as e:
            print("GoogleWeb Speech APIに音声認識を要求できませんでした;"
                " {0}".format(e))
        else:
            if text == "停止" : #停止で終了
                break
            else:
                print(text)
                pyperclip.copy(text)
                pyautogui.press('enter')     #メモ帳などを選択した状態で
                time.sleep(0.2)              #話すと、貼り付けられる
                pyautogui.hotkey('ctrl', 'v')
                voice_text = text

if __name__ == "__main__":
    app = voice_in()

print("停止を確認。終わります。")

音量レベルバー表示pyファイル

bar.py
import PySimpleGUI as sg
import pyaudio
import numpy as np

_VARS = {'window': False,'stream': False}

AppFont = 'Any 16'
sg.theme('Black')
layout = [[sg.ProgressBar(10000, orientation='h',
                          size=(30, 10), key='-PROG-')],
            [sg.Text('開始を押してね', size=(40, 2) ,font=(AppFont,11,'bold'),key='-TEXT-'))],     #ラベルを変更用のキー「key='-TEXT-'」
            [sg.Button('開始', font=(AppFont,10)),
            sg.Button('停止', font=(AppFont,10)),
            sg.Button('終了', font=(AppFont,10))]]
_VARS['window'] = sg.Window('音声入力レベルバー', layout,no_titlebar=False,finalize=True,transparent_color=True,keep_on_top=True)

CHUNK = 1024
RATE = 44100
INTERVAL = 1
pAud = pyaudio.PyAudio()

def stop():
    if _VARS['stream']:
        _VARS['stream'].stop_stream()
        _VARS['stream'].close()
        _VARS['window']['-PROG-'].update(0)

def callback(in_data, frame_count, time_info, status):
    data = np.frombuffer(in_data, dtype=np.int16)
    _VARS['window']['-PROG-'].update(np.amax(data))
    return (in_data, pyaudio.paContinue)

def listen():
    _VARS['stream'] = pAud.open(format=pyaudio.paInt16, channels=1, rate=RATE,input=True, frames_per_buffer=CHUNK, stream_callback=callback)
    _VARS['stream'].start_stream()

def app():
    while True:     #ボタン動作  
        event, values = _VARS['window'].read(timeout=100)
        if event == sg.WIN_CLOSED or event == '終了':
            pAud.terminate()
            break
        if event == '開始':
            listen()
        if event == '停止':
            stop()
if __name__ == "__main__":
    application = app()
    application.mainloop()
_VARS['window'].close()
0 likes

Your answer might help someone💌