メインコンテンツへスキップ
見出し画像
Photo byt0m0y0

69. Windows Apps の音声 AI でコマンド実行

    前回の記事                       次回の記事

    はじめに

    前回に引き続き、UWP の SpeechRecognizer の機能を試していきます。
    今回はあらかじめ登録しておいた語彙の認識によるコマンド実行を試します。

    音声指示を試す

    継続的な音声認識が確認できたので、次は、音声コマンド認識を試してみます。あらかじめ登録された語彙を認識するという機能です。

    を参考にコードを組み立てます。今回は、”blue”、”red”、”green”という言葉を認識して、アプリの背景色をそれに合わせて変えるというコードを試してみます。

    GUI 定義

    まず、MainPage.xamlを、次のように修正して、コマンド認識用のUI要素を追加します。

    <Page
        x:Class="AppUWPSpeechRecognition.MainPage"
        xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
        xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
        xmlns:local="using:AppUWPSpeechRecognition"
        xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
        xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
        mc:Ignorable="d"
        Background="{ThemeResource ApplicationPageBackgroundThemeBrush}">
    
        <Grid>
            <Grid.RowDefinitions>
                <RowDefinition/>
                <RowDefinition/>
            </Grid.RowDefinitions>
            <Grid>
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30"/>
                    <RowDefinition/>
                    <RowDefinition Height="30"/>
                </Grid.RowDefinitions>
                <Button x:Name="buttonInitialize" Content="Initialize" HorizontalAlignment="Stretch" Margin="1" Click="buttonInitialize_Click"/>
                <TextBlock x:Name="resultTextBlock" Grid.Row="1" Visibility="Collapsed" HorizontalAlignment="Stretch"/>
                <Button x:Name="buttonRecognize" Grid.Row="2" Content="Start" HorizontalAlignment="Stretch" Margin="1" Click="buttonRecognize_Click"/>
                <TextBlock x:Name="tbRecognized" Grid.Row="3" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="1" TextWrapping="Wrap"/>
                <Button x:Name="buttonClearText" Grid.Row="4" HorizontalAlignment="Stretch" Margin="1" Click="buttonClearText_Click" IsEnabled="False"/>
            </Grid>
            <Grid Grid.Row="1">
                <Grid.RowDefinitions>
                    <RowDefinition Height="30"/>
                    <RowDefinition Height="30" />
                    <RowDefinition/>
                </Grid.RowDefinitions>
                <Button x:Name="buttonCommand" Content="Order" HorizontalAlignment="Stretch" Margin="1" Click="buttonCommand_Click"/>
                <TextBlock x:Name="tbOrder" Grid.Row="1" HorizontalAlignment="Stretch" />
                <Canvas x:Name="canvasOrder" Grid.Row="2" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Margin="1"/>
            </Grid>
        </Grid>
    </Page>

    buttonCommand で、コマンド認識の起動を行い、結果を tbOrder で表示し、canvasOrder の背景色を変えることにします。

    コマンド認識

     
     
    過去30年以上にわたる体験を更に哲学・数学・論理学で磨き上げた、概念モデリング、アーキテクチャ、設計、エンジニアリングに関する知識やテクニックを発信します。旅の紀行文もね。 Home Page : https://www.kae-made.jp

    あなたへのおすすめ