Create Custom Form Recognizer script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
param( | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string] $ResourceGroup = $null, | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string] $ResourceLocation = 'EastUS', | |
# F0 is free and S0 is paid SKU | |
[Parameter()] | |
[ValidateSet('F0','S0')] | |
[ValidateNotNullOrEmpty()] | |
[string] $FormRecognizerSku = 'F0', | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string] $FormRecognizerName = 'FormRecognizer', | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string] $StorageAccountName = 'formstorageaccountname', | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string] $StorageBlobName = 'forms', | |
[Parameter()] | |
[ValidateNotNullOrEmpty()] | |
[string] $StorageAccountSasExpiry = '2022-01-01T00:00Z', | |
[Parameter()] | |
[bool] $ClearCors = $true, | |
[Parameter()] | |
[ValidateSet('Docker', 'GitHub', 'Web', 'None', '')] | |
[ValidateNotNullOrEmpty()] | |
[string] $InstallationType = 'Web' | |
) | |
[Console]::ResetColor() | |
# Check if everything is installed and let user know what to install/configure. | |
$useToolFromDocker = $InstallationType -eq "Docker"; | |
$useToolFromGitHub = $InstallationType -eq "GitHub"; | |
$useWeb = -not $useToolFromDocker -and -not $useToolFromGitHub | |
Write-Host "Generate Azure resources required for Custom Form Recognizer" | |
Write-Host "" | |
Write-Host "Parameter:" | |
Write-Host "ResourceGroup : " -NoNewline | |
Write-Host $ResourceGroup -ForegroundColor DarkGray | |
Write-Host "ResourceLocation : " -NoNewline | |
Write-Host $ResourceLocation -ForegroundColor DarkGray | |
Write-Host "FormRecognizerSku : " -NoNewline | |
Write-Host $FormRecognizerSku -ForegroundColor DarkGray | |
Write-Host "FormRecognizerName : " -NoNewline | |
Write-Host $FormRecognizerName -ForegroundColor DarkGray | |
Write-Host "StorageAccountName : " -NoNewline | |
Write-Host $StorageAccountName -ForegroundColor DarkGray | |
Write-Host "StorageBlobName : " -NoNewline | |
Write-Host $StorageBlobName -ForegroundColor DarkGray | |
Write-Host "StorageAccountSasExpiry : " -NoNewline | |
Write-Host $StorageAccountSasExpiry -ForegroundColor DarkGray | |
Write-Host "ClearCors : " -NoNewline | |
Write-Host $ClearCors -ForegroundColor DarkGray | |
Write-Host "InstallationType : " -NoNewline | |
Write-Host $InstallationType -ForegroundColor DarkGray | |
Write-Host "UseDocker : " -NoNewline | |
Write-Host $useToolFromDocker -ForegroundColor DarkGray | |
Write-Host "UseGitHub : " -NoNewline | |
Write-Host $useToolFromGitHub -ForegroundColor DarkGray | |
Write-Host "UseWeb : " -NoNewline | |
Write-Host $useWeb -ForegroundColor DarkGray | |
Write-Host "" | |
Write-Host "" | |
If ($null -eq $ResourceGroup -or "" -eq $ResourceGroup) { | |
Write-Warning "Arguments not configured." | |
Write-Host "Example:" | |
Write-Host ".\create-custom-form-recognizer.ps1 -FormRecognizerName poc-forms -StorageAccountName pocformsprotostorage -ResourceGroup PoC -StorageAccountSasExpiry ""2021-01-01T00:00Z""" -ForegroundColor DarkGray | |
Write-Host "" | |
exit | |
} | |
Write-Host "Checking if Azure CLI and Docker is installed..." | |
$wingetExists = Get-Command winget -erroraction 'silentlycontinue' | |
$isReady = $true | |
$azCommandExists = Get-Command az -erroraction 'silentlycontinue' | |
If ($null -eq $azCommandExists -or $azCommandExists.Count -eq 0) { | |
Write-Host "" | |
Write-Host "Azure CLI needs to be installed and accessible to PowerShell" -ForegroundColor Red | |
Write-Host "https://docs.microsoft.com/en-us/cli/azure/install-azure-cli?view=azure-cli-latest" -ForegroundColor DarkGray | |
if ($null -ne $wingetExists -or $wingetExists.Count -eq 0) { | |
Write-Host "" | |
Write-Host "Or install it via winget:" | |
Write-Host "winget install Microsoft.AzureCLI" -ForegroundColor DarkGray | |
} | |
Write-Host "" | |
$isReady = $false | |
} | |
If ($useToolFromDocker) { | |
$dockerCommandExists = Get-Command docker -erroraction 'silentlycontinue' | |
If ($null -eq $dockerCommandExists -or $dockerCommandExists.Count -eq 0) { | |
Write-Host "" | |
Write-Host "Docker needs to be installed and accessible to PowerShell" -ForegroundColor Red | |
Write-Host "https://www.docker.com/" -ForegroundColor DarkGray | |
if ($null -ne $wingetExists -or $wingetExists.Count -eq 0) { | |
Write-Host "" | |
Write-Host "Or install it via winget:" | |
Write-Host "winget install Microsoft.DockerDesktop" -ForegroundColor DarkGray | |
} | |
Write-Host "" | |
$isReady = $false | |
} | |
} | |
If ($useToolFromGitHub) { | |
$gitCommandExists = Get-Command git -erroraction 'silentlycontinue' | |
If ($null -eq $gitCommandExists -or $gitCommandExists.Count -eq 0) { | |
Write-Host "" | |
Write-Host "Git for Windows needs to be installed and accessible to PowerShell" -ForegroundColor Red | |
Write-Host "https://git-scm.com/download/win" -ForegroundColor DarkGray | |
if ($null -ne $wingetExists -or $wingetExists.Count -eq 0) { | |
Write-Host "" | |
Write-Host "Or install it via winget:" | |
Write-Host "winget install git" -ForegroundColor DarkGray | |
} | |
Write-Host "" | |
$isReady = $false | |
} | |
} | |
Write-Host "Checking Azure CLI subscription..." | |
$currentAzAccountName = az account show --query name --output tsv | |
If ($null -eq $currentAzAccountName) { | |
Write-Host "" | |
Write-Host "Authenticate and set an Azure Subscription" -ForegroundColor Red | |
Write-Host "az login" -ForegroundColor DarkGray | |
Write-Host "Authentincate in the browser" | |
Write-Host "az account set -s <subscription-id>" -ForegroundColor DarkGray | |
Write-Host "" | |
$isReady = $false | |
} | |
If ($isReady -eq $false) { | |
Write-Error "One or more errors found." | |
[Console]::ResetColor() | |
exit | |
} | |
# Create or get blob storage account | |
$blobStorageExists = az resource list --name $StorageAccountName | |
if ($null -eq $blobStorageExists -or $blobStorageExists -eq "[]") { | |
Write-Host "Creating $StorageAccountName Azure Storage Account..." | |
$primaryBlobUrl = az storage account create --name $StorageAccountName --resource-group $ResourceGroup --access-tier Cool --kind StorageV2 --sku Standard_LRS --query "primaryEndpoints.blob" --output tsv | |
} else { | |
Write-Host "Get $StorageAccountName Azure Storage Account..." | |
$primaryBlobUrl = az storage account show --name $StorageAccountName --resource-group $ResourceGroup --query "primaryEndpoints.blob" --output tsv | |
} | |
if ($null -eq $primaryBlobUrl) { | |
Write-Error "Unable to create Storage Account with Blob Storage." | |
[Console]::ResetColor() | |
exit | |
} | |
$connectionString = az storage account show-connection-string --name $StorageAccountName --key primary --query connectionString | |
Write-Host "Creating $StorageBlobName Blob Container..." | |
az storage container create --name $StorageBlobName --connection-string $connectionString$connectionString > nil | |
Write-Host "Configuring $StorageAccountName Azure Storage Account and $StorageBlobName Blob Container for custom Form Recognizer..." | |
# Configure blob container and get SAS key | |
If ($ClearCors -eq $true) { | |
Write-Host "Clearing $StorageAccountName CORS..." | |
az storage cors clear --services b --connection-string $connectionString | |
} | |
az storage cors add --methods DELETE, GET, HEAD, MERGE, OPTIONS, POST, PUT --origins * --services b --allowed-headers * --max-age 200 --exposed-headers * --connection-string $connectionString > nil | |
$sas = az storage container generate-sas --account-name $StorageAccountName --name $StorageBlobName --permissions dlrw --start 2020-01-01T00:00Z --expiry $StorageAccountSasExpiry --connection-string $connectionString --output tsv | |
# Create or update Form Recognizer and | |
$formsExists = az resource list --name $FormRecognizerName | |
if ($null -eq $formsExists -or $formsExists -eq "[]") { | |
Write-Host "Creating $FormRecognizerName Form Recognizer with $FormRecognizerSku SKU..." | |
$endpoint = az cognitiveservices account create --kind FormRecognizer --location $ResourceLocation --name $FormRecognizerName --resource-group $ResourceGroup --sku $FormRecognizerSku --yes --query endpoint --output tsv | |
} else { | |
Write-Host "Updating $FormRecognizerName Form Recognizer with $FormRecognizerSku SKU..." | |
$endpoint = az cognitiveservices account show --name $FormRecognizerName --resource-group $ResourceGroup --query endpoint --output tsv | |
$_ = az cognitiveservices account update --name $FormRecognizerName --resource-group $ResourceGroup --sku $FormRecognizerSku | |
} | |
$key1 = az cognitiveservices account keys list --name $FormRecognizerName --resource-group $ResourceGroup --query key1 --output tsv | |
Write-Host "Downloading Docker image tool for custom labeling..." | |
Write-Host "" | |
# Download necessary Docker image for custom form labeling. | |
If ($useToolFromDocker) { | |
docker pull mcr.microsoft.com/azure-cognitive-services/custom-form/labeltool | |
} ElseIf ($useToolFromGitHub) { | |
git clone https://github.com/Microsoft/OCR-Form-Tools.git | |
cd OCR-Form-Tools | |
npm install | |
npm run build | |
cd .. | |
} | |
[Console]::ResetColor() | |
Write-Host "" | |
Write-Host "" | |
Write-Host "Next steps:" | |
Write-Host "1. " -NoNewline -ForegroundColor DarkCyan | |
Write-Host "Download and install Azure Storage Explorer to upload your forms to Azure Blob Storage." | |
Write-Host "https://azure.microsoft.com/en-au/features/storage-explorer/" -ForegroundColor DarkGray | |
if ($null -ne $wingetExists -or $wingetExists.Count -eq 0) { | |
Write-Host "" | |
Write-Host "Or install it via winget:" | |
Write-Host "winget install Microsoft.AzureStorageExplorer" -ForegroundColor DarkGray | |
} | |
Write-Host "" | |
Write-Host "2. " -NoNewline -ForegroundColor DarkCyan | |
Write-Host "Upload images of forms to Blob Container" | |
Write-Host "$currentAzAccountName\Storage Accounts\$StorageAccountName\$StorageBlobName" -ForegroundColor DarkGray | |
Write-Host "" | |
$stepNumber = 3; | |
$toolUrl = ''; | |
If ($useToolFromDocker) { | |
Write-Host "3. " -NoNewline -ForegroundColor DarkCyan | |
Write-Host "Run following command to start custom form labeling tool" | |
Write-Host "docker run -it -p 30000:80 mcr.microsoft.com/azure-cognitive-services/custom-form/labeltool eula=accept" -ForegroundColor DarkGray | |
Write-Host "" | |
$toolUrl = 'http://localhost:30000'; | |
$stepNumber += 1; | |
} ElseIf ($useToolFromGitHub) { | |
Write-Host "3. " -NoNewline -ForegroundColor DarkCyan | |
Write-Host "In separate terminal, run following command to start custom form labeling tool" | |
Write-Host "npm run react-start" -ForegroundColor DarkGray | |
Write-Host "" | |
$toolUrl = 'http://localhost:3000'; | |
$stepNumber += 1; | |
} Else { | |
$toolUrl = 'https://fott.azurewebsites.net/'; | |
} | |
Write-Host "${stepNumber}. " -NoNewline -ForegroundColor DarkCyan | |
Write-Host "In browser open" | |
Write-Host $toolUrl -ForegroundColor DarkGray | |
Write-Host "" | |
$stepNumber += 1; | |
Write-Host "${stepNumber}. " -NoNewline -ForegroundColor DarkCyan | |
Write-Host "Create new project and use following details." | |
Write-Host "Blob Storage SAS URI : " -NoNewline | |
Write-Host $primaryBlobUrl -NoNewline -ForegroundColor DarkGray | |
Write-Host $StorageBlobName -NoNewline -ForegroundColor DarkGray | |
Write-Host "?$sas" -ForegroundColor DarkGray | |
Write-Host "Folder path : " -NoNewline | |
Write-Host "/" -ForegroundColor DarkGray | |
Write-Host "Form Recognizer service URI : " -NoNewline | |
Write-Host $endpoint -ForegroundColor DarkGray | |
Write-Host "Form Recognizer API key : " -NoNewline | |
Write-Host $key1 -ForegroundColor DarkGray | |
Write-Host "" | |
Write-Host "" | |
Write-Host "Original instructions at " -NoNewline | |
Write-Host "https://jkdev.me/getting-started-with-form-recognizer/" -ForegroundColor DarkGray | |
Write-Host "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is FANTASTIC! Thank you for this @jernejk