Skip to content

Commit

Permalink
feat: 添加新的http+json接口调用SDK,并更新客户端Demo代码
Browse files Browse the repository at this point in the history
  • Loading branch information
nl8590687 committed Mar 19, 2022
1 parent 48c4164 commit 1a8b91a
Show file tree
Hide file tree
Showing 18 changed files with 1,068 additions and 91 deletions.
15 changes: 11 additions & 4 deletions ASRT_SpeechClient_WPF/ASRT_SpeechClient_WPF.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,12 @@
<OutputType>WinExe</OutputType>
<RootNamespace>ASRT_SpeechClient_WPF</RootNamespace>
<AssemblyName>ASRT_SpeechClient_WPF</AssemblyName>
<TargetFrameworkVersion>v4.6.1</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.7.2</TargetFrameworkVersion>
<FileAlignment>512</FileAlignment>
<ProjectTypeGuids>{60dc8134-eba5-43b8-bcc9-bb4bc16c2548};{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}</ProjectTypeGuids>
<WarningLevel>4</WarningLevel>
<AutoGenerateBindingRedirects>true</AutoGenerateBindingRedirects>
<TargetFrameworkProfile />
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Debug|AnyCPU' ">
<PlatformTarget>AnyCPU</PlatformTarget>
Expand All @@ -34,9 +35,6 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="ailemon.asrt">
<HintPath>..\ailemon.asrt\bin\Release\ailemon.asrt.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Xml" />
Expand Down Expand Up @@ -96,5 +94,14 @@
<ItemGroup>
<None Include="App.config" />
</ItemGroup>
<ItemGroup>
<SplashScreen Include="SplashScreen.png" />
</ItemGroup>
<ItemGroup>
<ProjectReference Include="..\ailemon.asrt\ailemon.asrt.csproj">
<Project>{7aedf08a-4fe9-45d8-b547-4a6e5db0671d}</Project>
<Name>ailemon.asrt</Name>
</ProjectReference>
</ItemGroup>
<Import Project="$(MSBuildToolsPath)\Microsoft.CSharp.targets" />
</Project>
6 changes: 3 additions & 3 deletions ASRT_SpeechClient_WPF/App.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<?xml version="1.0" encoding="utf-8"?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.6.1" />
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.7.2"/>
</startup>
</configuration>
</configuration>
11 changes: 11 additions & 0 deletions ASRT_SpeechClient_WPF/App.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,16 @@ namespace ASRT_SpeechClient_WPF
/// </summary>
public partial class App : Application
{
/*
protected override void OnStartup(StartupEventArgs e)
{
SplashScreen s = new SplashScreen("SplashScreen.png");
s.Show(false);
s.Close(new TimeSpan(0, 0, 3));
base.OnStartup(e);
}
*/
}
}
8 changes: 5 additions & 3 deletions ASRT_SpeechClient_WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,17 @@
xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
xmlns:local="clr-namespace:ASRT_SpeechClient_WPF"
mc:Ignorable="d"
Title="ASRT_SpeechClient_WPF" Height="700" Width="1000" MinWidth="900" MinHeight="600" Margin="0" WindowStartupLocation="CenterScreen">
Title="ASRT语音识别系统客户端WPF版" Height="700" Width="1000" MinWidth="900" MinHeight="600" Margin="0" WindowStartupLocation="CenterScreen">
<Grid Background="White">
<TextBox x:Name="text_note" HorizontalAlignment="Stretch" Margin="0,50,0,160" Text="" TextWrapping="Wrap" AcceptsReturn="True" VerticalAlignment="Stretch" BorderThickness="0,0,0,2" ScrollViewer.VerticalScrollBarVisibility="Auto" FontFamily="Microsoft YaHei" FontSize="108" Foreground="Black" Background="#66FFFFFF" FontWeight="Bold" BorderBrush="Black"/>

<Button x:Name="btn_start_speech_input" Content="开始语音输入" HorizontalAlignment="Left" VerticalAlignment="Bottom" Margin="100,0,0,50" Width="260" Height="80" Foreground="Black" FontSize="36" Background="#99DDDDDD" Click="btn_start_speech_input_Click" BorderBrush="Black" BorderThickness="2" />
<Button x:Name="btn_end_speech_input" Content="停止语音输入" HorizontalAlignment="Right" VerticalAlignment="Bottom" Margin="0,0,100,50" Width="260" Height="80" Foreground="Black" FontSize="36" Background="#99DDDDDD" Click="btn_end_speech_input_Click" BorderBrush="Black" BorderThickness="2"/>
<Button x:Name="btn_change_url" Content="修改URL" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0" Width="200" Height="50" Foreground="Black" FontSize="36" Background="#99DDDDDD" Click="btn_change_url_Click" BorderBrush="Black" BorderThickness="2"/>
<Button x:Name="btn_change_url" Content="修改接口配置" HorizontalAlignment="Right" VerticalAlignment="Top" Margin="0" Width="250" Height="50" Foreground="Black" FontSize="36" Background="#99DDDDDD" Click="btn_change_url_Click" BorderBrush="Black" BorderThickness="2"/>
<TextBox x:Name="MessageBox" HorizontalAlignment="Center" Height="100" Text=" 正在识别... " VerticalAlignment="Bottom" Width="400" Background="#66FFFFFF" FontSize="70" Foreground="Black" FontFamily="Microsoft YaHei" IsReadOnly="True" Margin="0,0,0,140" BorderBrush="Black" BorderThickness="2"/>
<TextBox x:Name="textbox_url" Height="50" Margin="0,0,200,0" TextWrapping="Wrap" Text="http://127.0.0.1:20000/" VerticalAlignment="Top" FontSize="36" Background="#7FFFFFFF" Foreground="Black" BorderBrush="Black" BorderThickness="2,2,0,2"/>
<TextBox x:Name="textbox_url" Height="50" Margin="0,0,250,0" TextWrapping="Wrap" Text="http/127.0.0.1/20000" VerticalAlignment="Top" FontSize="36" Background="#7FFFFFFF" Foreground="Black" BorderBrush="Black" BorderThickness="2,2,0,2"/>
<Label Content="西安电子科技大学 西安市大数据与视觉智能关键技术重点实验室" HorizontalAlignment="Left" Margin="10,0,0,0" VerticalAlignment="Bottom" FontStyle="Italic"/>
<Button x:Name="btn_recognite_file" Content="识别Wave文件" HorizontalAlignment="Left" Margin="462,633,0,0" VerticalAlignment="Top" Width="92" Height="30" Click="btn_recognite_file_Click"/>

</Grid>
</Window>
82 changes: 58 additions & 24 deletions ASRT_SpeechClient_WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using ailemon.asrt;
using Ailemon.Asrt;

namespace ASRT_SpeechClient_WPF
{
Expand All @@ -21,64 +21,98 @@ namespace ASRT_SpeechClient_WPF
/// </summary>
public partial class MainWindow : Window
{
SpeechRecognizer asr;
AsrtClientProxy _clientProxy;

string token = "qwertasd";
string filename_conf = "conf.txt";
string host = "127.0.0.1";
string port = "20001";
string protocol = "http";

string textBuffer = "";

public MainWindow()
{
InitializeComponent();

MessageBox.Visibility = Visibility.Collapsed;
System.Threading.Thread.Sleep(1000); //延迟启动1秒,以便人们看清闪屏上的字


string url = "http://127.0.0.1:20000/";

if (System.IO.File.Exists(filename_conf))
{
//url = System.IO.File.ReadAllText(filename_url);
string[] conf_lines = System.IO.File.ReadAllLines(filename_conf);
url = conf_lines[0];
token = conf_lines[1];
textbox_url.Text = url;
protocol = conf_lines[0];
host = conf_lines[1];
port = conf_lines[2];
textbox_url.Text = protocol + "/" + host + "/" + port;
}
asr = new SpeechRecognizer(url, token);
asr.OnReceiveText += SpeechRecognizer_OnReceiveText;
_clientProxy = new AsrtClientProxy(host, port, protocol);
_clientProxy.SetRecorderDevice(0);
_clientProxy.OnReceiveText += SpeechRecognizer_OnReceiveText;
Console.WriteLine("MainWindow运行");
}

private void btn_start_speech_input_Click(object sender, RoutedEventArgs e)
{
asr.Start();
//asr.Start();
_clientProxy.Start();
MessageBox.Visibility = Visibility.Visible;
}

private void btn_end_speech_input_Click(object sender, RoutedEventArgs e)
{
asr.StopAsync();
_clientProxy.StopAsync();
MessageBox.Visibility = Visibility.Collapsed;
}

private async void btn_change_url_Click(object sender, RoutedEventArgs e)
private void btn_change_url_Click(object sender, RoutedEventArgs e)
{
//text_note.Text += await asr.RecogniteFromFile("1.wav");
//text_note.ScrollToEnd();
if (!asr.isRecognizing)
if (!_clientProxy.IsRecognizing)
{
string url_new = textbox_url.Text;
asr = new SpeechRecognizer(url_new, token);
asr.OnReceiveText += SpeechRecognizer_OnReceiveText;
string[] config_arr = url_new.Split('/');
protocol = config_arr[0];
host = config_arr[1];
port = config_arr[2];
_clientProxy = new AsrtClientProxy(host, port, protocol);
_clientProxy.OnReceiveText += SpeechRecognizer_OnReceiveText;

System.IO.File.WriteAllText(filename_conf, url_new + "\n" + token);

System.IO.File.WriteAllText(filename_conf, protocol + "\n" + host + "\n" + port);
}
}

private void SpeechRecognizer_OnReceiveText(object sender, string text)
private void SpeechRecognizer_OnReceiveText(object sender, AsrtResult result)
{
//事件处理方法
text_note.Text += text;
if (result.Confirm)
{
textBuffer += result.Text;
text_note.Text = textBuffer;
}
else
{
text_note.Text = textBuffer + result.Text;
}

text_note.ScrollToEnd();
Console.WriteLine("recv: {0}, {1}", result.Confirm.ToString(), result.Text);
}

private async void btn_recognite_file_Click(object sender, RoutedEventArgs e)
{
string filename = "";
Microsoft.Win32.OpenFileDialog openFileDialog = new Microsoft.Win32.OpenFileDialog();
openFileDialog.Filter = "WAVE音频文件|*.wav";
openFileDialog.DefaultExt = "WAVE音频文件|*.wav";
if (openFileDialog.ShowDialog() == true)
{
filename = openFileDialog.FileName;

Ailemon.Asrt.BaseSpeechRecognizer sr = Ailemon.Asrt.SDK.GetSpeechRecognizer(host, port, protocol);
Ailemon.Asrt.AsrtApiResponse rsp = (Ailemon.Asrt.AsrtApiResponse)await sr.RecogniteFile(filename);
System.Console.WriteLine((string)rsp.Result);
AsrtResult result = new AsrtResult("\n" + (string)rsp.Result + "\n", true, rsp.StatusCode, rsp.StatusMessage);
SpeechRecognizer_OnReceiveText(sender, result);
}
}
}
}
60 changes: 26 additions & 34 deletions ASRT_SpeechClient_WPF/Properties/Resources.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

30 changes: 13 additions & 17 deletions ASRT_SpeechClient_WPF/Properties/Settings.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Binary file added ASRT_SpeechClient_WPF/SplashScreen.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading

0 comments on commit 1a8b91a

Please sign in to comment.