Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JPEG2000 encoding/decoding fails when running in multiprocessing pool #22974

Open
4 tasks done
ndido98 opened this issue Dec 16, 2022 · 2 comments
Open
4 tasks done

JPEG2000 encoding/decoding fails when running in multiprocessing pool #22974

ndido98 opened this issue Dec 16, 2022 · 2 comments

Comments

@ndido98
Copy link

ndido98 commented Dec 16, 2022

System Information

Python version: 3.10.6
OpenCV Python version: 4.6.0.66
Operating System / Platform: Windows 11

Detailed description

When rapidly encoding and decoding images using the cv.imencode(".jp2") Python method inside a multiprocessing pool, the resulting image is somehow corrupted, as calls to cv.imdecode(buffer) log several errors in a non-deterministic way.
The kind of error depends on the images that are used.

For instance, when using the CIFAR-10 dataset, this is the error that is reported:

[ERROR:0@0.076] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\grfmt_jpeg2000_openjpeg.cpp (299) cv::`anonymous-namespace'::errorLogCallback OpenJPEG2000: Expected a SOC marker 

Other images from different sources produce other errors. For instance, another image produces this error log:

[ERROR:0@0.860] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\grfmt_jpeg2000_openjpeg.cpp (299) cv::`anonymous-namespace'::errorLogCallback OpenJPEG2000: Tile part length size inconsistent with stream length

[ERROR:0@0.864] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\grfmt_jpeg2000_openjpeg.cpp (299) cv::`anonymous-namespace'::errorLogCallback OpenJPEG2000: Failed to decode the codestream in the JP2 file

imdecode_(''): can't read data: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\grfmt_jpeg2000_openjpeg.cpp:592: error: (-2:Unspecified error) OpenJPEG2000: Decoding is failed in function 'cv::detail::Jpeg2KOpjDecoderBase::readData'

[ERROR:0@5.532] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\grfmt_jpeg2000_openjpeg.cpp (299) cv::`anonymous-namespace'::errorLogCallback OpenJPEG2000: Stream too short, expected SOT

[ERROR:0@5.537] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\grfmt_jpeg2000_openjpeg.cpp (299) cv::`anonymous-namespace'::errorLogCallback OpenJPEG2000: Failed to decode tile 1/1

[ERROR:0@5.541] global D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\grfmt_jpeg2000_openjpeg.cpp (299) cv::`anonymous-namespace'::errorLogCallback OpenJPEG2000: Failed to decode the codestream in the JP2 file

imdecode_(''): can't read data: OpenCV(4.6.0) D:\a\opencv-python\opencv-python\opencv\modules\imgcodecs\src\grfmt_jpeg2000_openjpeg.cpp:592: error: (-2:Unspecified error) OpenJPEG2000: Decoding is failed in function 'cv::detail::Jpeg2KOpjDecoderBase::readData'

The image which produces this comes from the FRGC Morphs dataset (in particular FRGC-Morphs\morph_facemorpher\04482d96.jpg_04493d86.jpg.jpg).

Also, this issue isn't present (or at least no errors are printed) when not calling cv.imdecode(buffer) or not using the multiprocessing pool.

Steps to reproduce

from pathlib import Path
import multiprocessing as mp
import cv2 as cv


USE_MP = True
USE_DECODE = True


def process_image(path):
    image = cv.imread(str(path))
    for q in range(1000):
        try:
            success, encoded = cv.imencode(".jp2", image, [cv.IMWRITE_JPEG2000_COMPRESSION_X1000, q])
        except cv.error as e:
            raise RuntimeError(f"Failed to encode {path} with quality {q}") from e
        if not success or encoded is None:
            raise RuntimeError(f"Failed to encode {path} with quality {q}")
        if USE_DECODE:
            try:
                decoded = cv.imdecode(encoded, cv.IMREAD_UNCHANGED)
            except cv.error as e:
                raise RuntimeError(f"Failed to decode {path} with quality {q}") from e
            if decoded is None:
                raise RuntimeError(f"Failed to decode {path} with quality {q}")


if __name__ == "__main__":
    # all_files = list(Path("CIFAR-10-images-master").rglob("*.jpg"))
    all_files = [Path("FRGC-Morphs/morph_facemorpher/04482d96.jpg_04493d86.jpg.jpg")] * 100
    print(f"Processing {len(all_files)} files...")
    if USE_MP:
        with mp.Pool(processes=None) as pool:
            pool.map(process_image, all_files)
    else:
        for path in all_files:
            process_image(path)

Run this piece of code (you may need to change the all_files variable accordingly; the important thing is that more than one worker must be busy, preferably all); the console should report the mentioned OpenCV errors.

Issue submission checklist

  • I report the issue, it's not a question
  • I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
  • I updated to the latest OpenCV version and the issue is still there
  • There is reproducer code and related data files (videos, images, onnx, etc)
@asmorkalov
Copy link
Contributor

Could you attach cv.getBuildInformation() call output? OpenCV can use Jasper or OpenJPEG for JPEG 2k.

@ndido98
Copy link
Author

ndido98 commented Dec 16, 2022

