Skip to content
DemoFAQ

设置 DocumentAI 模型

概述

在使用 OCR、版面分析、表格识别、PDF 转可搜索 PDF 或 PDF 转 OFD 之前,SDK 需要加载 DocumentAI 模型。从 PHP SDK v4.1.0 开始,模型生命周期通过 LibraryManager 上的三个静态方法暴露:

方法用途
setDocumentAIModel(string $modelPath, int $gpuId = -1): int加载模型。$gpuId-1 禁用 GPU 加速,传非负 GPU 设备索引则启用。
setDocumentAIModelCount(int $layoutModelCount, int $tableModelCount): void配置 SDK 在内存中保持多少个并发的版面/表格模型实例。增加计数可在多任务并行时提升吞吐量。
releaseDocumentAIModel(): void释放模型(及其 GPU 资源)而不关闭 SDK 的其余部分。调用后 AI 功能将不可用,直到再次调用 setDocumentAIModel()

注意事项

  • setDocumentAIModel() 必须在 LibraryManager::initialize() 之后、任何依赖 AI 功能的转换之前调用。
  • 该方法返回 int 错误码;使用 ErrorCode::isSuccess() / ErrorCode::describe() 进行判读。

示例

php
use ComPDFKit\Conversion\ErrorCode;
use ComPDFKit\Conversion\LibraryManager;

LibraryManager::initialize(__DIR__ . '/../');

$rc = LibraryManager::setDocumentAIModel(__DIR__ . '/../resource/models/documentai.model', -1);
if (!ErrorCode::isSuccess($rc)) {
    throw new RuntimeException('setDocumentAIModel failed: ' . ErrorCode::describe($rc));
}

// 在内存中保留一个版面模型和一个表格模型。
LibraryManager::setDocumentAIModelCount(1, 1);

如果您希望使用自己的模型或第三方服务来运行 OCR、版面分析或表格识别,而不是内置的 DocumentAI 模型,请参见通过回调使用自定义 AI 模型