91 lines
3.9 KiB
PowerShell
91 lines
3.9 KiB
PowerShell
function Get-OCR {
|
|
<#
|
|
.SYNOPSIS
|
|
Runs Windows 10 OCR on an image.
|
|
.DESCRIPTION
|
|
Takes a path to an image file, with some text on it.
|
|
Runs Windows 10 OCR against the image.
|
|
Returns an [OcrResult], hopefully with a .Text property containing the text
|
|
.PARAMETER -Path
|
|
Path to an image file
|
|
.EXAMPLE
|
|
Get-OCR -Path 'c:\test.bmp'
|
|
#>
|
|
[CmdletBinding()]
|
|
param (
|
|
[Parameter(Mandatory = $true, ValueFromPipeline = $true, ValueFromPipelineByPropertyName = $true, Position = 0, HelpMessage = 'Path to an image file, to run OCR on')]
|
|
[ValidateNotNullOrEmpty()]
|
|
$Path
|
|
)
|
|
|
|
Begin {
|
|
# Add the WinRT assembly, and load the appropriate WinRT types
|
|
Add-Type -AssemblyName System.Runtime.WindowsRuntime
|
|
$null = [Windows.Storage.StorageFile, Windows.Storage, ContentType = WindowsRuntime]
|
|
$null = [Windows.Media.Ocr.OcrEngine, Windows.Foundation, ContentType = WindowsRuntime]
|
|
$null = [Windows.Foundation.IAsyncOperation, Windows.Foundation, ContentType = WindowsRuntime]
|
|
$null = [Windows.Graphics.Imaging.SoftwareBitmap, Windows.Foundation, ContentType = WindowsRuntime]
|
|
$null = [Windows.Storage.Streams.RandomAccessStream, Windows.Storage.Streams, ContentType = WindowsRuntime]
|
|
|
|
|
|
$ocrEngine = [Windows.Media.Ocr.OcrEngine]::TryCreateFromUserProfileLanguages()
|
|
|
|
|
|
|
|
|
|
$getAwaiterBaseMethod = [WindowsRuntimeSystemExtensions].GetMember('GetAwaiter').Where({ $PSItem.GetParameters()[0].ParameterType.Name -eq 'IAsyncOperation`'IAsyncOperation`1'' }, ''First')[0]'
|
|
Function Await {
|
|
param($AsyncTask, $ResultType)
|
|
$getAwaiterBaseMethod.MakeGenericMethod($ResultType).Invoke($null, @($AsyncTask)).GetResult()
|
|
}
|
|
}
|
|
|
|
Process {
|
|
foreach ($p in $Path) {
|
|
# .Net method needs a full path, or at least might not have the same relative path root as PowerShell
|
|
$p = $ExecutionContext.SessionState.Path.GetUnresolvedProviderPathFromPSPath($p)
|
|
|
|
$params = @{
|
|
AsyncTask = [Windows.Storage.StorageFile]::GetFileFromPathAsync($p)
|
|
ResultType = [Windows.Storage.StorageFile]
|
|
}
|
|
$storageFile = Await @params
|
|
|
|
$params = @{
|
|
AsyncTask = $storageFile.OpenAsync([Windows.Storage.FileAccessMode]::Read)
|
|
ResultType = [Windows.Storage.Streams.IRandomAccessStream]
|
|
}
|
|
$fileStream = Await @params
|
|
|
|
$params = @{
|
|
AsyncTask = [Windows.Graphics.Imaging.BitmapDecoder]::CreateAsync($fileStream)
|
|
ResultType = [Windows.Graphics.Imaging.BitmapDecoder]
|
|
}
|
|
$bitmapDecoder = Await @params
|
|
|
|
$params = @{
|
|
AsyncTask = $bitmapDecoder.GetSoftwareBitmapAsync()
|
|
ResultType = [Windows.Graphics.Imaging.SoftwareBitmap]
|
|
}
|
|
$softwareBitmap = Await @params
|
|
|
|
# Run the OCR
|
|
$ocrResult = Await $ocrEngine.RecognizeAsync($softwareBitmap) ([Windows.Media.Ocr.OcrResult])
|
|
|
|
<# **CHANGED: Extract text and coordinates** #>
|
|
$ocrResult.Lines | ForEach-Object {
|
|
$_.Words | ForEach-Object {
|
|
[PSCustomObject]@{
|
|
Text = $_.Text
|
|
X = $_.BoundingRect.X
|
|
Y = $_.BoundingRect.Y
|
|
Width = $_.BoundingRect.Width
|
|
Height = $_.BoundingRect.Height
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
Get-OCR "C:\LDPLayer\LDPlayer9\screenshot.png" |