All code for this project has been uploaded to CSDN.
Hardware:
1. Run speech recognition on your computer with Ubuntu 19.0 and python 3.7;
2. SCM uses stm32f103rct6, which is the mini-development board of atoms.
Software:
3. It mainly uses API interface, thread and serial communication of speech recognition of Baidu.
4. The implementation process only uses the modules at the basic level and the basic methods, which are easy to understand and easy to upgrade and develop in the future.
5. Develop with pycham, and later changes can run directly on the terminal;
6. SCM uses codemx to configure. After configuring it, only a few changes have been made. It realizes the function of serial communication lighting. The specific code and configuration refer to my previous blog: Tian Xiaohua voice robot (2) use serial port for data transfer with single-chip computer and python seria0l module for basic serial data transfer function
..........................................................................................
Description of the running process (a simple description without video recording ^^):After running, the computer controls the light on the MCU development board to flash, and then enters the speech recognition. The computer will play a jingle sound and start recording.Say "Turn on the light" to the computer. If you recognize the success, the computer will play the voice "OK, turn on the light for you".Single-chip computer lights will all turn on.If "On Light" is not recognized, it will play "I can't hear you".After one second, play the jingle again, enter the off-light speech recognition, and turn on the light as well.End of program.During speech recognition operation, the serial information sent by the computer will be sent back through the serial port and printed on the computer.
..........................................................................................
Code explanation:
...Each module has been commented in detail for easy understanding in the future!After the main thread runs, a child thread is required to receive information from the serial port.The actual purpose is only for convenience, not for practical work, and needs to be improved in the future.
...I need users to register their own account on Baidu. I also wrote a blog before the specific tutorial: Tian Xiaohua Voice Robot (1) Using Baidu Voice API to implement basic speech conversion Python 3.7, ubuntu19 system
...This is my first python on Ubuntu system. When I built my own modules on win pycharm before, I used import to import the modules I built. As long as I put the files under the same working directory, there will be no problem, but I can't import them on Ubuntu. Finally, I only refer to other people's solutions:
How to import modules written by yourself in python
ImportError on python module and import: No module named'xxx'
...Thank you [for your love]
Main module code
Filename:
mian.py
#!/usr/bin/python3.7 # coding=utf-8 import threading import time import sys """Self-modeled modules need to import module paths manually!""" sys.path.append("~/2PROGRAM/1-python/20.1.30-Main Procedure for Tien Xiaohua Voice Robot/program/main") """Serial Communication Control Robot Motion Module""" import contro_robot """Speech Recognition Module""" import speak """Construct the body of the robot, including the control of changes in general and related attributes of the robot""" class robot(): def __init__(self,name): self.name = name self.local_value = ['10', '01', '11','00'] self.Init_set_serial() #Serial data connection initialization """Serial initialization, starting to let the serial port send data, etc.""" def Init_set_serial(self): self.Serial_port = contro_robot.SerialControRobot() """Set up separate threads to receive serial information""" self.Serial_seceve_robot = contro_robot.SerialReceveRobot() serial_seceve_robot_thread = threading.Thread(target=self.Serial_seceve_robot.recive_serial, name='Child Thread') # Create Subthread serial_seceve_robot_thread.start() # Thread Start """Test program,No practical significance,unexecuted""" def fun_1(self): while True: time.sleep(0.5) self.Serial_port.sand_message(self.local_value[0]) time.sleep(0.5) self.Serial_port.sand_message(self.local_value[1]) """Program Execution Entry""" if __name__ == "__main__": Tianxiaohua = robot("tiuan") Tianxiaohua.Serial_port.sand_message('10') time.sleep(1) Tianxiaohua.Serial_port.sand_message('01') time.sleep(1) Speak = speak.speaker() a = Speak.Sound_recording() if(a == "turn on the light"): Speak.distinguish("Okay, the lights are on for you") Speak.play_sound() Tianxiaohua.Serial_port.sand_message('11') else: Speak.distinguish("I didn't hear you clearly") Speak.play_sound() a = Speak.Sound_recording() if (a == "Turn off the lights"): Speak.distinguish("Turn off the light oh") Speak.play_sound() Tianxiaohua.Serial_port.sand_message('00') else: Speak.distinguish("I didn't hear you clearly") Speak.play_sound()
............................................................................................................
...The serial debugging process encountered the problem that serial port receive and send cannot use the same serial port to instantiate the object. I did not continue to research, but by establishing a serial port send object and a serial port receive object, and the two objects run independently in two threads. When data is processed later, the information received and sentNeed to be handled together, then the data management problems seen by threads will be a hassle!!If the reader has a good way to be private [loving]
...The following is a series of service modules:
Filename:
contro_robot.py
#!/usr/bin/python3.7 #encoding:utf-8 import time import serial.tools.list_ports # """Serial Send and Receive are opened in two classes and run in two threads //During the actual test, it is found that the loss of serial information will occur when the same serial object is used for serial port reading and sending. //Two serial port objects need to be instantiated in two subthreads, one for sending and one for receiving''. """Send data class to serial port""" class SerialControRobot(): def __init__(self): self.plist = list(serial.tools.list_ports.comports()) if len(self.plist) <= 0: print("No port found!") else: self.plist_0 = list(self.plist[0]) serialName = self.plist_0[0] self.serialFd = serial.Serial(serialName, 9600, timeout=60) print("Available Port Names>>>"+self.serialFd.name) self.ser = serial.Serial(self.serialFd.name, 115200) # Set the first serial number, baud rate, read timeout settings print("In Normal Connection") # '''PC Send data to single-chip computer through serial port''' def sand_message(self,message): message = str(message) self.ser.write((message+'\r\n').encode()) # """Test method, no practical significance""" def fun(self): while True: self.sand_message('10') time.sleep(0.2) self.sand_message('01') time.sleep(0.2) # """Accept data class from serial port""" class SerialReceveRobot(): def __init__(self): self.plist = list(serial.tools.list_ports.comports()) if len(self.plist) <= 0: print("No port found!") else: self.plist_0 = list(self.plist[0]) serialName = self.plist_0[0] self.serialFd = serial.Serial(serialName, 9600, timeout=60) print("Available Port Names>>>" + self.serialFd.name) self.ser = serial.Serial(self.serialFd.name, 115200) # Set the first serial number, baud rate, read timeout settings print("In Normal Connection") # '''PC Side-to-Singlechip Data Received by Serial Port''' def recive_serial(self): ser = serial.Serial( self.serialFd.name, 115200) # Set the first serial number, baud rate, read timeout settings while True: count = ser.inWaiting() if count > 0: print("PC Information received by the end serial port:", ser.read(count)) #recive = ser.read(count) #if recive == b'01\r\n': #print("Identify Successfully") """ a = ControRobot() a.fun() """
................................................................................................
Below is a module based on Baidu speech recognition.Referring to the previous blog I wrote, I did not repeat it: Tian Xiaohua Voice Robot (1) Using Baidu Voice API to implement basic speech conversion Python 3.7, ubuntu19 system
Filename:
speak.py
#!/usr/bin/python3.7 # coding=utf-8 from aip import AipSpeech #Import API of Baidu Voice!!!: pip install baidu-aip import os # """ //There are two main ways to establish a class based on Baidu speech recognition api: 1,Sound_recording: Direct recording, where the return value is the recognized speech-to-speech string 2,Sound_recording: The input value for this method is the string you want to convert to speech, which converts the string directly to speech and plays it out """ class speaker(): def __init__(self): app_id = "18342916" API_Key = "ZUmYyI8ViFGDL5cpDEbC1hlA" Secret_Key = "KcgQsVbBQFSTbq1Umj4EK9kw4Y2P8MvH" self.client = AipSpeech(app_id, API_Key, Secret_Key)#Substituting relevant account information creates a speech recognition object clien self.length = 2 #Length of recording, in seconds, specified when creating a recording file # """Use the microphone for recording and the return value is the string that is converted if it is recognized""" def Sound_recording(self): os.system("aplay resource/ding.wav")#Use the aplay program to turn on the recording and play the jingle os.system("arecord -d %d -r 16000 -c 1 -t wav -f S16_LE resource/record.wav" % (self.length,) )#Recording is done using the recording software arecore, and the relevant recording parameters are set as shown inside the function.Baidu uses.wav as a file os.system("aplay resource/ding.wav") with open("resource/record.wav", 'rb') as fp:#Open the just recorded audio file, upload it to Baidu and get a dictionary with relevant return values result = self.client.asr(fp.read(), 'wav', 16000, {'dev_pid': 1536, }) """When you print the result of a recording, you will actually encounter a situation where there is no result from the network or recording. try Response""" try: print("___Recognition results___: ",result) print(result['result'][0]) return result['result'][0] except: print("___Identify unsuccessful____") # """Pass the identified content to Baidu as a string,Generate playable MP3 File, no return value""" def distinguish(self,Speaker_say_message): """result Binary data representing the conversion of text to speech, from Baidu conversion, requires a good network connection""" result = self.client.synthesis(Speaker_say_message, #Text to be translated "zh",#Representation is in Chinese 1, #Represents the first {"vol":5,#Represents volume "spd":5,#Represents sound speed "pit":9,#Indicative Intonation "per":4#1 is a girl, 2 is a boy, 3 is at large, 4 is Lolly }) with open("resource/sprak.mp3", "wb") as f:#Create and write MP3 file objects f.write(result)#Write the converted binary voice file to the file self.play_sound()#Play the conversion results # """Play voice""" def play_sound(self):#Play converted content os.system("mpg123 resource/sprak.mp3")#Here mpg123 is a program plugin that can be used to play MP3 files """ a = speaker() x = a.Sound_recording() if(x == "Turn off the lights"): a.distinguish("Okay, the lights are being turned off for you") a.play_sound() else: a.distinguish("I didn't hear you clearly") a.play_sound() """
There are many unresolved problems after the whole system is built, such as data transfer between threads, serial communication can not be in the same thread, function implementation is too simple, and so on. It can be regarded as a starting point.
Tian Xiaohua Voice Robot is continuously updating...
End!
23333333333333