Python calls Baidu Voice (speech recognition-bucket landlord voice card)

Source: Internet
Author: User

I. Overview

This article briefly introduces the basic use of the voice recognition of Baidu (in fact, when the landlord wants to get a card player and no money, grab a bag of what is not, had to make speech recognition)

Second, create the application

Open the Baidu Voice official website, product and use --- voice recognition--now use --Create an app

The following page appears

Follow the prompts to complete the final result

(PS: I just want to get a card, it's the name of a counter)

Click "View Key" on the right to note the app Id,api Key,secret key. The next step is to use

need to install module PIP install Baidu-aippip install Pyaudio

Speech Recognition Code

 fromAIP Import Aipspeech"""your APPID AK SK"""app_id='the app_id you wrote down.'Api_key='the Api_key you wrote down.'Secret_key='the Secret_key you wrote down.'Client=Aipspeech (app_id, Api_key, Secret_key) # Read file Def get_file_content (FilePath): With open (FilePath,'RB') () # identify local files Li=client.asr (Get_file_content ('01.PCM'),'PCM',8000, {    'LAN':'ZH',}) Print (LI) # get file recognition # from URL # client.asr ("','PCM',16000, {#     'URL':'HTTP://',#     'Callback':'',# })

Python Recording Code

Import Wave fromPyaudio Import Pyaudio,paint16framerate=8000Num_samples= -Channels=1Sampwidth=2 Time=2def save_wave_file (filename,data):" "Save the date to the Wavfile" " (filename,'WB') wf.setnchannels (Channels) wf.setsampwidth (sampwidth) wf.setframerate (framerate) wf.writeframes (b"". Join (data)) Wf.close () def my_record (): PA=Pyaudio () (format = paint16,channels=1, rate=framerate,input=True, Frames_per_buffer=num_samples) My_buf=[] Count=0     whilecount<time*5: #控制录音时间 (num_samples) my_buf.append (string_audio_data) Count+=1Print ('.') Save_wave_file ('01.PCM', My_buf) stream.close () chunk= thedef play (): (R"01.PCM",'RB') P=Pyaudio () (Format=p.get_format_from_width (Wf.getsampwidth ()), channels=wf.getnchannels (), rate=wf.getframerate (), output=True) whileTrue:data=wf.readframes (Chunk)ifdata=="": Breakstream.write (data) Stream.Close () p.terminate ()if__name__ = ='__main__': My_record () print ('over!') play ()

Effects such as:

Help Documentation:

Baidu Voice help document or manual

Third, PostScript

This code is not fully implemented, interested in self-organizing, playing landlords when the sound may be larger, because the recognition sometimes reported 3001 error, audio quality is too poor, but be beaten can not find me

Python calls Baidu Voice (speech recognition-bucket landlord voice card)

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.