0

I am using Windows and python. The objective is simple, running the main.py starts a speech recognition and once it recognizes what was said returns the text to main.py. The speech recognition program recognizes without any issues, the problem is at multithreading and getting the result back to main.py.

Here is the main.py:

import threading
from speechEngine.recognize import *
from SpeechSynthesis.speech import *
from core.AI import *
spch= "default"
newthread=threading.Thread(target=speechrec())
newthread.start()
while(True):
    if(spch == "default"):
        print("at default")
        continue
    else:
        print(spch)
        result=process(spch)
        speak(result)
        spch="default"

And here is speech recognition which is called as a new thread:

import argparse
import os
import queue
from typing import Text
import sounddevice as sd
import vosk
import sys
import json
#from vosk import SetLogLevel
#SetLogLevel(-1)
def speechrec():
    q = queue.Queue()
    "a lot of argument lines have been deleted to increase readability"

    try:
        if args.model is None:
            args.model = "model"
        if args.samplerate is None:
            device_info = sd.query_devices(args.device, 'input')
            args.samplerate = int(device_info['default_samplerate'])

            model = vosk.Model(args.model)
            with sd.RawInputStream(samplerate=args.samplerate, blocksize = 8000, device=args.device, dtype='int16', channels=1, callback=callback):
                rec = vosk.KaldiRecognizer(model, args.samplerate)
                while True:
                    data = q.get()
                    if rec.AcceptWaveform(data):
                        vc=rec.FinalResult()   #produces raw output of what the user said
                        vc=json.loads(vc)
                        vc=vc['text']    #converts the user speech to text format
                        if vc != '':
                            global spch
                            spch = vc          
    except KeyboardInterrupt:
        parser.exit(0)
martineau
  • 119,623
  • 25
  • 170
  • 301
Benji
  • 15
  • 5
  • Most Python implementations have a [Global Interpreter Lock](https://en.wikipedia.org/wiki/Global_interpreter_lock), so you should consider using some other programming language, or designing your software architecture with several communicating processes – Basile Starynkevitch Jul 12 '21 at 06:33
  • @BasileStarynkevitch Is there any way I can run the recognition and get the result back? – Benji Jul 12 '21 at 06:37
  • 2
    @BasileStarynkevitch I dont think thats his issue at all in this case ... to send back to the main thread use a queue and put messages in it from the thread and pull them out from the main thread – Joran Beasley Jul 12 '21 at 06:39
  • You may need to use operating system specific inter-process communication primitives. For Linux, see [syscalls(2)](https://man7.org/linux/man-pages/man2/syscalls.2.html), [pipe(7)](https://man7.org/linux/man-pages/man7/pipe.7.html), [poll(2)](https://man7.org/linux/man-pages/man2/poll.2.html) – Basile Starynkevitch Jul 12 '21 at 06:40
  • @BasileStarynkevitch Can u give me a code example on how to do it with the queue.. I just need the text back at main.py. I am using windows – Benji Jul 12 '21 at 06:40
  • No, since I am not a Python expert, I never used or coded for Windows, and not a native English speaker. Consider sending me a long email to `basile@starynkevitch.net` explaining what you really want to do. – Basile Starynkevitch Jul 12 '21 at 06:41
  • @BasileStarynkevitch I am relatively new to coding and i really wanna make this work . its for a project work – Benji Jul 12 '21 at 06:42
  • Be aware that it takes [ten years to learn programming](http://www.norvig.com/21-days.html), so you need to allocate efforts and time to your project. Consider following university courses and going inside libraries to read books – Basile Starynkevitch Jul 12 '21 at 06:43
  • @BasileStarynkevitch how to use a queue, at main.py and get result, thats the only part i need – Benji Jul 12 '21 at 06:43
  • 1
    Benji: Stack Overflow is not intended to replace existing tutorials or documentation. That said, there are numerous question having to do with subject of using queues here. – martineau Jul 12 '21 at 06:58
  • `Thread` needs function's name without `()` - `Thread(target=speechrec)` - and later it will use `()` to run this function. At this moment you run `speechrec()` in main thread and it should block all code -like `result = speechrec()` `Thread(target=result)`. And if you want to use queue then create it in main thread and send to thread `q = queue.Queue()` `Thread(target=speechrec, args=(q,))` - creating it inside `speechrec()` is useless. – furas Jul 12 '21 at 07:02
  • @furas thank u bro I will try that.. – Benji Jul 12 '21 at 07:05
  • @furas it worked but it doesnt not return the value to main.py (spch) – Benji Jul 12 '21 at 07:08
  • you have to use `queue` for this - in `speechrec` use `q.put(...)` and in main `q.get()`. OR assign value to global variable - threads share global variables - and then you don't need `queue`. And I don't understand why you use `data = q.get()` in `speechrec` if you don't send any values to `speechrec` - and this `q.get()` may wait for value on queue and block all thread. – furas Jul 12 '21 at 07:23

0 Answers0