0

我已经创建了我的表单识别器 AI,对其进行了训练,并收到了 modelID,但是当我实际在 Powershell 中实现它时,它会出错,告诉我它无法读取文件,所以我怀疑它与我正在发送的文件有关。

这是针对表单识别器 2.0 的,有什么建议吗?

cls

$aiFormRecognizerKey = '{apiKey}'
$aiFormRecognizerEndPoint = 'https://{Url}.cognitiveservices.azure.com/'
$aiModelToUse = 'f11f43a7-6207-4dc9-9e8a-fc58677047f1'

$headers = @{
    "Ocp-Apim-Subscription-Key" = $aiFormRecognizerKey
    "Content-Type" = "application/pdf"
}


$FormFields = @{
    "form-data" = Get-Item C:\temp\test3.pdf
    "type" = "application/pdf"
}


$analyzedDocumentLocation = (Invoke-WebRequest ($aiFormRecognizerEndPoint + 'formrecognizer/v2.0-    preview/custom/models/' + $aiModelToUse + '/analyze' ) -Method "POST" -Headers ($headers) -Body     $FormFields ).Headers.'Operation-Location'

$analyzedDocumentLocation

$uriTest = 'https://{url}.cognitiveservices.azure.com/formrecognizer/v2.0-    preview/custom/models/5b4cb7c4-406f-400d-b53e-7d50fecd4a1d/analyzeresults/b07b863e-0aa5-4e1d-9a64-    73eb18c1f793'
Invoke-WebRequest -uri $uriTest  -Method "GET" -Headers ($headers) 

编辑:

这是像我这样好奇的人的解决方案:

cls

$aiFormRecognizerKey = '{Key}'
$aiFormRecognizerEndPoint = 
'https://{MyEndPoint}.cognitiveservices.azure.com/'
$aiModelToUse = '{TrainedModelId}'


$headers = @{
    "Ocp-Apim-Subscription-Key" = $aiFormRecognizerKey
}

$analyzedDocumentLocation = (Invoke-WebRequest -InFile C:\temp\test1.pdf - 
  ContentType "application/pdf" -uri ($aiFormRecognizerEndPoint + 
'formrecognizer/v2.0-preview/custom/models/' + $aiModelToUse + '/analyze' ) 
-Method "POST" -Headers ($headers)).Headers.'Operation-Location'

$analyzedDocumentLocation

$uriTest = $analyzedDocumentLocation[0]

$FileStream.Close()
Start-Sleep -s 10

(Invoke-WebRequest -uri $uriTest  -Method "GET" -Headers ($headers)).Content
4

1 回答 1

0

看起来您的请求正文正在尝试使用multipart/form-datav2.0 中不再支持的内容(尽管如果是,您需要在Content-Type标头中使用该值)。

相反,您应该直接在 body 参数中传递文件内容。这是 PowerShell 的端到端示例:

# Config
$headers = @{ "Ocp-Apim-Subscription-Key" = "your_key" }
$endpoint = "https://your_region.cognitiveservices.azure.com/formrecognizer/v2.0-preview"

# Train
$body = @{
    "source" = "https://your_azure_storage_container_sas_url";
    "sourceFilter" = @{ "prefix" = "optional" }
} | ConvertTo-Json
$resp = Invoke-WebRequest "$endpoint/custom/models" -Method "POST" -Headers $headers -Body $body -ContentType "application/json"
$location = $resp.Headers.Location[0]
$modelId = $location.Substring($location.LastIndexOf('/') + 1)
Write-Output "ModelId: $modelId"

# Wait for training
$status = $null
do {
    Start-Sleep -Seconds 5
    $resp = Invoke-WebRequest $location -Headers $headers 
    $body = $resp.Content | ConvertFrom-Json
    $status = $body.modelInfo.status
    Write-Output "Training... $status"
} while ($status -eq "creating")
if ($status -ne "ready") {
    throw "Training failed"
}

# Analyze
$content = Get-Content -Raw /path/to/your/file.pdf -ReadCount 0
$contentType = "application/pdf"
$resp = $content | Invoke-WebRequest "$endpoint/custom/models/$modelId/analyze" -Method "POST" -Headers $headers -ContentType $contentType
$location = $resp.Headers.'Operation-Location'[0]

# Wait for analysis
$status = $null
do {
    Start-Sleep -Seconds 5
    $resp = Invoke-WebRequest $location -Headers $headers 
    $body = $resp.Content | ConvertFrom-Json
    $status = $body.status
    Write-Output "Analyzing... $status"
} while ($status -eq "notStarted" -or $status -eq "running")
if ($status -ne "succeeded") {
    throw "Analysis failed"
}

# Output
Write-Output $resp.Content

请注意,我正在$content将(PDF)管道传输到Invoke-WebRequest. 您也许还可以将文件加载到内存中并使用-Body参数传递。

希望这可以帮助!

于 2019-12-20T20:32:24.543 回答