opencv / opencv-python

Automated CI toolchain to produce precompiled opencv-python, opencv-python-headless, opencv-contrib-python and opencv-contrib-python-headless packages.
https://pypi.org/project/opencv-python/
MIT License
4.47k stars 842 forks source link

background subtractors cause segfault on macOS 14.0 #914

Closed tewhalen closed 10 months ago

tewhalen commented 11 months ago

I can't seem to get any background subtractors to successfully run on my Mac. They all throw a segfault when the .apply() method is called. It doesn't seem to matter which of the cv2.bgsegm Subtractors I use - they all segfault on apply(). cv2.BackgroundSubtractorMOG2 and cv2.BackgroundSubtractorKNN also segfault when .apply() is called.

This is on Mac OS Sonoma 14.0, x86_64

Steps to reproduce

import cv2

print(cv2.getBuildInformation())
img = cv2.imread("old_man_yells_at_cloud.jpg")
backSub = cv2.bgsegm.BackgroundSubtractorGMG()
backSub.apply(img)

Yields:

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

  Platform:
    Timestamp:                   2023-09-27T14:33:27Z
    Host:                        Darwin 20.6.0 x86_64
    CMake:                       3.27.5
    CMake generator:             Unix Makefiles
    CMake build tool:            /usr/bin/make
    Configuration:               Release

  CPU/HW features:
    Baseline:                    SSE SSE2 SSE3 SSSE3 SSE4_1
      requested:                 DETECT
    Dispatched code generation:  SSE4_2 FP16 AVX AVX2 AVX512_SKX
      requested:                 SSE4_1 SSE4_2 AVX FP16 AVX2 AVX512_SKX
      SSE4_2 (1 files):          + POPCNT SSE4_2
      FP16 (0 files):            + POPCNT SSE4_2 FP16 AVX
      AVX (7 files):             + POPCNT SSE4_2 AVX
      AVX2 (35 files):           + POPCNT SSE4_2 FP16 FMA3 AVX AVX2
      AVX512_SKX (5 files):      + POPCNT SSE4_2 FP16 FMA3 AVX AVX2 AVX_512F AVX512_COMMON AVX512_SKX

  C/C++:
    Built as dynamic libs?:      NO
    C++ standard:                11
    C++ Compiler:                /usr/local/opt/ccache/libexec/clang++  (ver 13.0.0.13000029)
    C++ flags (Release):         -arch x86_64   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C++ flags (Debug):           -arch x86_64   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    C Compiler:                  /usr/local/opt/ccache/libexec/clang
    C flags (Release):           -arch x86_64   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -O3 -DNDEBUG  -DNDEBUG
    C flags (Debug):             -arch x86_64   -fsigned-char -W -Wall -Wreturn-type -Wnon-virtual-dtor -Waddress -Wsequence-point -Wformat -Wformat-security -Wmissing-declarations -Wmissing-prototypes -Wstrict-prototypes -Wundef -Winit-self -Wpointer-arith -Wshadow -Wsign-promo -Wuninitialized -Wno-delete-non-virtual-dtor -Wno-unnamed-type-template-args -Wno-comment -fdiagnostics-show-option -Wno-long-long -Qunused-arguments -Wno-semicolon-before-method-body -ffunction-sections -fdata-sections  -fvisibility=hidden -fvisibility-inlines-hidden -g  -O0 -DDEBUG -D_DEBUG
    Linker flags (Release):      -arch x86_64  -Wl,-dead_strip
    Linker flags (Debug):        -arch x86_64  -Wl,-dead_strip
    ccache:                      YES
    Precompiled headers:         NO
    Extra dependencies:          -framework OpenCL /Applications/Xcode_13.2.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Accelerate.framework -lm -ldl -framework Cocoa -framework AppKit
    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:                    java world
    Disabled by dependency:      -
    Unavailable:                 python2 ts
    Applications:                -
    Documentation:               NO
    Non-free algorithms:         NO

  GUI:                           COCOA
    Cocoa:                       YES
    VTK support:                 NO

  Media I/O:
    ZLib:                        build (ver 1.2.13)
    JPEG:                        build-libjpeg-turbo (ver 2.1.3-62)
      SIMD Support Request:      YES
      SIMD Support:              YES
    WEBP:                        build (ver encoder: 0x020f)
    PNG:                         build (ver 1.6.37)
    TIFF:                        build (ver 42 - 4.2.0)
    JPEG 2000:                   build (ver 2.5.0)
    OpenEXR:                     build (ver 2.3.0)
    HDR:                         YES
    SUNRASTER:                   YES
    PXM:                         YES
    PFM:                         YES

  Video I/O:
    DC1394:                      NO
    FFMPEG:                      YES
      avcodec:                   YES (60.3.100)
      avformat:                  YES (60.3.100)
      avutil:                    YES (58.2.100)
      swscale:                   YES (7.1.100)
      avresample:                NO
    GStreamer:                   NO
    AVFoundation:                YES

  Parallel framework:            GCD

  Trace:                         YES (with Intel ITT)

  Other third-party libraries:
    Intel IPP:                   2021.8 [2021.8.0]
           at:                   /Users/runner/work/opencv-python/opencv-python/_skbuild/macosx-10.16-x86_64-3.7/cmake-build/3rdparty/ippicv/ippicv_mac/icv
    Intel IPP IW:                sources (2021.8.0)
              at:                /Users/runner/work/opencv-python/opencv-python/_skbuild/macosx-10.16-x86_64-3.7/cmake-build/3rdparty/ippicv/ippicv_mac/iw
    Lapack:                      YES (/Applications/Xcode_13.2.1.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX12.1.sdk/System/Library/Frameworks/Accelerate.framework -lm -ldl)
    Eigen:                       NO
    Custom HAL:                  NO
    Protobuf:                    build (3.19.1)
    Flatbuffers:                 builtin/3rdparty (23.5.9)

  OpenCL:                        YES (no extra features)
    Include path:                NO
    Link libraries:              -framework OpenCL

  Python 3:
    Interpreter:                 /Users/runner/work/opencv-python/opencv-python/venv/bin/python (ver 3.7.9)
    Libraries:                   /Library/Frameworks/Python.framework/Versions/3.7/lib/libpython3.7m.dylib (ver 3.7.9)
    numpy:                       /Users/runner/work/opencv-python/opencv-python/venv/lib/python3.7/site-packages/numpy/core/include (ver 1.17.0)
    install path:                python/cv2/python-3

  Python (for build):            /Users/runner/work/opencv-python/opencv-python/venv/bin/python

  Java:
    ant:                         /usr/local/bin/ant (ver 1.10.14)
    Java:                        NO
    JNI:                         /Users/runner/hostedtoolcache/Java_Temurin-Hotspot_jdk/8.0.382-5/x64/Contents/Home/include /Users/runner/hostedtoolcache/Java_Temurin-Hotspot_jdk/8.0.382-5/x64/Contents/Home/include/darwin /Users/runner/hostedtoolcache/Java_Temurin-Hotspot_jdk/8.0.382-5/x64/Contents/Home/include
    Java wrappers:               NO
    Java tests:                  NO

  Install to:                    /Users/runner/work/opencv-python/opencv-python/_skbuild/macosx-10.16-x86_64-3.7/cmake-install
-----------------------------------------------------------------

zsh: segmentation fault  
asmorkalov commented 11 months ago

You need to use createXXX factory methods for all background subtractor classes, e.g. backSub = cv2.bgsegm.createBackgroundSubtractorGMG() See https://github.com/opencv/opencv_contrib/blob/4.x/modules/bgsegm/tutorials/bgsegm_bg_subtraction/bgsegm_bg_subtraction.markdown. The same approach is valid for all cv::Algorithm derivatives, as they usually used in C++ code as cv::Ptr<>, but not as plain object.