Skip to content

fix: return empty instead of failure for textless images in OCR extractor - #304

Closed
Johnson-zs wants to merge 1 commit into
linuxdeepin:develop/snipefrom
Johnson-zs:agent/bugfix/384853
Closed

Johnson-zs wants to merge 1 commit into
linuxdeepin:develop/snipefrom
Johnson-zs:agent/bugfix/384853

Conversation

@Johnson-zs

@Johnson-zs Johnson-zs commented Nov 30, 2026 •

Copy link
Copy Markdown

Root Cause Analysis

OcrExtractor::extract() in ocrextractor.cpp:334 returned std::nullopt (failure) when the OCR engine successfully analyzed an image but the cleaned text had fewer than 2 characters. This treated "image has no text" (e.g., photos, wallpapers, solid-color images) as "extraction failed". The downstream caller ExtractorApp::processBatch() (extractorapp.cpp:108) checks result.has_value() — std::nullopt triggers sendFailed(), marking the image as failed. With 62–88% of images being textless, the overall index update failure rate exceeded the threshold, causing the UI to show "索引更新失败".

Key evidence: ocrextractor.cpp:332-334 returns std::nullopt for textless images; extractorapp.cpp:108-113 sends sendFailed on std::nullopt; repro demo confirmed Case 2 (textless) and Case 3 (engine failure) produce identical sendFailed results.

Fix

Changed return std::nullopt; to return QByteArray(); at ocrextractor.cpp:334, and updated the debug log message from "cleaned OCR result has less than 2 characters" to "image has no extractable text" to reflect the corrected semantics. QByteArray() is a successful extraction with empty content — has_value() returns true, so the caller walks the sendData success path, sending empty text. All downstream handlers (processextractor.cpp, taskhandler.cpp) already handle empty text via the success path.

Change Safety Assessment

Code Safety

  • Risk Level: Low
  • Target line was introduced by commit 8d481bc (initial migration), not a historical bug fix — this change does not revert any existing fix
  • Single caller ExtractorApp::processBatch() already handles empty QByteArray via sendData success path; no signature or API change

Business Impact Scope

  • Image text content search indexing: textless images (photos, wallpapers, solid colors) no longer interrupt index updates
  • Affected modules: OCR extractor (ocrextractor.cpp), extraction app (extractorapp.cpp), process extractor (processextractor.cpp)
  • User scenario: after system upgrade, first-time enabling "image text content search" should complete index update successfully even when the folder contains textless images

Verification Suggestion

  • Test image text content search with folders containing textless images (wallpapers, photos) — index update should complete successfully
  • Regression: verify folders with text-containing images still index correctly
  • Mixed folders (text + textless images) should index completely without failures

根因分析

OcrExtractor::extract()(ocrextractor.cpp:334)在 OCR 引擎成功分析图片但清洗后文本不足 2 字符时返回 std::nullopt(失败),将"图片无文字"(如照片、壁纸、纯色图片)误判为"提取失败"。下游调用者 ExtractorApp::processBatch()(extractorapp.cpp:108)检查 result.has_value(),std::nullopt 触发 sendFailed(),将该图片标记为提取失败。由于 62–88% 的图片为无文字图片,整体索引更新失败率超过阈值,导致 UI 显示"索引更新失败"。

关键证据:ocrextractor.cpp:332-334 对无文字图片返回 std::nullopt;extractorapp.cpp:108-113 收到 std::nullopt 后发送 sendFailed;复现验证器确认 Case 2(无文字)与 Case 3(引擎故障)产生相同的 sendFailed 结果。

修复方案

将 ocrextractor.cpp:334 的 return std::nullopt; 改为 return QByteArray();,同时更新调试日志从"cleaned OCR result has less than 2 characters"改为"image has no extractable text"以反映修正后的语义。QByteArray() 表示成功的空内容提取——has_value() 返回 true,调用方走 sendData 成功路径,发送空文本。所有下游处理者(processextractor.cpp、taskhandler.cpp)已通过成功路径处理空文本。

改动安全评估

代码安全评估

  • 风险等级: 低风险
  • 目标行由 commit 8d481bc(初始迁移)引入,非历史 bug 修复产物——本次改动不撤销任何已有修复
  • 唯一调用者 ExtractorApp::processBatch() 已通过 sendData 成功路径兼容空 QByteArray,无签名或 API 变更

业务影响范围

  • 图片文本内容搜索索引:无文字图片(照片、壁纸、纯色图)不再中断索引更新
  • 涉及模块:OCR 提取器(ocrextractor.cpp)、提取应用(extractorapp.cpp)、进程提取器(processextractor.cpp)
  • 用户场景:系统升级后首次勾选"图片文本内容搜索",即使文件夹包含无文字图片,索引更新也应成功完成

验证建议

  • 测试包含无文字图片(壁纸、照片)的文件夹的图片文本内容搜索——索引更新应成功完成
  • 回归:验证含文字图片的文件夹仍能正确索引
  • 混合文件夹(含文字+无文字图片)应完整索引,无失败记录

Summary by Sourcery

Bug Fixes:

  • Treat images with no extractable OCR text as successful empty results instead of extraction failures, preventing textless images from causing index updates to fail.

…ctor

1. Root cause: OcrExtractor::extract() returned std::nullopt (failure)
   when the OCR engine succeeded but the cleaned text had fewer than
   2 characters, treating "image has no text" as "extraction failed"
2. Fix: return QByteArray() (empty = success, no text) so the caller
   processes it as a successful extraction with empty content
3. Impact: textless images no longer interrupt index updates; callers
   already handle empty QByteArray via the success path

