Skip to content

fix: return three values when a gif cannot be decoded - #18346

Open
linhongyu510 wants to merge 1 commit into
PaddlePaddle:mainfrom
linhongyu510:fix/corrupted-gif-return-arity
Open

linhongyu510 wants to merge 1 commit into
PaddlePaddle:mainfrom
linhongyu510:fix/corrupted-gif-return-arity

Conversation

@linhongyu510

Copy link
Copy Markdown

🔗 Related Issue

None — found while reading ppocr/utils/utility.py.

📖 Description

check_and_read returns a 3-tuple from every branch except one. When cv2.VideoCapture cannot decode a gif, it returns (None, False):

https://github.com/PaddlePaddle/PaddleOCR/blob/main/ppocr/utils/utility.py#L123-L126

Every caller unpacks three values. From tools/infer/predict_system.py:212:

img, flag_gif, flag_pdf = check_and_read(image_file)

predict_rec.py:892, predict_cls.py:146, predict_e2e.py:161 and predict_sr.py:136 do the same with img, flag, _ =. So a corrupted gif raises in the caller instead of being skipped:

ValueError: not enough values to unpack (expected 3, got 2)

That defeats the branch's own intent — it logs a message and returns a sentinel precisely so the caller can move to the next file.

There is a second problem in the same three lines. The log call passes an unformatted {}:

logger.info("Cannot read {}. This gif image maybe corrupted.")

so the output drops the path that was the only thing it existed to report:

[    INFO] utility.py:125 - Cannot read {}. This gif image maybe corrupted.

Reproduction

from ppocr.utils.utility import check_and_read

with open("broken.gif", "wb") as f:      # .gif name, undecodable contents
    f.write(b"GIF89a" + b"\x00\xff garbage")

img, flag_gif, flag_pdf = check_and_read("broken.gif")

Before this change, on paddlepaddle 3.3.1 / Python 3.12:

[    INFO] utility.py:125 - Cannot read {}. This gif image maybe corrupted.
ValueError: not enough values to unpack (expected 3, got 2)

After:

[    INFO] utility.py:125 - Cannot read /tmp/.../broken.gif. This gif image maybe corrupted.
img=None flag_gif=False flag_pdf=False

(None, False, False) matches what the function already returns for a path it does not handle (utility.py:152), so callers treat the corrupted gif the same way they treat any non-gif, non-pdf input.

🧪 Tests

Added tests/ppocr/test_check_and_read.py: one test per bug, plus a check that non-gif and non-pdf paths keep their current return value. That third test does not depend on the fix and stays green on either side of it.

The first two are load-bearing — restoring the old two-value return and the unformatted log message turns exactly those red:

FAILED tests/ppocr/test_check_and_read.py::test_corrupted_gif_returns_three_values
FAILED tests/ppocr/test_check_and_read.py::test_corrupted_gif_logs_the_path
2 failed, 4 passed

With the fix in place:

tests/ppocr/test_check_and_read.py                     6 passed
tests/ (excluding tests/ppocr/test_iaa_augment.py)     126 passed, 89 errors
black --check                                          2 files unchanged

The 89 errors are pre-existing on this machine and unrelated: they are all in tests/pipelines/, which downloads model weights this box cannot reach, and test_iaa_augment.py needs albumentations. I confirmed via git stash that tests/pipelines/test_table_recognition_v2.py produces the identical errors on a clean checkout.

✅ Checklist

  • Ran black on changed files
  • Added tests that fail without the change
  • No functional change to any other branch of the function

AI assistance was used for this change; I reviewed every changed line and ran the commands above locally.

`check_and_read` returns a 3-tuple from every branch except the one that
handles an unreadable gif, which returned `(None, False)`. Every caller
unpacks three values, e.g. `tools/infer/predict_system.py:212`:

    img, flag_gif, flag_pdf = check_and_read(image_file)

so a corrupted gif raised `ValueError: not enough values to unpack
(expected 3, got 2)` in the caller rather than being skipped -- despite this
branch already logging and returning a sentinel so that the caller could move
on. The same 2-value return is reached from predict_rec, predict_cls,
predict_e2e and predict_sr.

The log line in that branch also passed an unformatted `{}` to `logger.info`,
so the output read literally `Cannot read {}. This gif image maybe corrupted.`
and dropped the path, which was the only thing it existed to report.

Added tests covering both, plus a check that non-gif paths keep their current
return value. Reverting this change turns the first two red.

Co-authored-by: Claude <noreply@anthropic.com>
@CLAassistant

CLAassistant commented Sep 5, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants