CtrEditor/paddleocr/download-paddleocr-models.ps1

133 lines
4.7 KiB
PowerShell

# Script para descargar e instalar modelos PaddleOCR
# Guardar como: download-paddleocr-models.ps1
param(
[string]$OutputDirectory = ".\paddleocr",
[switch]$ForceDownload = $false
)
# URLs de modelos PaddleOCR (inglés por defecto)
$modelUrls = @{
"det" = "https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_det_slim_infer.tar"
"rec" = "https://paddleocr.bj.bcebos.com/PP-OCRv3/english/en_PP-OCRv3_rec_slim_infer.tar"
"cls" = "https://paddleocr.bj.bcebos.com/dygraph_v2.0/ch/ch_ppocr_mobile_v2.0_cls_slim_infer.tar"
"keys" = "https://raw.githubusercontent.com/PaddlePaddle/PaddleOCR/release/2.6/ppocr/utils/en_dict.txt"
}
# Crear directorios si no existen
function EnsureDirectory($path) {
if (-not (Test-Path $path)) {
New-Item -Path $path -ItemType Directory | Out-Null
Write-Host "Creado directorio: $path" -ForegroundColor Green
}
}
# Descargar archivo con barra de progreso
function DownloadFile($url, $outputFile) {
$webClient = New-Object System.Net.WebClient
$webClient.Encoding = [System.Text.Encoding]::UTF8
$downloadExists = Test-Path $outputFile
if ($downloadExists -and -not $ForceDownload) {
Write-Host "El archivo ya existe: $outputFile. Use -ForceDownload para sobrescribir." -ForegroundColor Yellow
return $false
}
Write-Host "Descargando $url..." -ForegroundColor Cyan
try {
$webClient.DownloadFile($url, $outputFile)
Write-Host "Descarga completada: $outputFile" -ForegroundColor Green
return $true
} catch {
Write-Host "Error al descargar $url : $_" -ForegroundColor Red
return $false
}
}
# Extraer archivo TAR
function ExtractTarFile($tarFile, $destDir) {
Write-Host "Extrayendo $tarFile..." -ForegroundColor Cyan
# Verificar si tar está disponible
$tarAvailable = $null -ne (Get-Command "tar" -ErrorAction SilentlyContinue)
if ($tarAvailable) {
# Usar tar nativo
try {
tar -xf $tarFile -C $destDir
Write-Host "Extracción completada: $tarFile" -ForegroundColor Green
return $true
} catch {
Write-Host "Error al extraer con tar: $_" -ForegroundColor Red
}
}
# Fallback a .NET
try {
Add-Type -AssemblyName System.IO.Compression.FileSystem
# Descomprimir TAR con .NET
# Nota: Esto es un ejemplo simplificado. Necesitarías una biblioteca específica para TAR
Write-Host "TAR nativo no disponible. Se requiere una biblioteca para manejar archivos TAR." -ForegroundColor Yellow
# Aquí puedes agregar código para usar SharpCompress u otra biblioteca para TAR
# Por ejemplo:
# [Reflection.Assembly]::LoadFrom("path\to\SharpCompress.dll")
# $reader = [SharpCompress.Readers.ReaderFactory]::Open($tarFile)
# ...
return $false
} catch {
Write-Host "Error al extraer $tarFile : $_" -ForegroundColor Red
return $false
}
}
# Crear directorio principal
EnsureDirectory $OutputDirectory
# Crear estructura de directorios
$modelTypes = @("det", "rec", "cls", "keys")
foreach ($type in $modelTypes) {
$typeDir = Join-Path $OutputDirectory $type
EnsureDirectory $typeDir
if ($type -ne "keys") {
$inferenceDir = Join-Path $typeDir "inference"
EnsureDirectory $inferenceDir
}
}
# Descargar y procesar cada modelo
foreach ($entry in $modelUrls.GetEnumerator()) {
$type = $entry.Key
$url = $entry.Value
$typeDir = Join-Path $OutputDirectory $type
if ($type -eq "keys") {
# Para el archivo de claves, solo descargarlo directamente
$outputFile = Join-Path $typeDir "ppocr_keys.txt"
DownloadFile $url $outputFile
} else {
# Para los modelos, descargar TAR y extraerlo
$tarFile = Join-Path $env:TEMP "$type.tar"
$downloaded = DownloadFile $url $tarFile
if ($downloaded) {
$extracted = ExtractTarFile $tarFile $typeDir
# Limpiar archivo temporal
if (Test-Path $tarFile) {
Remove-Item $tarFile -Force
}
}
}
}
Write-Host "`nInstalación de modelos PaddleOCR completada en: $OutputDirectory" -ForegroundColor Green
Write-Host "Para usar estos modelos, configura PaddleOCREngine con las siguientes rutas:" -ForegroundColor Yellow
Write-Host " - det_infer: $OutputDirectory\det\inference" -ForegroundColor White
Write-Host " - rec_infer: $OutputDirectory\rec\inference" -ForegroundColor White
Write-Host " - cls_infer: $OutputDirectory\cls\inference" -ForegroundColor White
Write-Host " - keys: $OutputDirectory\keys\ppocr_keys.txt" -ForegroundColor White