执行在网页中使用PyAudio的python脚本

I have a python script which uses pyaudio to record audio using microphone and the code converts speech to text.It works perfectly fine on terminal. Here is a section of the python code :

stream = p.open(format=FORMAT,
                channels=CHANNELS,
                rate=RATE,
                input=True,
                output=True,
                frames_per_buffer=CHUNK)


async def send_audio(ws):
    # Starts recording of microphone
    print("* READY *")
    start = time.time()
    while True:
        try:
            #print(".")
            data = stream.read(CHUNK)
            await ws.send(data)
            ##if time.time() - start > 30:    # Records for n seconds
                ##await ws.send(json.dumps({'action': 'stop'}))
                ##return False
        except Exception as e:
            print(e)
            return False
        except KeyboardInterrupt:  # allow Ctrl + C to shut down the program
            await ws.send(json.dumps({'action': 'stop'}))
            return False

    # Stop the stream and terminate the recording
    stream.stop_stream()
    stream.close()
    p.terminate()


async def speech_to_text():
    async with websockets.connect(url, extra_headers=token_header) as conn:
        # Send request to watson and waits for the listening response
        send = await conn.send(json.dumps(params))
        rec = await conn.recv()
        print(rec)
        asyncio.ensure_future(send_audio(conn))
        w=0
        global lst
        # Keeps receiving transcript until we have the final transcript
        while True:
            try:
                rec = await conn.recv()
                parsed = json.loads(rec)
                transcript = parsed["results"][0]["alternatives"][0]["transcript"]

                #print(parsed)
                if "results" in parsed:
                    if len(parsed["results"]) > 0:
                        if "final" in parsed["results"][0]:
                            if parsed["results"][0]["final"]:
                                #conn.close()
                                #return False
                                print(transcript)
                                lst.append(transcript)
                                lst.append("
")
                                pass
            except KeyError:
                conn.close()
                return False

# Starts the application loop
loop = asyncio.get_event_loop()
loop.run_until_complete(speech_to_text())
loop.close()

Now,I want to call this script in a web page by clicking a button.I tried using exec() and shell_exec() in php as:

<div>
<?php
echo "<h3>POST:</h3>";
print_r($_POST);
if(isset($_POST["submit"])){
 print_r (shell_exec("cd / && python C:/xampp/htdocs/heyy/postibm.py"));

}
?>

<form method="post"
action="try.php ">
<input type="submit" name="submit"
value="Submit">
</form>
</div>

But on clicking the button,the page is stuck on loading screen and gives no response.If I replace "postibm.py" with simple "helloworld.py" printing hello world,it works.Any idea where the problem is ? or any better ways to solve this?

dtypj3308
dtypj3308 编辑。检查它是否澄清了我的怀疑?
大约 2 年之前 回复
douqie1884
douqie1884 exec()和shell_exec()应该可以正常工作。请发布您的代码。您可能没有足够的权限在服务器上执行应用程序
大约 2 年之前 回复
dselp3944
dselp3944 “它不起作用”是一个非常糟糕的解释。究竟发生了什么?错误信息?向我们展示您的尝试并正确解释会发生什么。
大约 2 年之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问