gpt4 book ai didi

Powershell可以说话,但如果我说话,它可以写吗?

转载 作者:行者123 更新时间:2023-12-03 12:57:10 26 4
gpt4 key购买 nike

下面是让powershell说话的方法。

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

其实我想做相反的事情。如果我说话,powershell可以将其转换为字母。

如果我在录音机中说“嘿,我会说话”,它会转换成文本吗?

如果可能,请指导我如何实现它?

最佳答案

看起来你可以使用 System.Speech.Recognition .这里甚至是用 PowerShell 编写的示例用法:

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

这个链接变成了 404,所以我把它从机器后面挖了出来。

用你的声音控制你的电脑……和 PowerShell

作者:Joel 'Jaykul' Bennett,2009 年 6 月 25 日

你有没有想过能够问你的电脑问题并让它大声回答你?你有没有想过你的电脑是否可以更像运行星际迷航企业版的电脑,响应语音查询和命令?您是否玩过 ZWave 或 X10 家庭自动化,并认为您的设备的语音控制将是一个明显的下一步?

好吧,好吧……我不会向你展示如何开灯或使用家庭自动化——但这是让我一直在思考语音识别的主要内容。哪个极客不想走进客厅并说“计算机:灯亮”并让它工作?

相反,作为所有这一切的第一步,让我向您展示如何使用 PowerShell 执行简单的语音命令识别脚本……它可以触发您想要编写的任何 PowerShell 脚本!下面的代码实际上是一个模块,尽管您可以将其作为脚本进行点源,并且它确实需要 PowerShell 2.0 来处理事件,尽管使用 Oisin 的 PSEventing 库重构它以在 PowerShell 1.0 上工作是微不足道的。

$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-Object System.Speech.Synthesis.SpeechSynthesizer
$global:SpeechModuleListener = New-Object System.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-Object System.Speech.Recognition.Choices
foreach ($choice in $script:SpeechModuleMacros.GetEnumerator()) {
New-Object System.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-Object System.Speech.Recognition.GrammarBuilder"$Computer, "
$builder.Append((New-ObjectSystem.Speech.Recognition.SemanticResultKey"Commands", $choices.ToGrammarBuilder()))
$grammar = New-Object System.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

这里基本上只需要担心一个功能:New-VoiceCommands。您将一个哈希表传递给它,它将字符串映射到脚本 block ,如果您使用 -Listenswitch,这就是它的全部内容。你也可以手动调用Start-Listening,当然我还提供了Say功能,方便电脑说话……

一旦计算机“听”...您只需说出它的名称,然后是您的命令之一。我喜欢这样,因为它确保我不会意外运行脚本,但您可以删除 ${Env:ComputerName},如果您认为没有必要,则从 GrammarBuilder 开头的字符串开始,或者您可以将其硬编码为计算机名称以外的其他名称,例如说“Hal,拜托,我求求你......”或“计算机,请”或其他?

你可以用这个做很多事情......任何事情,真的......但是给你一个你可以很容易理解的例子,我会做一些非常简单的事情,让我的电脑只回答几个基本问​​题回复我,然后添加一些命令让它启动应用程序或网页。
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" }
}

你知道这有多容易吗?您可以使用“说”来说出任何文本(尽管某些文本会比其他文本获得更好的结果),并且您可以调用任何其他 powershell 命令,包括获取 Web 数据的 HttpRest 命令,或用于 Windows 自动化的 WASP 命令,或用于显示的 PowerBoots 命令以大文本输出,或用于控制 X10 或 ZWave 设备的 cmdlet……你知道吗?

关于Powershell可以说话,但如果我说话,它可以写吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9361594/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com