简体   繁体   English

直接从 SFTP 服务器将音频文件加载到 Python 的语音识别模块(使用 Paramiko SFTPClient)

[英]Loading audio file to Speech recognition module for Python directly from SFTP server (using Paramiko SFTPClient)

I want to preface this with the fact that I am still very much a noob at Paramiko, so this might all be completely impossible.我想以这样一个事实作为序言,即我仍然是 Paramiko 的菜鸟,所以这可能是完全不可能的。

I want to open a .wav file on a server from my computer in order to do some speech recognition on it.我想从我的计算机在服务器上打开一个.wav文件,以便对其进行一些语音识别。 To do this, I create a Transport with Paramiko and use it to open the audio file on the server.为此,我使用 Paramiko 创建了一个 Transport 并使用它打开服务器上的音频文件。 Then I set this file as my source and use SpeechRecognition to print whatever's being said in the audio file.然后我将此文件设置为我的源并使用 SpeechRecognition 打印音频文件中所说的任何内容。 However, when I open the file located in remotefilepath , it is no longer recognised as an audio file, since I get the error message AssertionError: Source must be an audio source .但是,当我打开位于remotefilepath的文件时,它不再被识别为音频文件,因为我收到错误消息AssertionError: Source must be an audio source Printing type(file) I get <class 'paramiko.sftp_file.SFTPFile'> .打印type(file)我得到<class 'paramiko.sftp_file.SFTPFile'>

I want to just open the file on the server and do the speech recognition on it without having to first save the file onto my own computer.我只想在服务器上打开文件并对其进行语音识别,而不必先将文件保存到我自己的计算机上。 Is there any way for me to do this?我有什么办法可以做到这一点吗? Any help/advice is very much appreciated非常感谢任何帮助/建议

import paramiko
import speech_recognition as sr

remotefilepath = /path/to/file.wav
server_ip = 12.34.567.8
server_port = 22
transport = paramiko.Transport((server_ip, server_port))
transport.connect(username="foo", password="bar")
print "Connected to transport"
sftp = transport.open_sftp_client()
file = sftp.open(remotefilepath)

#print type(file)

r = sr.Recognizer()

with file as source:
    r.adjust_for_ambient_noise(source)
    audio = r.record(source)
    try:
        text = r.recognize_google(audio)
        print "You said: {}".format(text)
    except:
        print "Sorry, I could not understand."

file.close()
sftp.close()
transport.close()
print "Closed transport. Ending program" 

Python 2.7 on Windows 10. Windows 10 上的 Python 2.7。

adjust_for_ambient_noise takes an implementation of AudioSource as an argument. adjust_for_ambient_noise需要的实现AudioSource作为参数。 You are passing file-like object instead.您正在传递类似文件的对象。

I believe you can use AudioFile implementation of AudioSource instead.我相信您可以改用AudioSource AudioFile实现。 AudioFile can be created with file-like object: AudioFile可以使用类似文件的对象创建:

with sftp.open(remotefilepath) as file:
    with AudioFile(file) as source:
        r.adjust_for_ambient_noise(source)

尝试将文件从源复制到本地并开始处理它。

sftp.get(filepath, localpath)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM