有没有办法将图像传递给 Microsoft 截图工具,让它执行文本提取,然后返回提取的内容

问题描述 投票:0回答:1

我正在开发一些使用 OCR 来娱乐的东西。我目前正在使用 PyTesseract,它的错误率非常高,因此我构建了一个用户验证功能,以便可以验证摄取的文本。当我测试不同的东西时,我厌倦了手动输入 PyTesseract 经常出错的一行,所以我进行了剪切并使用了剪切工具中的文本提取功能。准确率 100%。

这让我想到,如果我能找到一种方法让 python 调用截图工具,它会 A) 提高 OCR 的准确性。 B) 不需要让使用这个东西的人安装 Tesseract-OCR,这样他们的本地 python 就可以运行代码。

我一生都无法弄清楚如何做到这一点,或者是否可能。我在这里读到了一些关于如何在 PowerShell 中截取屏幕截图的内容,但这只是我的问题的一半。任何帮助都会很棒。

我目前的代码位于https://github.com/MElse1/Puzzle-Solving/blob/e591a77af27b7f7325b08edb725e12f31cc0f0c9/Word%20Search%20Solver/PyTesseract%20testing.py它很复杂,可能做得很糟糕,但它得到了即使由于 OCR 解决方案导致工作非常不准确,工作仍然完成。

自从我链接了我的 github 以来,这并不是为了任何严肃的事情,这只是一种以对我来说有趣的方式更好地利用编程逻辑的方法。这个解决方案最终是为了解决单词搜索。我将程序的逻辑部分放在我的 github 的其他地方,但我认为使用 OCR 构建列表来搜索单词会很酷。

python windows powershell
1个回答
0
投票

这是有关如何仅使用 Windows 内置库(Windows 8 及更高版本内置这些库)的 Powershell 进行 OCR 的示例

<#
    OCR sample using the built in WinRT API
#>

# FilePath of the image
$FilePath = "C:\test\OCRTestImage.png"

# OCR Language
$Language = 'en-us'

# assembly with the WindowsRT exention methods we need
Add-Type -AssemblyName System.Runtime.WindowsRuntime

# load the RT assemblies we need
[void][Windows.UI.Core.CoreWindow,Windows.UI.Core,ContentType=WindowsRuntime]
[void][Windows.Media.Ocr.OcrEngine,Windows.Media.Ocr,ContentType=WindowsRuntime]
[void][Windows.Storage.StorageFile,Windows.Storage,ContentType=WindowsRunTime]
[void][Windows.Graphics.Imaging.BitmapDecoder,Windows.Graphics.Imaging,ContentType=WindowsRuntime]

# get the AsTask generic extension method with the signature we need
$asTaskGeneric = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object { $_.Name -eq 'AsTask' -and $_.GetParameters().Count -eq 1 -and $_.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`1' })[0]

# non generic version (to check the status on the calls which return void/Iasyncblabla without a T)
$asTask = ([System.WindowsRuntimeSystemExtensions].GetMethods() | Where-Object {$_.Name -eq 'AsTask'})[5]

# Load the file
$StorageFileAsync = [Windows.Storage.StorageFile]::GetFileFromPathAsync($FilePath)
$StorageFile = $asTaskGeneric.MakeGenericMethod([Windows.Storage.StorageFile]).Invoke($null, $StorageFileAsync).Result

# convert file to a stream
$StreamAsync = $StorageFile.OpenReadAsync()
$Stream = $asTaskGeneric.MakeGenericMethod([Windows.Storage.Streams.IRandomAccessStreamWithContentType]).Invoke($null, $StreamAsync).Result

# And convert this to a bitmap => we'll have to create a softwareBitmap
$BitmapDecoderAsync = [Windows.Graphics.Imaging.BitmapDecoder]::CreateAsync($Stream)
$BitMap = $asTaskGeneric.MakeGenericMethod([Windows.Graphics.Imaging.BitmapDecoder]).Invoke($null, $BitmapDecoderAsync).Result
$SoftwareBitmapAsync = $BitMap.GetSoftwareBitmapAsync()
$SoftwareBitmap = $asTaskGeneric.MakeGenericMethod([Windows.Graphics.Imaging.SoftwareBitmap]).Invoke($null, $SoftwareBitmapAsync).Result


# OCR stuff => here we do the actual OCR stuff
# Create the OCR Engine object
$OCR = [Windows.Media.Ocr.OcrEngine]::TryCreateFromLanguage($Language)
# Do the actual recognition
$OCRResultAsync = $OCR.RecognizeAsync($SoftwareBitmap)
$OCRResult = $asTaskGeneric.MakeGenericMethod([Windows.Media.Ocr.OcrResult]).Invoke($null, $OCRResultAsync).Result

# output the result
$OCRResult

简而言之,我加载 UWP/RT 库,获取用于解包 iasync 调用的扩展方法的方法签名,之后它只是调用方法并解包结果。

© www.soinside.com 2019 - 2024. All rights reserved.