doudao7511 2018-05-30 08:42
浏览 61

执行在网页中使用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?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 #MATLAB仿真#车辆换道路径规划
    • ¥15 java 操作 elasticsearch 8.1 实现 索引的重建
    • ¥15 数据可视化Python
    • ¥15 要给毕业设计添加扫码登录的功能!!有偿
    • ¥15 kafka 分区副本增加会导致消息丢失或者不可用吗?
    • ¥15 微信公众号自制会员卡没有收款渠道啊
    • ¥15 stable diffusion
    • ¥100 Jenkins自动化部署—悬赏100元
    • ¥15 关于#python#的问题:求帮写python代码
    • ¥20 MATLAB画图图形出现上下震荡的线条