python - 如何解决程序挂起且没有错误,可能与 Google Cloud Storage 有关?
问题描述
这是我正在使用的代码片段。该程序正确地记录来自麦克风的音频并从中制作一个文件。然后,当您按下“停止录制”时,它全部变为空白并挂在那里一百万次吐出“它是否卡在这里”,最终我的 Mac 粉丝开始加入(这是内存泄漏吗?)。我希望它进入我的程序的下一部分(一切都应该工作)。我应该怎么做才能解决这个问题并让我的程序运行?我认为它可能涉及我正在使用的.json文件,但同样,我不知道如何修复它,即使它是潜在问题。任何帮助,将不胜感激。
附录:我正在将此程序从 Windows 移植到我的 Mac 上,并尝试确保不存在兼容性问题。该程序是用 Python 和 PyCharm 编写的(它也在 Windows 中的相同版本的 PyCharm 中运行。)
import os
from nltk.sentiment.vader import SentimentIntensityAnalyzer
from google.cloud import storage
from PyAudio import RecAUD
from google.cloud import speech_v1p1beta1 as speech
numberOfSpeakers = int(input("Please enter number of speakers (up to 6 people may participate): "))
chosenSpeaker = int(input("Please enter the # of the speaker to be analyzed: "))
guiAUD = RecAUD()
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'Users/davidkarimi/PycharmProjects/ATWM/Project ATWM-a83e05b28663.json'
def create_bucket(bucket_name):
"""Creates a new bucket."""
print("Is it getting stuck here?")
storage_client = storage.Client()
bucket = storage_client.create_bucket(bucket_name)
print('Bucket {} created'.format(bucket.name)) ## have program automatically name new buckets in unique ways to prevent naming conflicts
解决方案
我认为这不适用于 Mac from PyAudio import RecAUD
。以下代码在 Mac 上为我工作。
import pyaudio
import wave
from google.cloud import storage
FORMAT = pyaudio.paInt16
CHANNELS = 2
RATE = 44100
CHUNK = 1024
RECORD_SECONDS = 5
WAVE_OUTPUT_FILENAME = "file.wav"
audio = pyaudio.PyAudio()
# start Recording
stream = audio.open(format=FORMAT, channels=CHANNELS,
rate=RATE, input=True,
frames_per_buffer=CHUNK)
print( "recording...")
frames = []
for i in range(0, int(RATE / CHUNK * RECORD_SECONDS)):
data = stream.read(CHUNK)
frames.append(data)
print ("finished recording")
# stop Recording
stream.stop_stream()
stream.close()
audio.terminate()
waveFile = wave.open(WAVE_OUTPUT_FILENAME, 'wb')
waveFile.setnchannels(CHANNELS)
waveFile.setsampwidth(audio.get_sample_size(FORMAT))
waveFile.setframerate(RATE)
waveFile.writeframes(b''.join(frames))
waveFile.close()
os.environ['GOOGLE_APPLICATION_CREDENTIALS'] = 'key.json'
storage_client = storage.Client()
bucket = storage_client.create_bucket("bucket-name")
print("Bucket Created {}".format(bucket.name))
推荐阅读
- javascript - 下载然后上传图像而不存储它
- javascript - 为什么在使用类型断言时不检查缺失的属性?
- html - Angular ngIf 显示 1 个项目并隐藏其他多个项目
- c++ - 每当我尝试导入 graphics.h 时,我总是找不到 -lbgi
- r - 将库(xlsx)表格内容和库(rvg)可编辑图形内容合并到同一个 Excel 选项卡上
- go - 如何将 sitemap.xml 发布到我的网站?
- javascript - 从特定标题值的表中抓取数据并过滤特定行(Google App Script)
- pytorch - Pytorch Geometric:如何从 pytorch 几何数据对象列表在 colab 笔记本中创建一个小型临时数据集
- javascript - Aframe 未注册组件
- flutter - 颤动中带有复选框动态的TextFormField