0

I was trying to make an VA using speech recogniser pyqt5 qml and gtts. But every time I ran this then I got error says speek defined before assignment. i can't add myCommand(any_other defenition) or myCommand() because this is non defined for pyqt5 what shall i do.

import sys
from PyQt5.QtCore import QObject, QUrl
from PyQt5.QtWidgets import QApplication
from PyQt5.QtQuick import QQuickView
from PyQt5.QtQml import QQmlApplicationEngine
import os
import speech_recognition as sr
from gtts import gTTS
def myCommand():
    say1.setProperty("text", "say something")
    r = sr.Recognizer()
    with sr.Microphone() as source:
        print('Ready...')
        audio = r.listen(source)
    try:
        command = r.recognize_google(audio, language = 'ml-IN')
        print (command)
    except sr.UnknownValueError:
        print('Your last command couldn\'t be heard')
    if command != '':
        command1.setProperty("text", command)
        if 'ലൈറ്റ്' in command:
            if 'ഓണ്‍' in command:
                speek = 'ശരി'
    print(speek)

if __name__ == '__main__':
    myApp = QApplication(sys.argv)

    engine = QQmlApplicationEngine()
    context = engine.rootContext()
    context.setContextProperty("main", engine)

    engine.load('main.qml')
    engine.load('steve.qml')

    win = engine.rootObjects()[0]
    commander = win.findChild(QObject, "commander")
    say1 = win.findChild(QObject, "say")
    command1 = win.findChild(QObject, "command")
    editText = win.findChild(QObject, "editText")
    animation = win.findChild(QObject, "animation")
    scaler = win.findChild(QObject, "scaler")
    rotator = win.findChild(QObject, "rotator")


    commander.clicked.connect(myCommand)
    win.show()

    sys.exit(myApp.exec_())

1 Answers1

1

You need to indent the print(speek) command in the function myCommand().

if command != '':
    command1.setProperty("text", command)
    if 'ലൈറ്റ്' in command:
        if 'ഓണ്‍' in command:
            speek = 'ശരി'
            print(speek)

This error was thrown because speek is defined in the if block and in the cases the if does not evaluate to True, speek is not accessible.

You might find this interesting

naive
  • 367
  • 1
  • 3
  • 9