c#呼叫流程如下
首先,需要在百度智能云平臺領取免費資源,并創建應用,此時會得到AppID,API Key和Secret Key這三個引數,在呼叫該應用時需要使用這三個引數,

在VS2019中,選擇專案,管理NuGet程式包,搜索并安裝Baidu.Ai

在檔案開頭參考有關內容

影像文字識別簡單的代碼段
var apiKey = "";
var apiSecretKey = "";
Ocr ocr = new Ocr(apiKey, apiSecretKey) { Timeout = 60000 };
byte[] bt = File.ReadAllBytes(imagePath);
JObject jobject = ocr.GeneralBasic(bt);
影像分類代碼段
var apiKey = "";
var apiSecretKey = "";
ImageClassify cla = new ImageClassify(apiKey, apiSecretKey) { Timeout = 60000 };
byte[] bt = File.ReadAllBytes(imagePath);
JObject jobject = cla.AdvancedGeneral(bt);
語音識別代碼段
var data = File.ReadAllBytes(pathname);
var AppID = "";
var apiKey = "";
var apiSecretKey = "";
var language = 1537; //普通話模型
var options = new Dictionary<string, object>
{
{"dev_pid", language}
};
Baidu.Aip.Speech.Asr asr = new Baidu.Aip.Speech.Asr(AppID, apiKey, apiSecretKey) { Timeout = 60000};
//JObject Asr.Recognize(byte[] data, string format, int rate, [Dictionary options<string, object> options = null])
var jobject = asr.Recognize(data, "pcm", 16000, options);
語音識別需要注意的地方(參考官方檔案):
1、支持的語音格式:原始 PCM 的錄音引數必須符合 16k、8k 采樣率、16bit 位深、單聲道,支持的格式有:pcm(不壓縮)、wav(不壓縮,pcm編碼)、amr(壓縮格式),
2、目前系統支持的語音時長上限為60s,請不要超過這個長度,否則會回傳錯誤,
dev_pid引數串列:
| 1537 | 普通話(純中文識別) | 語音近場識別模型 | 有標點 | 支持自定義詞庫 |
| 1737 | 英語 | 無標點 | 不支持自定義詞庫 | |
| 1637 | 粵語 | 有標點 | 不支持自定義詞庫 | |
| 1837 | 四川話 | 有標點 | 不支持自定義詞庫 | |
| 1936 | 普通話遠場 | 遠場模型 | 有標點 | 不支持 |
針對語音識別的格式限制,需要對現有音頻做格式轉化,我這里使用ffmpeg工具,
首先在官網下載exe檔案,完成后解壓,并放在固定目錄下不要再移動,

打開高級系統設定配置環境變數,將在系統變數找到Path,添加bin檔案夾的路徑,之后就可以在cmd命令列中直接呼叫ffmpeg、ffprobe和ffplay,


ffmpeg命令格式參考官方檔案,
查看音頻檔案資訊的命令如下:
ffprobe -show_data 音頻檔案
將MP3格式轉化為pcm,16000hz,單通道的命令如下:
ffmpeg -y -i 原始檔案.mp3 -acodec pcm_s16le -f s16le -ac 1 -ar 16000 輸出檔案.pcm
在音頻檔案中截取部分,命令如下(-ss為開始時間,-t為持續時間):
ffmpeg -y -i 輸入檔案.mp3 -ss 00:00:00 -t 00:01:00 輸出檔案.mp3
在c#中呼叫命令列執行ffmpeg工具
Process CmdProcess = new Process();
CmdProcess.StartInfo.UseShellExecute = false;
CmdProcess.StartInfo.RedirectStandardInput = true;
CmdProcess.StartInfo.RedirectStandardOutput = true;
CmdProcess.StartInfo.RedirectStandardError = true;
CmdProcess.StartInfo.CreateNoWindow = true;
CmdProcess.StartInfo.FileName = "cmd.exe";
CmdProcess.Start();//執行
string order = "";
CmdProcess.StandardInput.WriteLine(order + "&exit"); //向cmd視窗發送輸入資訊
CmdProcess.StandardInput.AutoFlush = true; //提交
CmdProcess.WaitForExit();//等待程式執行完退出行程
string result = CmdProcess.StandardOutput.ReadToEnd() + CmdProcess.StandardError.ReadToEnd();//輸出
CmdProcess.Close();//結束
最后,只需要簡單制作一個界面,將各個功能結合應用起來,就可以了,

轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/352059.html
標籤:其他
上一篇:自然語言處理之語音識別(2)
