无法提交要分析的表格

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

我创建了我的Form Recognizer AI,对其进行了培训,并收到了modelID,但是当我实际在Powershell中实现此功能时,它出错了,并告诉我它无法读取该文件,因此我怀疑它与我的文件有关已发送。

这是针对Form-Recognizer 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
microsoft-cognitive form-recognizer
1个回答
0
投票

看来您的请求主体正在尝试使用v2.0不再支持的multipart/form-data(尽管支持,但您需要在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参数传递。

希望这会有所帮助!

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