Okay got the recognizer working with python-osc:
pip3 install SpeechRecognition
pip3 install pyaudio (check if you need other extras, https://github.com/Uberi/speech_recognition )
python3 -m textblob.download_corpora
import speech_recognition as sr
from textblob import TextBlob
from pythonosc import udp_client
def interpret(input):
text = input
blob = TextBlob(text)
sentiment = blob.sentiment.polarity
print("sentiment is", sentiment)
return sentiment
if name == "main":listening = True mic = sr.Microphone() rec = sr.Recognizer() # Set OSC stuff ip = "127.0.0.1" port = 9999 client = udp_client.SimpleUDPClient(ip, port) while listening: with mic as source: rec.adjust_for_ambient_noise(source) rec.dynamic_energy_threshold = 3000 #the higher, the more actively it filters. 1000 recognizes more quiet stuff than 3000ß try: print("Listening...") audio = rec.listen(source) response = rec.recognize_google(audio) print(response) sentiment_value = interpret(response) # send to interpret function client.send_message("/sentiment", 2) print("sent OSC") except sr.UnknownValueError: print("Not recognized, sending default values...") client.send_message("/sentiment", 1) print("sent default OSC")
#speech stuff adapted from #https://www.youtube.com/watch?v=ELTVRkbyetk and #https://www.youtube.com/watch?v=R1SFP3t7Gwo
and then get the processing sketch:
//Take note that the baud rate is the same on both sides.
// Importing the library that will help us in sending and receiving the values from the Arduino
// You always need to import the serial library in addition to the VSync library
import vsync.; import processing.serial.;
// Below libraries will connect and send, receive OSC messages
import oscP5.; import netP5.;
// Creating the instances to connect and send, receive OSC messages
OscP5 oscP5;
NetAddress dest;
// We create a new ValueReceiver to receive values from the arduino and a new
// ValueSender to send values to arduino
ValueSender sender;
ValueReceiver receiver;
// The below variables will be syncronized with the Arduino and they should be same on the both sides.
public int output;
public int input = 2;
void setup()
{
// Starting the serial communication, the baudrate and the com port should be same as on the Arduino side.
Serial serial = new Serial(this, "COM5", 9600);
// Ininialize the ValueReceiver and ValueSender with this (to hook it to your sketch)
// and the serial interface you want to use.
sender = new ValueSender(this, serial);
receiver = new ValueReceiver(this, serial);
// Synchronizing the variables as on the Arduino side. The order should be same.
sender.observe("output");
receiver.observe("input");
// Starting the OSC Communication. listen on port 9999, return messages on port 9998
oscP5 = new OscP5(this, 9999);
dest = new NetAddress("127.0.0.1", 9998);
}
void draw() {
if (input == 1) {
sendOsc();
}
if (input == 2) {
sendOsc();
}
print("input: ");
println(input);
}
// Function to send OSC messages to browser
void sendOsc() {
OscMessage msg = new OscMessage("/socketio"); // tell the address
msg.add((float)input); // add the message
oscP5.send(msg, dest); //send the OSC message
}
// Recieve OSC messages from browser
void oscEvent(OscMessage theOscMessage) {
if (theOscMessage.checkAddrPattern("/sentiment") == true) {
// Receiving the output from browser
output = theOscMessage.get(0).intValue(); print("output: "); println(output);
IMPORTANT IS TO MATCH THE PORT FROM THE PYTHON TO THE LISTENING ONE IN THE PROCESSING SKETCH (in this case 9999)
And finally you need the arduino connected:
//Including the library that will help us in receiving and sending the values from processing
ValueReceiver<1> receiver; // Receiver Object
ValueSender<1> sender; // Sender Object
// The below variables will be syncronized with the Processing and they should be same on the both sides.
int output;
int input;
int ledPin = 7; //LED Pin
void setup()
{
/* Starting the serial communication because we are communicating with the
Arduino through serial. The baudrate should be same as on the processing side. */
Serial.begin(9600);
pinMode(LED_BUILTIN, OUTPUT);
// Synchronizing the variables with the processing. The variables must be int type.
receiver.observe(output);
sender.observe(input);
}
void loop()
{
// Receiving the output from the processing.
receiver.sync();
// Matching the received output to light up LED
if (output == 1)
{
digitalWrite(LED_BUILTIN, HIGH);
input = 1;
}
else if (output == 2)
{
digitalWrite(LED_BUILTIN, LOW);
input = 2;
}
sender.sync();
}
All of the above code needs to be refactored as I've mushed two things together so there are bits that are unnecessary and no browser involved