首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法提交要分析的表单

无法提交要分析的表单
EN

Stack Overflow用户
提问于 2019-12-21 01:41:00
回答 1查看 115关注 0票数 0

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

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

代码语言:javascript
复制
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) 

编辑:

对于像我这样好奇的人来说,这里有一个解决方案:

代码语言:javascript
复制
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
EN

回答 1

Stack Overflow用户

发布于 2019-12-21 04:32:25

看起来您的请求体正在尝试使用v2.0中不再支持的multipart/form-data (尽管如果是这样,您将需要在您的Content-Type头中使用该值)。

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

代码语言:javascript
复制
# 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参数将文件加载到内存中并传递。

希望这能有所帮助!

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/59429361

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档