fix: 修复OCR提取器对无文字图片返回失败导致索引更新中断

1. 根因:OcrExtractor::extract() 在OCR引擎成功识别但清洗后文本
   不足2字符时返回std::nullopt(失败),将"图片无文字"误判为
   "提取失败",导致索引更新中断
2. 方案:改为返回QByteArray()(空=成功无文本),使调用方走成功
   路径处理空内容
3. 影响:无文字图片不再中断索引更新,调用方已兼容空QByteArray

Log: Fixed OCR extractor returning failure for textless images, causing index update interruption
Influence:
  - Test image text content search with images containing text
  - Test image text content search with textless images (wallpapers, solid colors)
  - Verify mixed folders with both text and textless images index completely
Log: 修复OCR提取器对无文字图片误判为失败导致索引更新中断的问题
Influence:
  - 测试含文字图片的文本内容搜索索引
  - 测试无文字图片(壁纸、纯色图)的文本内容搜索索引
  - 验证混合文件夹(含文字+无文字图片)的索引完整性
PMS: BUG-384853
@Johnson-zs
Johnson-zs marked this pull request as draft November 30, 2026 12:38
@sourcery-ai

sourcery-ai Bot commented Nov 30, 2026

Copy link
Copy Markdown

Reviewer's Guide

Implementation

Treat OCR results with fewer than two cleaned characters as successful empty extraction rather than failure.

  • src/extractor/plugins/ocr-extractor/ocrextractor.cpp L333-334

Tips and commands

Interacting with Sourcery

  • Trigger a new review: Comment @sourcery-ai review on the pull request.
  • Continue discussions: Reply directly to Sourcery's review comments.
  • Generate a GitHub issue from a review comment: Ask Sourcery to create an
    issue from a review comment by replying to it. You can also reply to a
    review comment with @sourcery-ai issue to create an issue from it.
  • Generate a pull request title: Write @sourcery-ai anywhere in the pull
    request title to generate a title at any time. You can also comment
    @sourcery-ai title on the pull request to (re-)generate the title at any time.
  • Generate a pull request summary: Write @sourcery-ai summary anywhere in
    the pull request body to generate a PR summary at any time exactly where you
    want it. You can also comment @sourcery-ai summary on the pull request to
    (re-)generate the summary at any time.
  • Generate reviewer's guide: Comment @sourcery-ai guide on the pull
    request to (re-)generate the reviewer's guide at any time.
  • Resolve all Sourcery comments: Comment @sourcery-ai resolve on the
    pull request to resolve all Sourcery comments. Useful if you've already
    addressed all the comments and don't want to see them anymore.
  • Dismiss all Sourcery reviews: Comment @sourcery-ai dismiss on the pull
    request to dismiss all existing Sourcery reviews. Especially useful if you
    want to start fresh with a new review - don't forget to comment
    @sourcery-ai review to trigger a new review!

Customizing Your Experience

Access your dashboard to:

  • Enable or disable review features such as the Sourcery-generated pull request
    summary, the reviewer's guide, and others.
  • Change the review language.
  • Add, remove or edit custom review instructions.
  • Adjust other review settings.

Getting Help

@deepin-ci-robot

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is NOT APPROVED

This pull-request has been approved by: Johnson-zs

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

@sourcery-ai sourcery-ai Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hey - I've found 1 issue

Prompt for AI Agents
Please address the comments from this code review:

## Individual Comments

### Comment 1
<location path="src/extractor/plugins/ocr-extractor/ocrextractor.cpp" line_range="334" />
<code_context>
-        qDebug() << "OcrExtractor::extract: cleaned OCR result has less than 2 characters for:" << filePath;
-        return std::nullopt;
+        qDebug() << "OcrExtractor::extract: image has no extractable text for:" << filePath;
+        return QByteArray();
     }

</code_context>
<issue_to_address>
**Single-character OCR text disappears**

When OCR recognizes a meaningful single character and cleaning preserves it, `OcrExtractor::extract()` rejects the one-character result at the `< 2` guard and returns empty content, so `OcrDocumentBuilder` omits the OCR field and the character is not searchable.

Reject only empty cleaned results, allowing valid one-character OCR text to reach the index.
</issue_to_address>

Sourcery is free for open source - if you like our reviews please consider sharing them ✨

qDebug() << "OcrExtractor::extract: cleaned OCR result has less than 2 characters for:" << filePath;
return std::nullopt;
qDebug() << "OcrExtractor::extract: image has no extractable text for:" << filePath;
return QByteArray();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Medium · Single-character OCR text disappears

When OCR recognizes a meaningful single character and cleaning preserves it, OcrExtractor::extract() rejects the one-character result at the < 2 guard and returns empty content, so OcrDocumentBuilder omits the OCR field and the character is not searchable.

Reject only empty cleaned results, allowing valid one-character OCR text to reach the index.

Prompt for AI agents
In `src/extractor/plugins/ocr-extractor/ocrextractor.cpp` at line 334:

**Single-character OCR text disappears**

When OCR recognizes a meaningful single character and cleaning preserves it, `OcrExtractor::extract()` rejects the one-character result at the `< 2` guard and returns empty content, so `OcrDocumentBuilder` omits the OCR field and the character is not searchable.

Reject only empty cleaned results, allowing valid one-character OCR text to reach the index.

@Johnson-zs

Copy link
Copy Markdown
Author

基于错误分析创建,按纠偏 corr-01a22b17-1 返工关闭

@Johnson-zs Johnson-zs closed this Dec 1, 2026
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.

2 participants