14

Sau đây là cách để làm cho sức mạnh để nói.Powershell có thể nói, nhưng nó có thể viết nếu tôi nói không?

Add-Type -AssemblyName System.Speech 
$synthesizer = New-Object -TypeName System.Speech.Synthesis.SpeechSynthesizer 
$synthesizer.Speak('Hey, I can speak!') 

Thực ra tôi muốn làm ngược lại. Nếu tôi nói, có thể powershell chuyển đổi nó thành chữ cái.

Nếu tôi nói trong máy ghi âm "Này, tôi có thể nói", nó có chuyển thành văn bản không?

Nếu có thể, hãy hướng dẫn tôi cách đạt được điều đó?

+0

Không chắc chắn nếu nó có thể nghe, nhưng cảm ơn cho thấy làm thế nào nó có thể nói, upvoted –

+0

Lưu ý rằng đó là hai bỏ các vấn đề khác nhau. – Joey

Trả lời

9

Có vẻ như bạn có thể với System.Speech.Recognition. Ở đây thậm chí còn sử dụng ví dụ viết bằng PowerShell:

http://huddledmasses.org/control-your-pc-with-your-voice-and-powershell/

Liên kết này đã đi 404 vì vậy tôi đào nó ra khỏi con đường trở lại máy.

kiểm soát máy tính của bạn với giọng nói của bạn ... và PowerShell

By Joel 'Jaykul' Bennett vào ngày 25-Jun-2009

Bạn đã bao giờ muốn để có thể đặt câu hỏi máy tính của bạn và có nó trả lời bạn Bạn đã bao giờ tự hỏi liệu máy tính của bạn có thể giống như máy tính đang chạy Star Trek Enterprise, đáp ứng các truy vấn và lệnh thoại không? Bạn đã chơi với ZWave hoặc X10 tự động hóa nhà và nghĩ rằng điều khiển bằng giọng nói của thiết bị của bạn sẽ là hiển nhiên bước tiếp theo?

Vâng, ok ... Tôi sẽ không chỉ cho bạn cách bật đèn hoặc làm việc với tự động hóa tại nhà - nhưng đó là điều chính khiến tôi suy nghĩ về nội dung nhận dạng giọng nói này. Geek không muốn bước vào phòng khách và nói “Computer: Lights On” và nó có hoạt động không? Thay vào đó, là bước đầu tiên cho tất cả điều đó, hãy để tôi chỉ cho bạn cách sử dụng PowerShell để thực hiện các lệnh nhận dạng lệnh bằng giọng nói đơn giản… có thể kích hoạt bất kỳ tập lệnh PowerShell nào mà bạn quan tâm! Mã sau đây thực sự là một mô-đun, mặc dù bạn có thể chỉ cần phân tích nó thành tập lệnh và thực sự yêu cầu PowerShell 2.0 cho các sự kiện, mặc dù sẽ không quan trọng để cấu trúc lại nó để hoạt động trên PowerShell 1.0 bằng thư viện PSEventing của Oisin.

$null =[Reflection.Assembly]::LoadWithPartialName("System.Speech") 

## Create the two main objects we need for speech recognition and synthesis 
if(!$Global:SpeechModuleListener){## For XP's sake, don't create them twice... 
   $Global:SpeechModuleSpeaker =new-objectSystem.Speech.Synthesis.SpeechSynthesizer 
   $Global:SpeechModuleListener =new-objectSystem.Speech.Recognition.SpeechRecognizer 
} 

$Script:SpeechModuleMacros = @{} 
## Add a way to turn it off 
$Script:SpeechModuleMacros.Add("Stop Listening", { $script:listen =$false; Suspend-Listening }) 
$Script:SpeechModuleComputerName =${Env:ComputerName} 

function Update-SpeechCommands { 
#.Synopsis  
#  Recreate the speech recognition grammar 
#.Description 
#  This parses out the speech module macros,  
#  and recreates the speech recognition grammar and semantic results,  
#  and then updates the SpeechRecognizer with the new grammar,  
#  and makes sure that the ObjectEvent is registered. 
   $choices = new-objectSystem.Speech.Recognition.Choices 
   foreach($choice in$Script:SpeechModuleMacros.GetEnumerator()){ 
      New-ObjectSystem.Speech.Recognition.SemanticResultValue$choice.Key,  
                                                               $choice.Value.ToString() | 
         ForEach-Object{$choices.Add( $_.ToGrammarBuilder()) } 
   } 

   if($VerbosePreference -ne"SilentlyContinue") {$Script:SpeechModuleMacros.Keys |  
      ForEach-Object { Write-Host"$Computer, $_" -Fore Cyan } } 

   $builder = New-ObjectSystem.Speech.Recognition.GrammarBuilder"$Computer, " 
   $builder.Append((New-ObjectSystem.Speech.Recognition.SemanticResultKey"Commands",  
                                                         $choices.ToGrammarBuilder())) 
   $grammar = new-objectSystem.Speech.Recognition.Grammar$builder 
   $grammar.Name = "Power VoiceMacros" 

   ## Take note of the events, but only once (make sure to remove the old one) 
   Unregister-Event"SpeechModuleCommandRecognized" -ErrorAction SilentlyContinue 
   $null = Register-ObjectEvent$grammar SpeechRecognized ` 
               -SourceIdentifier"SpeechModuleCommandRecognized" ` 
               -Action { iex$event.SourceEventArgs.Result.Semantics.Item("Commands").Value} 
    
   $Global:SpeechModuleListener.UnloadAllGrammars() 
   $Global:SpeechModuleListener.LoadGrammarAsync($grammar ) 
} 

function Add-SpeechCommands { 
#.Synopsis 
#  Add one or more commands to the speech-recognition macros, and update the recognition 
#.Parameter CommandText 
#  The string key for the command to remove 
   [CmdletBinding()] 
   Param([hashtable]$VoiceMacros,[string]$Computer=$Script:SpeechModuleComputerName) 
    
   ## Add the new macros 
   $Script:SpeechModuleMacros +=$VoiceMacros  
   ## Update the default if they change it, so they only have to do that once. 
   $Script:SpeechModuleComputerName= $Computer  
   Update-SpeechCommands 
} 

function Remove-SpeechCommands { 
#.Synopsis 
#  Remove one or more command from the speech-recognition macros, and update the recognition 
#.Parameter CommandText 
#  The string key for the command to remove 
   Param([string[]]$CommandText) 
   foreach($command in $CommandText){$Script:SpeechModuleMacros.Remove($Command)} 
   Update-SpeechCommands 
} 

function Clear-SpeechCommands { 
#.Synopsis 
#  Removes all commands from the speech-recognition macros, and update the recognition 
#.Parameter CommandText 
#  The string key for the command to remove 
   $Script:SpeechModuleMacros = @{} 
   ## Default value: A way to turn it off 
   $Script:SpeechModuleMacros.Add("Stop Listening", { Suspend-Listening }) 
   Update-SpeechCommands 
} 


function Start-Listening { 
#.Synopsis 
#  Sets the SpeechRecognizer to Enabled 
   $Global:SpeechModuleListener.Enabled= $true 
   Say "Speech Macros are $($Global:SpeechModuleListener.State)" 
   Write-Host "Speech Macros are $($Global:SpeechModuleListener.State)" 
} 
function Suspend-Listening { 
#.Synopsis 
#  Sets the SpeechRecognizer to Disabled 
   $Global:SpeechModuleListener.Enabled= $false 
   Say "Speech Macros are disabled" 
   Write-Host "Speech Macros are disabled" 
} 

function Out-Speech { 
#.Synopsis 
#  Speaks the input object 
#.Description 
#  Uses the default SpeechSynthesizer settings to speak the string representation of the InputObject 
#.Parameter InputObject 
#  The object to speak  
#  NOTE: this should almost always be a pre-formatted string, 
#        most objects don't render to very speakable text. 
   Param([Parameter(ValueFromPipeline=$true)][Alias("IO")]$InputObject ) 
   $null =$Global:SpeechModuleSpeaker.SpeakAsync(($InputObject|Out-String)) 
} 

