python - 检查不同节点(不同 IP 地址)、同一网络上的文件 - 可能吗?
问题描述
我有一小群 Raspberry Pi,都在同一个本地网络 (192.168.1.2xx) 上都运行 Python 3.7.3,一个 (R Pi CM3) 在 Raspbian Buster 上,另一个 (R Pi 4B 8gig) 在 Raspberry Pi 上操作系统 64。
我在一台设备(Pi 4B)上有一个文件,位于 /tmp/speech.wav,它是实时生成的:
192.168.1.201 - /tmp/speech.wav
我有一个在该设备上运行良好的脚本,它告诉我 .wav 文件的播放持续时间(以秒为单位):
import wave
import contextlib
def getPlayTime():
fname = '/tmp/speech.wav'
with contextlib.closing(wave.open(fname,'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = round(frames / float(rate), 2)
return duration
但是 - 需要对该持续时间信息进行操作的节点正在 192.168.1.210 的另一个节点上运行。我不能简单地将各种文件全部移动到同一个节点,因为发生了很多事情,事情在哪里是有原因的。
所以我需要知道的是如何改变我的方法,以便我可以将脚本引用更改为类似这样的伪代码:
fname = '/tmp/speech.wav @ 192.168.1.201'
这样的事情可能吗?在网上搜索似乎我遇到了数以百万计的人在寻找如何获取 IP 地址、修复多个 IP 地址问题、修复重复的 IP 地址问题......但我似乎还没有找到如何简单地检查文件正如我在这里描述的那样,在不同的 IP 地址上。我没有网络安全限制,所以任何设置都可以考虑。帮助将不胜感激。
解决方案
好的,这就是我最终决定的——而且效果很好。使用 ZeroMQ 进行消息传递,我具有获取 wav 播放时间的功能,另一个收集有关即将说出的语音的数据,然后在发送语音之前将所有数据发送到电机核心。运动核心处理时间问题以使下巴与语音同步。所以,我实际上并没有将生成 wav 并将 wav 播放时间的长度返回到最终使用它的节点上的代码,但事实证明消息传递足够快,所以有足够的时间空间来接收、处理和执行运动控制以完美匹配语音。在此处发布此内容,以防它对将来处理类似问题的人们有所帮助。
import time
import zmq
import os
import re
import wave
import contextlib
context = zmq.Context()
socket = context.socket(zmq.REP)
socket.bind("tcp://*:5555") #Listens for speech to output
print("Connecting to Motor Control")
jawCmd = context.socket(zmq.PUB)
jawCmd.connect("tcp://192.168.1.210:5554") #Sends to MotorFunctions for Jaw Movement
def getPlayTime(): # Checks to see if current file duration has changed
fname = '/tmp/speech.wav' # and if yes, sends new duration
with contextlib.closing(wave.open(fname,'r')) as f:
frames = f.getnframes()
rate = f.getframerate()
duration = round(frames / float(rate), 3)
speakTime = str(duration)
return speakTime
def set_voice(V,T):
T2 = '"' + T + '"'
audioFile = "/tmp/speech.wav" # /tmp set as tmpfs, or RAMDISK to reduce SD Card write ops
if V == "A":
voice = "Allison"
elif V == "B":
voice = "Belle"
elif V == "C":
voice = "Callie"
elif V == "D":
voice = "Dallas"
elif V == "V":
voice = "David"
else:
voice = "Belle"
os.system("swift -n " + voice + " -o " + audioFile + " " +T2) # Record audio
tailTrim = .5 # Calculate Jaw Timing
speakTime = eval(getPlayTime()) # Start by getting playlength
speakTime = round((speakTime - tailTrim), 2) # Chop .5 s for trailing silence
wordList = T.split()
jawString = []
for index in range(len(wordList)):
wordLen = len(wordList[index])
jawString.append(wordLen)
jawString = str(jawString)
speakTime = str(speakTime)
jawString = speakTime + "|" + jawString # 3.456|[4, 2, 7, 4, 2, 9, 3, 4, 3, 6] - will split on "|"
jawCmd.send_string(jawString) # Send Jaw Operating Sequence
os.system("aplay " + audioFile) # Play audio
pronunciationDict = {'teh':'the','process':'prawcess','Maeve':'Mayve','Mariposa':'May-reeposah','Lila':'Lala','Trump':'Ass hole'}
def adjustResponse(response): # Adjusts spellings in output string to create better speech output.
for key, value in pronunciationDict.items():
if key in response or key.lower() in response:
response = re.sub(key, value, response, flags=re.I)
return response
SpeakText="Speech center connected and online."
set_voice(V,SpeakText) # Cepstral Voices: A = Allison; B = Belle; C = Callie; D = Dallas; V = David;
while True:
SpeakText = socket.recv().decode('utf-8') # .decode gets rid of the b' in front of the string
SpeakTextX = adjustResponse(SpeakText) # Run the string through the pronunciation dictionary
print("SpeakText = ",SpeakTextX)
set_voice(V,SpeakTextX)
print("Received request: %s" % SpeakTextX)
socket.send_string(str(SpeakTextX)) # Send data back to source for confirmation
推荐阅读
- ruby-on-rails - Rails: Add form from different model
- vba - VBA循环遍历InputBox中给出的行数
- java - POST、DELTE、GET 操作 RestController spring boot 带复合键
- python-3.x - Python 3.7 - 从用户定义的函数中得到一个意外的 TypeError
- python - dask dataframe read parquet schema difference
- javascript - Javascript 代码无法正常工作
- android - After uninstall app and after some months again install app then push notifications receive or not?
- mysql - Can't create constraint key
- java - 从“Mon Jan 12 06:20:06 IST 1976”中提取日期
- android - 如何使屏幕在android中只显示白色和黑色,除了子视图