IE-482-582 / spring2023

5 stars 8 forks source link

whisper code #14

Open cmurray3 opened 1 year ago

cmurray3 commented 1 year ago

#!/usr/bin/env python3

import whisper
import sounddevice as sd
from scipy.io.wavfile import write

# import rospy
# import ...String...
# import ...Twist...

print("loading model now.  This can be slow.")
model = whisper.load_model("tiny.en")
print("model loaded. \n")

fs = 44100  # Sample rate
seconds = 3  # Duration of recording
WAVE_OUTPUT_FILENAME = "output.wav"

# Define a publisher for Twist commands

# ===================================

def myCallback(msg):

    print("START SPEAKING (3 seconds max)...")
    myrecording = sd.rec(int(seconds * fs), samplerate=fs, channels=1)
    sd.wait()  # Wait until recording is finished
    print("DONE. \n")
    write(WAVE_OUTPUT_FILENAME, fs, myrecording)  # Save as WAV file 

    print('Transcription:')
    result = model.transcribe(WAVE_OUTPUT_FILENAME, fp16=False)

    myText = result["text"].lower()
    print(myText)

    twist = Twist()

    if ("forward" in myText):
        print("linear.x = 1.0")
        twist.linear.x = 1.0
    elif ("rotate left" in myText):
        print("angular.z = 0.2")
        twist.angular.z = 0.2
    else:
        print("unrecognized command")

    # publish the Twist command

'''
Subscribe to the "keys" topic
    Define a callback function

'''