function Remove-SpeechXP { 
#.Synopis 
#  Dispose of the SpeechModuleListener and SpeechModuleSpeaker 
   $Global:SpeechModuleListener.Dispose();$Global:SpeechModuleListener = $null 
   $Global:SpeechModuleSpeaker.Dispose();$Global:SpeechModuleSpeaker = $null 
} 

set-alias asc Add-SpeechCommands 
set-alias rsc Remove-SpeechCommands 
set-alias csc Clear-SpeechCommands 
set-alias say Out-Speech 
set-alias listen Start-Listener 
Export-ModuleMember -Function * -Alias * -VariableSpeechModuleListener, SpeechModuleSpeaker 

Về cơ bản chỉ có một chức năng mà bạn cần phải lo lắng ở đây: New-VoiceCommands. Bạn chuyển cho nó một hashtable để ánh xạ các chuỗi tới các tập lệnh, và nếu bạn sử dụng -Listenswitch, tất cả đều có sẵn cho nó. Bạn cũng có thể gọiStart-Listening theo cách thủ công, và tất nhiên, tôi đã cung cấp chức năng Nói để làm cho máy tính dễ dàng hơn…

Khi máy tính đang “nghe” ... bạn chỉ cần nói tên của nó, đã theo dõi bởi một trong các lệnh của bạn. Tôi thích điều đó vì nó đảm bảo rằng tôi không chạy tập lệnh một cách tình cờ, nhưng bạn có thể xóa chuỗi ${Env:ComputerName}, từ đầu GrammarBuilder nếu bạn cho rằng nó không cần thiết hoặc bạn có thể mã hóa mã này đến tên khác ngoài tên máy tính của bạn , như nói "Hal, làm ơn, tôi cầu xin bạn ..." hoặc "Máy tính, làm ơn" hay bất cứ điều gì.

Bạn có thể làm rất nhiều thứ với điều này ... bất cứ điều gì, thực sự ... nhưng để cho bạn một ví dụ mà bạn có thể dễ dàng hiểu, tôi sẽ làm điều gì đó rất đơn giản, và máy tính của tôi chỉ trả lời một vài điều cơ bản các câu hỏi bằng cách nói lại với tôi, và sau đó thêm một vài lệnh để nó bắt đầu một ứng dụng hoặc một trang web.

Add-SpeechCommands @{ 
   "What time is it?" = { Say "It is $(Get-Date -f "h:mm tt")" } 
   "What day is it?"  = { Say $(Get-Date -f "dddd, MMMM dd") } 
   "What's running?"  = { 
      $proc = ps | sort ws -desc 
      Say $("$($proc.Count) processes, including $($proc[0].name), which is using " + 
            "$([int]($proc[0].ws/1mb)) megabytes of memory") 
   } 
} -Computer "Laptop" -Verbose  

Add-SpeechCommands @{ "Run Notepad"= { &"C:\Programs\DevTools\Notepad++\notepad++.exe"} } 
Add-SpeechCommands @{ "Check Gee Mail" = { Start-Process"https://mail.google.com" } } 

Bạn thấy thế nào dễ dàng như vậy? Bạn có thể sử dụng "Nói" để nói bất kỳ văn bản nào (mặc dù đôi khi sẽ nhận được kết quả tốt hơn), và bạn có thể gọi bất kỳ lệnh nào khác, bao gồm lệnh HttpRest để lấy dữ liệu web hoặc lệnh WASP cho tự động hóa cửa sổ hoặc lệnh PowerBoots để hiển thị đầu ra trong văn bản lớn hoặc lệnh ghép ngắn để điều khiển thiết bị X10 hoặc ZWave… bạn biết đấy, bất cứ điều gì 

2

Nhận dạng giọng nói vẫn là thử nghiệm công nghệ. Có một số khung .Net resources, ngay cả với example. Tuy nhiên, đừng hy vọng sẽ tạo ra một PowerShiri bất cứ lúc nào.

1

Công nghệ này là một quá khứ "thử nghiệm", nhưng nó không đáng tin cậy.

Tính năng này hiện có tùy chọn "Xem trước thư thoại" của UM. Kết quả có thể thay đổi từ khá tốt đến vui nhộn, tùy thuộc vào loa.

Các vấn đề liên quan