php exec不适用于带有

时间:2017-04-22 16:16:35

标签: php python google-speech-api

我对我的问题感到非常生气。

我正在尝试做什么:

  • 将“.flac”文件上传到我的服务器(可行)
  • 使用àphp脚本启动àpython脚本(不起作用)
  • Python脚本调用google语音API将语音转换为文本(从终端直接启动时工作
  • 在数据库中插入我的python处理结果(可行)

我尝试使用exec()system()和passthru()命令。

我在互联网上搜索了很长时间才找到解决方案,而且我明确地阻止了我的php似乎没有等待我的python程序发生(在10秒到5分钟之间)。

当我尝试使用另一个只在数据库中插入值的python脚本时,一切正常,这让我真的假设我的php exec()在结束之前杀死了我的python进程或类似的东西。 所以我的代码如下。

PHP代码:

<?php
#Define target of uploaded file
$target_dir = "/var/www/voice/voice_file/";

#create unique new name for file
$var_name = md5(uniqid(rand(), true));

#define file extension for new file
$var_name .= ".flac";

#define (temporalily) comment value --> will be replaced by form "comment"
$commentaire = "\"test d'un commentaire\"";

# define target
$target_file = $target_dir . basename($var_name);


#moove flac file to target
move_uploaded_file($_FILES["fileToUpload"]["tmp_name"], $target_file);


#execute python script to convert audio to text
ob_start();
passthru("python voice_file/convert_audio.py $target_file $commentaire");
$output = ob_get_clean();

?>

Python脚本。

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Apr 17 14:09:32 2017

@author: flo-linux
"""
###### parameters ###########
from pydub import AudioSegment
import MySQLdb as my
#from pydub.utils import mediainfo
from pydub.utils import make_chunks
import math
import speech_recognition as sr
import os
import sys
#################################
#%%########define variables######

commentaire_result=str(sys.argv[2])
text_result =""
GOOGLECREDENTIALS = r"""{MYAPIGOOGLECREDENTIALS}"""

####### Format audio file and grep audio parameters #####

flac_audio = AudioSegment.from_file(sys.argv[1], "flac")    
flac_audio.export("short.wav", format="wav")                
myaudio = AudioSegment.from_file("short.wav" , "wav")        
channel_count = myaudio.channels    #Get channels
sample_width = myaudio.sample_width #Get sample width
duration_in_sec = len(myaudio) / 1000 #Length of audio in sec
sample_rate = myaudio.frame_rate
bit_rate =16   
wav_file_size = (sample_rate * bit_rate * channel_count * duration_in_sec) / 8


#%%## Split audio for Google API limitataion #############

file_split_size = 10000000  # 10Mb OR 10, 000, 000 bytes
total_chunks =  wav_file_size // file_split_size + 1


chunk_length_in_sec = math.ceil((duration_in_sec * 10000000 ) /wav_file_size)   #in sec
chunk_length_ms = chunk_length_in_sec * 1000
chunks = make_chunks(myaudio, chunk_length_ms)


#%%##Export all of the individual chunks as wav files #######


for i, chunk in enumerate(chunks):
    chunk_name = "chunk{0}.flac".format(i)
    chunk.export(chunk_name, format="flac")
    ## Process each chunk in google Speech API.
    r = sr.Recognizer()
    with sr.AudioFile(chunk_name) as source:
        audio = r.record(source)
        try:
            text_result += (r.recognize_google_cloud(audio, credentials_json=GOOGLECREDENTIALS,language="fr-FR"))

        except sr.UnknownValueError:
            text_result += ("Google Cloud Speech could not understand audio")
        except sr.RequestError as e:
            text_result += ("Could not request results from Google Cloud Speech service; {0}".format(e))
    os.remove (chunk_name)
os.remove(short.wav)

#%% Insert result of voice to text in data base ##########
db = my.connect(host="localhost",user="my_user", passwd="my_passwd", db="my_db")
cursor = db.cursor()
try:
    cursor.execute("INSERT into data (Comment, Result) values (%s,%s)",(commentaire_result, text_result))
    db.commit()
except:
   db.rollback()

db.close()

我真的很感激一只手。 非常感谢

0 个答案:

没有答案