Private/Invoke-BridgeOCRGoogleCloud.ps1

<#
.SYNOPSIS
Αναλύει εικόνα με OCR μέσω Google Cloud.
 
.DESCRIPTION
Η Invoke-BridgeOCRGoogleCloud στέλνει Base64 εικόνα σε Google Vision OCR
και επιστρέφει τα αναγνωρισμένα αποτελέσματα.
 
.PARAMETER ApiKey
Το API Key της υπηρεσίας Google Cloud.
 
.PARAMETER ImageUri
Το URI της εικόνας που θα αναλυθεί.
 
.OUTPUTS
[pscustomobject[]] - Αποτελέσματα OCR σε μορφή αντικειμένων.
 
.EXAMPLE
Invoke-BridgeOCRGoogleCloud -ApiKey 'your-api-key' -ImageUri 'https://example.com/image.jpg'
 
.NOTES
Απαιτεί έγκυρο API Key και δημόσια προσβάσιμες εικόνες.
 
.PARAMETER Configuration
Η διαμόρφωση της BridgeWatcher που χρησιμοποιεί η συνάρτηση.
#>

function Invoke-BridgeOCRGoogleCloud {
    [CmdletBinding()]
    [OutputType([pscustomobject[]])]
    param (
        [Parameter(Mandatory)][ValidateNotNull()][PSCustomObject]$Configuration,
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][string]$ApiKey,
        [Parameter(Mandatory)][ValidateNotNullOrEmpty()][ValidateScript({
                if ([Uri]::IsWellFormedUriString($_, [UriKind]::Absolute)) {
                    $true
                } else {
                    throw "Η παράμετρος '$_' δεν είναι ένα έγκυρο απόλυτο URI."
                }
            })][string]$ImageUri
    )

    try {
        [byte[]]$imageBytes = Get-BridgeCachedImageBytes -Url $ImageUri -TimeoutSec $Configuration.Defaults.HttpRequestTimeout
        $base64Image = [System.Convert]::ToBase64String($imageBytes)
        $requestObject = @{
            requests = @(
                @{
                    image    = @{ content = $base64Image }
                    features = @(
                        @{
                            type       = 'DOCUMENT_TEXT_DETECTION'
                            model      = 'builtin/latest'
                            maxResults = 50
                        }
                    )
                }
            )
        }
        $requestBody = $requestObject | ConvertTo-Json -Depth 5
        $invokeOCRRequestSplat = @{
            ApiKey      = $ApiKey
            RequestBody = $requestBody
            Configuration = $Configuration
        }
        $apiResponse = Invoke-BridgeOCRRequest @invokeOCRRequestSplat
        $convertFromOCRResultSplat = @{
            ApiResponse   = $apiResponse
            Configuration = $Configuration
        }
        $result = ConvertFrom-BridgeOCRResult @convertFromOCRResultSplat
        if ($null -ne $result) {
            $md5 = [System.Security.Cryptography.MD5]::Create()
            try {
                $imageHash = [System.BitConverter]::ToString($md5.ComputeHash($imageBytes)) -replace '-'
            } finally {
                if ($null -ne $md5) { $md5.Dispose() }
            }
            if ($result -is [System.Collections.IDictionary]) {
                $result['ImageHash'] = $imageHash
            } else {
                $result | Add-Member -MemberType NoteProperty -Name 'ImageHash' -Value $imageHash -Force
            }
        }
        return $result
    } catch {
        $writeBridgeLogSplat = @{
            Stage   = 'Σφάλμα'
            Message = "❌ Η αίτηση OCR απέτυχε: $_"
            Level   = 'Warning'
        }
        Write-BridgeLog @writeBridgeLogSplat -Configuration $Configuration
        throw
    }
}