Skip to content

Conversation

@dengzhongyuan365-dev
Copy link
Contributor

feat: Enhance OCR plugin loading mechanism

  • Implemented logic to check for the availability of the OCR V5 plugin and load it if present.
  • Added fallback to load the default OCR plugin if the OCR V5 plugin fails to load.

This change improves the flexibility and robustness of the OCR engine's plugin management.

- Implemented logic to check for the availability of the OCR V5 plugin and load it if present.
- Added fallback to load the default OCR plugin if the OCR V5 plugin fails to load.

This change improves the flexibility and robustness of the OCR engine's plugin management.
@github-actions
Copy link

TAG Bot

TAG: 6.5.12
EXISTED: no
DISTRIBUTION: unstable

- update version to 6.5.12

 log: update version to 6.5.12
@lzwind lzwind merged commit 124945d into linuxdeepin:master Oct 30, 2025
14 of 17 checks passed
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

2 similar comments
@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot
Copy link

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: dengzhongyuan365-dev, lzwind

The full list of commands accepted by this bot can be found here.

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@deepin-ci-robot
Copy link

deepin pr auto review

我来对这个diff进行审查:

  1. 代码逻辑审查:
  • 新增的OCR V5插件加载逻辑是合理的,采用了优雅降级的策略:优先尝试加载V5插件,失败则回退到默认插件
  • 使用Qt::CaseInsensitive进行插件名称比较是合适的,提高了兼容性
  • 加载失败时有适当的日志记录,便于问题排查
  1. 代码质量建议:
  • 建议将硬编码的字符串"PPOCR_V5"提取为常量,便于维护
  • 可以考虑使用更描述性的变量名,如isPluginLoaded替代load
  • 注释中提到的产品设计缺陷应该被记录为TODO或创建相应的issue
  1. 性能优化建议:
  • 当前实现会先获取所有插件列表,建议可以直接使用loadPlugin的返回值来判断插件是否存在
  • setUseMaxThreadsCount(2)的值应该考虑是否需要配置化
  1. 安全性考虑:
  • 插件加载过程应该考虑添加插件签名验证
  • 建议记录加载插件的完整路径,便于安全审计

改进建议代码:

// 在类定义中添加常量
static const QString OCR_V5_PLUGIN = "PPOCR_V5";

// 构造函数中的改进实现
OCREngine::OCREngine()
{
    qCInfo(dmOcr) << "Initializing OCR driver";
    ocrDriver = new Dtk::Ocr::DOcr;
    
    bool isPluginLoaded = ocrDriver->loadPlugin(OCR_V5_PLUGIN);
    if (isPluginLoaded) {
        qCInfo(dmOcr) << "OCR V5 plugin loaded successfully";
    } else {
        qCWarning(dmOcr) << "Failed to load OCR V5 plugin, falling back to default";
        ocrDriver->loadDefaultPlugin();
        qCInfo(dmOcr) << "Default OCR plugin loaded";
    }

    // TODO: Make thread count configurable
    ocrDriver->setUseMaxThreadsCount(2);
    
    // TODO: Add GPU detection and configuration
    QFileInfo mtfi("/dev/mtgpu.0");
    if (mtfi.exists()) {
        // ...
    }
}
  1. 其他建议:
  • changelog中的版本号6.5.12应该与实际发布的版本保持一致
  • 建议在代码中添加更多的错误处理机制
  • 考虑添加插件版本检查,确保加载的插件版本与程序兼容

这些改进将使代码更加健壮、安全和易于维护。

@deepin-ci-robot
Copy link

deepin pr auto review

我来对这段代码进行审查:

  1. 语法逻辑分析:
  • 代码语法正确,没有明显的语法错误
  • 逻辑流程清晰:先尝试加载OCR V5插件,失败则回退到默认插件
  • 使用了Qt的字符串比较和容器操作,符合Qt编程规范
  1. 代码质量改进建议:
  • 变量命名可以更清晰:ocrV5可以改为ocrV5PluginNameload可以改为isV5PluginLoaded
  • 建议将插件名称定义为常量,避免魔法字符串
  • 可以使用RAII或智能指针管理ocrDriver的生命周期
  • 日志输出可以更详细,比如在加载失败时输出失败原因
  1. 性能优化建议:
  • installedPluginNames()可能涉及文件系统操作,建议缓存结果
  • 可以考虑异步加载插件,避免阻塞主线程
  1. 安全性改进:
  • 对插件名称的大小写比较可能存在安全隐患,建议统一转换为大写或小写后再比较
  • 建议添加插件完整性校验机制
  • 可以考虑添加插件版本检查

改进后的代码示例:

OCREngine::OCREngine()
    : ocrDriver(nullptr)
{
    // 定义常量
    static const QString OCR_V5_PLUGIN_NAME = "PPOCR_V5";
    static const QString MTGPU_DEVICE_PATH = "/dev/mtgpu.0";
    
    qCInfo(dmOcr) << "Initializing OCR driver";
    
    // 使用智能指针管理资源
    ocrDriver = std::make_unique<Dtk::Ocr::DOcr>();
    
    bool isV5PluginLoaded = false;
    
    // 获取已安装插件列表(考虑缓存)
    const auto plugins = ocrDriver->installedPluginNames();
    const QString normalizedPluginName = OCR_V5_PLUGIN_NAME.toLower();
    
    // 检查并尝试加载V5插件
    if (std::any_of(plugins.begin(), plugins.end(),
        [&normalizedPluginName](const QString& plugin) {
            return plugin.toLower() == normalizedPluginName;
        })) {
        
        if (ocrDriver->loadPlugin(OCR_V5_PLUGIN_NAME)) {
            isV5PluginLoaded = true;
            qCInfo(dmOcr) << "Successfully loaded OCR V5 plugin";
        } else {
            qCWarning(dmOcr) << "Failed to load OCR V5 plugin, falling back to default";
        }
    }
    
    // 如果V5插件加载失败,使用默认插件
    if (!isV5PluginLoaded) {
        ocrDriver->loadDefaultPlugin();
        qCInfo(dmOcr) << "Default OCR plugin loaded";
    }
    
    // 设置线程数
    ocrDriver->setUseMaxThreadsCount(2);
    
    // 检查MTGPU设备
    if (QFileInfo::exists(MTGPU_DEVICE_PATH)) {
        // 处理MTGPU相关逻辑
    }
}

这些改进提高了代码的可维护性、性能和安全性,同时保持了原有功能的完整性。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants