python - 如何使用 python 批量/批量转录 wav 文件?

标签 python ibm-cloud speech-recognition ibm-watson speech-to-text

我正在尝试使用我的 python 应用程序转录文件夹中的多个文件并加快进程。
目前我可以一次做一个文件 -

####RUN THIS PART FIRST#########
import json
from os.path import join, dirname
from ibm_watson import SpeechToTextV1
from ibm_watson.websocket import RecognizeCallback, AudioSource
import threading
from ibm_cloud_sdk_core.authenticators import IAMAuthenticator
import pandas as pd
authenticator = IAMAuthenticator('xxyyzz')

service = SpeechToTextV1(authenticator=authenticator)
service.set_service_url('https://api.us-east.speech-to-text.watson.cloud.ibm.com')

models = service.list_models().get_result()
#print(json.dumps(models, indent=2))

model = service.get_model('en-US_BroadbandModel').get_result()
#print(json.dumps(model, indent=2))

# This is the name of the file u need to change below
with open(join(dirname('__file__'), 'Call 8.wav'),
          'rb') as audio_file:
#    print(json.dumps(
    output = service.recognize(
    audio=audio_file,
    speaker_labels=True,
    content_type='audio/wav',
    #timestamps=True,
    #word_confidence=True,
    inactivity_timeout = -1,
    model='en-US_NarrowbandModel',
    continuous=True).get_result(),
    indent=2
  ############END################################  

# get data to a csv
########################RUN THIS PART SECOND#####################################
df0 = pd.DataFrame([i for elts in output for alts in elts['results'] for i in alts['alternatives']])

df1 = pd.DataFrame([i for elts in output for i in elts['speaker_labels']])

list(df0.columns) 
list(df1.columns) 
df0 = df0.drop(["timestamps"], axis=1)
df1 = df1.drop(["final"], axis=1)
df1 = df1.drop(['confidence'],axis=1)
test3 = pd.concat([df0, df1], axis=1)
#sentiment
transcript = test3['transcript']
transcript = transcript.dropna()
from vaderSentiment.vaderSentiment import SentimentIntensityAnalyzer
analyzer = SentimentIntensityAnalyzer()
text = transcript
scores = []
for txt in text:
    vs = analyzer.polarity_scores(txt)
    scores.append(vs)
data = pd.DataFrame(text, columns= ['Text'])
data2 = pd.DataFrame(scores)
final_dataset= pd.concat([data,data2], axis=1)
test4 = pd.concat([test3,final_dataset], axis=1)
test4 = test4.drop(['Text'],axis=1)
test4.rename(columns={'neg':'Negative'}, 
                 inplace=True)
test4.rename(columns={'pos':'Positive'}, 
                 inplace=True)
test4.rename(columns={'neu':'Neutral'}, 
                 inplace=True)

# This is the name of the output csv file
test4.to_csv("Call 8.csv")
我怎样才能做到这一点来转录一个文件夹中的多个文件而不是一次一个文件?我可以多次运行这个脚本,但我想自动化它,以便它从文件夹中提取 wav 文件并运行它。假设我的文件夹 C:\Python 中有 15 个音频 wav 文件。我想让它成为一个自动化过程,它将运行脚本并获得 15 个 csvs。 1 为每个与他们的 resp。输出。现在这个脚本可以工作,但必须为每个 wav 文件手动运行它以获得每个 wavs 输出 csv。
另外,作为第二个问题(抱歉!),有没有办法加快转录速度?将 wav 文件分解成更小的段并发送到 watson,但它没有用。我的引用是 - ( https://github.com/freelanceastro/interview-transcriber )

最佳答案

你试过多次运行这个脚本吗?您可以编写一个包装器,在类似于这样的子进程中启动此脚本:

import subprocess
import sys

processes = []
for _ in range(5):
    processes.append(subprocess.Popen([sys.executable, "/path/to/script.py"]))

# now wait for them to finish
for process in processes:
    process.wait()

关于python - 如何使用 python 批量/批量转录 wav 文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66451038/

相关文章:

python - 正则表达式 - 替换带有加号或括号的单词

python - 遍历字典

java - 使用java中的麦克风通过VOSK进行语音识别

python - 如何将实时音频 url 传递给 Google Speech to Text API

c# - 使用 System.Speech 将 mp3 文件转换为文本

Python dateutils 根据 iCalendar 格式打印重复规则(参见 RFC 5545)

python - 如何断言整个数据框只包含数字数据类型?

hadoop - 如何使用 Ambari UI 配置分析引擎使用 Hive LLAP?

Kubernetes Ingress - 重写路径

docker - 如何从 Bluemix 应用程序访问容器?