In one of my scripts (although this also happens when I just import cv2 in a Python 3 file), I'm getting an Import Error when importing cv2 on my Raspberry Pi 4B running Bullseye. Here is the error:
Traceback (most recent call last):
File "/home/mgerber23/Desktop/Scripts/Security/security.py", line 5, in <module>
import cv2
File "/usr/local/lib/python3.9/dist-packages/cv2/__init__.py", line 181, in <module>
bootstrap()
File "/usr/local/lib/python3.9/dist-packages/cv2/__init__.py", line 153, in bootstrap
native_module = importlib.import_module("cv2")
File "/usr/lib/python3.9/importlib/__init__.py", line 127, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
ImportError: /usr/local/lib/python3.9/dist-packages/cv2/cv2.abi3.so: undefined symbol: __atomic_store_8
I've gone through a bunch of different posts about this trying various pip and sudo apt install commands but nothing seems to work. I've tried:
sudo pip3 install opencv-python
sudo pip install opencv-python
sudo apt-get install build-essential cmake pkg-config libjpeg-dev libtiff5-dev libjasper-dev libpng-dev libavcodec-dev libavformat-dev libswscale-dev libv4l-dev libxvidcore-dev libx264-dev libfontconfig1-dev libcairo2-dev libgdk-pixbuf2.0-dev libpango1.0-dev libgtk2.0-dev libgtk-3-dev libatlas-base-dev gfortran libhdf5-dev libhdf5-serial-dev libhdf5-103 python3-pyqt5 python3-dev -y
I've also attempted to reinstall numpy, but that didn't help. I'm currently running numpy version 1.19.5 according to pip(3) show numpy. At one point, there was a long detailed error message specifying that numpy was not in PATH. I'm wondering if this is potentially messing up cv2.
This post seemed to be related (https://github.com/capnproto/capnproto/issues/1448), but I don't know enough to understand it, so if someone could slightly dumb it down for me that would be much appreciated.
When trying the following commands:
wget https://github.com/prepkg/opencv-raspberrypi/releases/latest/download/opencv_64.deb
sudo apt install -y ./opencv_64.deb
I receive the following error:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Note, selecting 'opencv:arm64' instead of './opencv_64.deb'
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:
The following packages have unmet dependencies:
opencv:arm64 : Depends: libgstreamer1.0-0:arm64 (>= 1.22.0) but it is not installable
Depends: libgstreamer-plugins-base1.0-0:arm64 (>= 1.22.0) but it is not installable
Depends: libavcodec59:arm64 (>= 5.1.3) but it is not installable
Depends: libavformat59:arm64 (>= 5.1.3) but it is not installable
Depends: libavutil57:arm64 (>= 5.1.3) but it is not installable
Depends: libswscale6:arm64 (>= 5.1.3) but it is not installable
Depends: python3-numpy:arm64 (>= 1.24.2) but it is not installable
E: Unable to correct problems, you have held broken packages.
Maybe the error has something to do with this?
My best guess is that it has something to do with how I installed cv2, but I have no idea what that exact problem could be. Also, I apologize in advance if this question isn't detailed enough, please let me know if there's anything else I can add.
A quick fix is to load Python with LD_PRELOAD
pointed to the correct location for libatomic
on your filesystem before running your script or Python REPL. Executing this command in your shell will tell you whether this is a suitable solution or not:
LD_PRELOAD=/usr/lib/arm-linux/gnueabihf/libatomic.so.1.2.0 python3 -c 'import cv2
print(cv2.__version__)';
More info regarding LD_PRELOAD
is available elsewhere on Stack Overflow.
A more permanent fix is to recompile opencv
for your system (based on the original guidance on the opencv
wiki) with an extra flag for cmake
using this script below as described in this GitHub issue comment:
# Install minimal prerequisites (Ubuntu 18.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip
# Download and unpack sources
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
unzip opencv.zip
# Create build directory
mkdir -p build && cd build
# Configure
cmake ../opencv-4.x
# Build
cmake --build .
# Install minimal prerequisites (Ubuntu 18.04 as reference)
sudo apt update && sudo apt install -y cmake g++ wget unzip
# Download and unpack sources
wget -O opencv.zip https://github.com/opencv/opencv/archive/4.x.zip
wget -O opencv_contrib.zip https://github.com/opencv/opencv_contrib/archive/4.x.zip
unzip opencv.zip
unzip opencv_contrib.zip
# Create build directory and switch into it
mkdir -p build && cd build
# Configure
cmake -DOPENCV_EXTRA_MODULES_PATH=../opencv_contrib-4.x/modules ../opencv-4.x -DOPENCV_FORCE_LIBATOMIC_COMPILER_CHECK=1
# Build
cmake --build .
A fix for the ultimate root cause seems to be in the pipeline for release in opencv 5.x
, but it's not clear when the maintainers expect to release this to the public given the ongoing crowdfunding effort doesn't seem to be close to their target. You could try to build from the v5.x
source on GitHub, but that'd be treading in uncharted waters and wouldn't necessarily work with the current Python bindings (among other incompatibilities I'm likely overlooking).