2021SummerTeam6

From MAE/ECE 148 - Introduction to Autonomous Vehicles
Revision as of 16:46, 5 September 2021 by 2021SummerTeam6 (talk | contribs) (Python Code)
Jump to: navigation, search

Team 6 Members

P1.jpg


From Left to Right

Kevin Bishara (MAE) | William Lynch (ECE) | Anwar Hsu (ECE)

Robot & 3D Modeling Designs

Our Robot

P2.png

Electronics Plate

Cad1.png

Camera Mount

Jetson Nano Case

Cad2.png Cad3.png

Autonomous Laps

    DonkeyCar Laps

Our autonomous laps for DonkeyCar can be found here.

    OpenCV/ROS Laps

Our OpenCV/ROS autonomous laps can be found here.

Final Project Overview

Our Python Code!

Python Code

<nowiki>
       # -*- coding: utf-8 -*-

""" Created on Mon Mar 29 14:31:14 2021

@author: Anwar """

    1. You need to install pyaudio to run this example
  1. pip install pyaudio
  1. When using a microphone, the AudioSource `input` parameter would be
  2. initialised as a queue. The pyaudio stream would be continuosly adding
  3. recordings to the queue, and the websocket client would be sending the
  4. recordings to the speech to text service

import pyaudio from ibm_watson import SpeechToTextV1 from ibm_watson.websocket import RecognizeCallback, AudioSource from threading import Thread from ibm_cloud_sdk_core.authenticators import IAMAuthenticator

try:

   from Queue import Queue, Full

except ImportError:

   from queue import Queue, Full
        1. Initalize queue to store the recordings ##

CHUNK = 1024

  1. Note: It will discard if the websocket client can't consumme fast enough
  2. So, increase the max size as per your choice

BUF_MAX_SIZE = CHUNK * 10

  1. Buffer to store audio

q = Queue(maxsize=int(round(BUF_MAX_SIZE / CHUNK)))

  1. Create an instance of AudioSource

audio_source = AudioSource(q, True, True)

        1. Prepare Speech to Text Service ########
  1. initialize speech to text service

authenticator = IAMAuthenticator('your API key') speech_to_text = SpeechToTextV1(authenticator=authenticator)

  1. define callback for the speech to text service

class MyRecognizeCallback(RecognizeCallback):

   def __init__(self):
       RecognizeCallback.__init__(self)
   def on_transcription(self, transcript):
       print(transcript)
   def on_connected(self):
       print('Connection was successful')
   def on_error(self, error):
       print('Error received: {}'.format(error))
   def on_inactivity_timeout(self, error):
       print('Inactivity timeout: {}'.format(error))
   def on_listening(self):
       print('Service is listening')
   def on_hypothesis(self, hypothesis):
       print(hypothesis)
   def on_data(self, data):
       print(data)
   def on_close(self):
       print("Connection closed")
  1. this function will initiate the recognize service and pass in the AudioSource

def recognize_using_weboscket(*args):

   mycallback = MyRecognizeCallback()
   speech_to_text.recognize_using_websocket(audio=audio_source,
                                            content_type='audio/l16; rate=44100',
                                            recognize_callback=mycallback,
                                            interim_results=True)
        1. Prepare the for recording using Pyaudio ##
  1. Variables for recording the speech

FORMAT = pyaudio.paInt16 CHANNELS = 1 RATE = 44100

  1. define callback for pyaudio to store the recording in queue

def pyaudio_callback(in_data, frame_count, time_info, status):

   try:
       q.put(in_data)
   except Full:
       pass # discard
   return (None, pyaudio.paContinue)
  1. instantiate pyaudio

audio = pyaudio.PyAudio()

  1. open stream using callback

stream = audio.open(

   format=FORMAT,
   channels=CHANNELS,
   rate=RATE,
   input=True,
   frames_per_buffer=CHUNK,
   stream_callback=pyaudio_callback,
   start=False

)

        1. Start the recording and start service to recognize the stream ######

print("Enter CTRL+C to end recording...") stream.start_stream()

try:

   recognize_thread = Thread(target=recognize_using_weboscket, args=())
   recognize_thread.start()
   while True:
       pass

except KeyboardInterrupt:

   # stop recording
   stream.stop_stream()
   stream.close()
   audio.terminate()
   audio_source.completed_recording()