11

I'm trying to test my picamera on thonny python ide on my raspberry pi but i'm getting an error:

Traceback (most recent call last):
  File "/home/pi/Documents/Camera_test.py", line 2, in <module>
    import cv2
  File "/usr/lib/python3/dist-packages/thonny/backend.py", line 305, in _custom_import
    module = self._original_import(*args, **kw)
  File "/home/pi/.local/lib/python3.7/site-packages/cv2/__init__.py", line 3, in <module>
    from .cv2 import *
  File "/usr/lib/python3/dist-packages/thonny/backend.py", line 305, in _custom_import
    module = self._original_import(*args, **kw)
ImportError: /home/pi/.local/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8

I've already tried searching for possible solutions but were not able to do it because i cant understand what they were trying to say something about -latomic. i'm pretty much new to this.

import numpy as np
import cv2

cap = cv2.VideoCapture(0)
cap.set(3,640) # set Width
cap.set(4,480) # set Height

while(True):
    ret, frame = cap.read()
    frame = cv2.flip(frame, -1) # Flip camera vertically
    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)

    cv2.imshow('frame', frame)
    cv2.imshow('gray', gray)

    k = cv2.waitKey(30) & 0xff
    if k == 27: # press 'ESC' to quit
        break

cap.release()
cv2.destroyAllWindows()
  • 1
    For what it's worth I'm having the same problem when importing OpenCV on my Raspberry Pi Compute Module 3, so at least we're not alone. – Rob Smallshire Nov 09 '19 at 20:51
  • The thing is it works on the terminal, but not on thonny ide. –  Nov 10 '19 at 01:37
  • 3
    The workaround for this OpenCV problem is to set the LD_PRELOAD environment variable (this is not the same as a variable in Python) to cause the missing library to be loaded: LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1 Unfortunately, I couldn't find a way to set environment variables in the Thonny IDE. – Rob Smallshire Nov 10 '19 at 12:12
  • I recommend you follow this GitHub issue on the PiWheels OpenCV build. Hopefully a fixed version of the OpenCV wheel will be forthcoming soon. https://github.com/piwheels/packages/issues/59 – Rob Smallshire Nov 10 '19 at 12:14

4 Answers4

13

Use OpenCV contrib with the right version.

pip install opencv-contrib-python==4.1.0.25

See answer at the end of this post.

cristhoper
  • 141
  • 5
2

If you pip install opencv then try
in your virtual environment

"pip install opencv-contrib-python==3.4.3.18"

laksh
  • 21
  • 3
2

I was facing the same problem also while folowing this totorial https://www.pyimagesearch.com/2019/09/16/install-opencv-4-on-raspberry-pi-4-and-raspbian-buster/

in comments is solved in this link and also solve the same for me thanks to him :- https://www.pyimagesearch.com/2019/09/16/install-opencv-4-on-raspberry-pi-4-and-raspbian-buster/#comment-572080

and this is what he wrots:-

#

I’m following the tutorial with RP4, 1gb memory, on Buster.

I was seeing the following error after following step 4(a), the full compile, when verifying the cv2 install from Python console (step 5):

ImportError: /home/pi/.virtualenvs/cv/lib/python3.7/site-packages/cv2/cv2.cpython-37m-arm-linux-gnueabihf.so: undefined symbol: __atomic_fetch_add_8

I was able to resolve the issue with the following steps:

1 – $ sudo find / -type f -name ‘atom.so*’ /usr/lib/arm-linux-gnueabihf/libatomic.so.1.2.0

2 – LD_PRELOAD=/usr/lib/arm-linux-gnueabihf/libatomic.so.1.2.0 python3

3 – Python 3.7.3 (default, Apr 3 2019, 05:39:12) [GCC 8.2.0] on linux Type “help”, “copyright”, “credits” or “license” for more information.

import cv2 print(cv2.version) 4.1.1

Hope this helps anyone with the same issue…

J

#

Also for ur information when i follow another tuotrail for the same writer it was working ok with me without problems here is the link:-

https://www.pyimagesearch.com/2018/09/26/install-opencv-4-on-your-raspberry-pi/

I am still 15 days experience in python and raspberry pi so sorry for any mistake in advane regards

0

Follow these steps it worked fine for me:

Install OpenCV on Raspberry Pi

  1. sudo apt-get update

  2. sudo apt-get install libhdf5-dev -y && sudo apt-get install libhdf5-serial-dev -y && sudo apt-get install libatlas-base-dev -y && sudo apt-get install libjasper-dev -y && sudo apt-get install libqtgui4 -y && sudo apt-get install libqt4-test -y

  3. pip3 install --user opencv-contrib-python

  4. pip install opencv-python

Testing OpenCV

To check whether OpenCV is correctly installed or not, try importing OpenCV by typing: Run python shell python3

then type: import cv2

If no errors are shown, your installation was successful!

To know which version of OpenCV you have, type the following command: cv2.version

Recommended Optional Libraries

sudo apt install libatlas3-base sudo apt-get install python3-numpy

sudo apt update sudo apt-get install python3-matplotlib -y