为什么Serial不是类serial的属性?

问题描述 投票:0回答:1

我想将一些外部设备连接到我的电脑(例如 Arduino),以便从中获取一些音频数据,但串行类不起作用。

Enter image description here

Error description

Imports, I imported both serial and pyserial

好吧,我尝试编码,但收到错误,Serial 不是串行类的属性。 我也很困惑 pyserialserial 之间是否有区别。我应该导入其中哪一个?

from transformers import pipeline
import time
# Write code to connect serial port to Arduino, take the audio data from Arduino, convert it to text
import serial
import speech_recognition as sr

# Configure the serial connection
ser = serial.Serial('COM3', 9600)  # Replace 'COM3' with the appropriate port for your Arduino

# Initialize the speech recognizer
r = sr.Recognizer()
sentiment_pipeline = pipeline("sentiment_analysis")
while True:
    # Read the audio data from Arduino
    audio_data = ser.readline().strip()

    # Convert the audio data to text
    audio_text = audio_data.decode('utf-8')  # Adjust the decoding format if necessary

    # Use speech recognition to recognize the text
    with sr.AudioFile(audio_text) as source:
        audio = r.record(source)  # Load the audio file

        try:
            recognized_text = r.recognize_google(audio)  # Perform speech recognition
            print("Recognized speech:", recognized_text)
            keyvaluepair = sentiment_pipeline(recognized_text)
            if(keyvaluepair['label'] == 'POSITIVE'):
                # Send a signal to the Arduino
                signal = "1"  # Change the signal value as needed
                ser.write(signal.encode())
            
            elif(keyvaluepair['label'] == 'NEGATIVE'):
                
                # Send a signal to the Arduino
                signal = "0"  # Change the signal value as needed
                ser.write(signal.encode())
            
            
        except sr.UnknownValueError:
            print("Unable to recognize speech.")
            time.sleep(4)#Wait for 4 seconds
            continue
        except sr.RequestError as e:
            print("Error occurred during speech recognition:", str(e))
python serial-port pyserial
1个回答
0
投票

您是否安装了模块

serial
而不是
pySerial
?对于很多人来说,这确实是一个令人困惑的名字。尝试一下

pip install pySerial
© www.soinside.com 2019 - 2024. All rights reserved.