General configuration for OpenCV 4.6.0 =====================================
  Version control:               4.6.0

  Platform:
    Timestamp:                   2022-06-07T10:17:42Z
    Host:                        Windows 10.0.17763 AMD64
    CMake:                       3.22.5
    CMake generator:             Visual Studio 14 2015
    CMake build tool:            MSBuild.exe
    MSVC:                        1900
    Configuration:               Debug Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3
      requested:                 SSE3
    Dispatched code generation:  SSE4_1 SSE4_2 FP16 AVX AVX2
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_1 (16 files):         + SSSE3 SSE4_1
      SSE4_2 (1 files):          + SSSE3 SSE4_1 POPCNT SSE4_2
      FP16 (0 files):            + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 AVX
      AVX (4 files):             + SSSE3 SSE4_1 POPCNT SSE4_2 AVX
      AVX2 (31 files):           + SSSE3 SSE4_1 POPCNT SSE4_2 FP16 FMA3 AVX AVX2

  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe  (ver 19.0.24245.0)
    C++ flags (Release):         /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MT /O2 /Ob2 /DNDEBUG
    C++ flags (Debug):           /DWIN32 /D_WINDOWS /W4 /GR  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /EHa /wd4127 /wd4251 /wd4324 /wd4275 /wd4512 /wd4589 /MP  /MTd /Zi /Ob0 /Od /RTC1
    C Compiler:                  C:/Program Files (x86)/Microsoft Visual Studio 14.0/VC/bin/x86_amd64/cl.exe
    C flags (Release):           /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP   /MT /O2 /Ob2 /DNDEBUG
    C flags (Debug):             /DWIN32 /D_WINDOWS /W3  /D _CRT_SECURE_NO_DEPRECATE /D _CRT_NONSTDC_NO_DEPRECATE /D _SCL_SECURE_NO_WARNINGS /Gy /bigobj /Oi  /fp:precise     /MP /MTd /Zi /Ob0 /Od /RTC1
    Linker flags (Release):      /machine:x64  /NODEFAULTLIB:atlthunk.lib /INCREMENTAL:NO  /NODEFAULTLIB:libcmtd.lib /NODEFAULTLIB:libcpmtd.lib /NODEFAULTLIB:msvcrtd.lib
    Linker flags (Debug):        /machine:x64  /NODEFAULTLIB:atlthunk.lib /debug /INCREMENTAL  /NODEFAULTLIB:libcmt.lib /NODEFAULTLIB:libcpmt.lib /NODEFAULTLIB:msvcrt.lib
    ccache:                      NO
    Precompiled headers:         YES
    Extra dependencies:          wsock32 comctl32 gdi32 ole32 setupapi ws2_32
    3rdparty dependencies:       libprotobuf ade ittnotify libjpeg-turbo libwebp libpng libtiff libopenjp2 IlmImf zlib quirc ippiw ippicv

  OpenCV modules:
    To be built:                 calib3d core dnn features2d flann gapi highgui imgcodecs imgproc ml objdetect photo python3 stitching video videoio
    Disabled:                    world
    Disabled by dependency:      -
    Unavailable:                 java python2 ts
    Applications:                -
    Documentation:               NO
    Non-free algorithms:         NO

  Windows RT support:            NO

  GUI:                           WIN32UI
    Win32 UI:                    YES
    VTK support:                 NO

  Media I/O:
    ZLib:                        build (ver 1.2.12)
    JPEG:                        build-libjpeg-turbo (ver 2.1.2-62)
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.2.0)
    JPEG 2000:                   build (ver 2.4.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES (prebuilt binaries)
      avcodec:                   YES (58.134.100)
      avformat:                  YES (58.76.100)
      avutil:                    YES (56.70.100)
      swscale:                   YES (5.9.100)
      avresample:                YES (4.0.0)
    GStreamer:                   NO
    DirectShow:                  YES
    Media Foundation:            YES
      DXVA:                      YES

  Parallel framework:            Concurrency

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2020.0.0 Gold [2020.0.0]
           at:                   D:/a/opencv-python/opencv-python/_skbuild/win-amd64-3.6/cmake-build/3rdparty/ippicv/ippicv_win/icv
    Intel IPP IW:                sources (2020.0.0)
              at:                D:/a/opencv-python/opencv-python/_skbuild/win-amd64-3.6/cmake-build/3rdparty/ippicv/ippicv_win/iw
    Lapack:                      NO
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)

  OpenCL:                        YES (NVD3D11)
    Include path:                D:/a/opencv-python/opencv-python/opencv/3rdparty/include/opencl/1.2
    Link libraries:              Dynamic load

  Python 3:
    Interpreter:                 C:/hostedtoolcache/windows/Python/3.6.8/x64/python.exe (ver 3.6.8)
    Libraries:                   C:/hostedtoolcache/windows/Python/3.6.8/x64/libs/python36.lib (ver 3.6.8)
    numpy:                       C:/hostedtoolcache/windows/Python/3.6.8/x64/lib/site-packages/numpy/core/include (ver 1.13.3)
    install path:                python/cv2/python-3

  Python (for build):            C:/hostedtoolcache/windows/Python/2.7.18/x64/python.exe

  Java:
    ant:                         NO
    JNI:                         C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/8.0.332-9/x64/include C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/8.0.332-9/x64/include/win32 C:/hostedtoolcache/windows/Java_Temurin-Hotspot_jdk/8.0.332-9/x64/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    D:/a/opencv-python/opencv-python/_skbuild/win-amd64-3.6/cmake-install
-----------------------------------------------------------------

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

No branches or pull requests

2 participants