Python文件在visualstudio中运行时的工作方式与使用anacondapromp运行时不同

2024-04-19 15:49:37 发布

您现在位置:Python中文网/ 问答频道 /正文

我对python很陌生,我一直在编写一个简单的程序,可以录制声音,修改一下,然后回放。我在visualstudio中写的,但是这个软件不适合我使用sounddevice并且说No module named '_sounddevice_data',但是当我在Anaconda提示符下使用python "path to this file"运行它时,它没有任何问题。这个包在我使用的Visual的python环境中-Anaconda 5.2.0。另一方面,从Anaconda提示符运行不会与tensorflow一起工作,因为在VisualYour CPU supports instructions that this TensorFlow binary was not compiled to use: AVX2中跳过了一些错误。更重要的是,当我使用simple.bat文件运行以下代码时,一切都很好:D

echo off

CALL  D:\Users\blaze\Anaconda3\Scripts\activate.bat D:\Users\blaze\Anaconda3
python C:\Users\blaze\source\repos\recorder\recorder\recorder.py

echo on 

我很感激你的帮助,因为我不明白发生了什么事。 代码如下:

import tensorflow as  tf
import numpy as np
import time
import glob
import os
model=tf.keras.models.load_model("D:\Projekt\ML\save\model.h5py",compile=True)

duration = 100  # seconds
frames = 8192
global a 
a=0

file_list_c = glob.glob(os.path.join(os.getcwd(), "Check", "*.txt"))
for file_path in file_list_c:
    with open(file_path) as f:       
        datax =np.genfromtxt(file_path)     
        datax=np.reshape(datax,(-1,8192))
        print(model.predict(datax))
        print(np.argmax(model.predict(datax)))

import sounddevice as sd
def callback(indata, outdata, frames, time, status):
    if status:
        print(status)
    print(np.argmax(model.predict(np.reshape(indata,(-1,8192)))))
    outdata[:] = indata


with sd.Stream(samplerate=48000,blocksize=8192,channels=1, callback=callback):
    sd.sleep(int(duration*1000))


time.sleep(100)

Tags: pathimportmodeltimeasnpanacondausers