How to install python-opencv in amazon-sagemaker?

Viewed 964

I installed the OpenCV package by pip install opencv-python.

When I import cv2 in my code.

import cv2

I got the following error:


---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
/tmp/ipykernel_324/571303353.py in <module>
----> 1 import cv2

~/.conda/envs/default/lib/python3.9/site-packages/cv2/__init__.py in <module>
      6 import sys
      7 
----> 8 from .cv2 import *
      9 from .cv2 import _registerMatType
     10 from . import mat_wrapper

ImportError: libgthread-2.0.so.0: cannot open shared object file: No such file or directory

Here are some solutions for this error but seemly need root permission.

apt-get update -y
apt-get install libglib2.0-0

When I run these commands in the terminal I got the following errors.

(studiolab) studio-lab-user@default:~/sagemaker-studiolab-notebooks/vit/ViT-pytorch$ apt-get update -y
Reading package lists... Done
E: List directory /var/lib/apt/lists/partial is missing. - Acquire (13: Permission denied)
3 Answers

I had the same issue. With this line, you can install the glib dependency for Amazon Sagemaker Studio Lab. Just run it on your notebook cell.

! conda install glib=2.51.0 -y

You also can create another virtual environment for your session that contains glib:

! conda create -n glib-test -c defaults -c conda-forge python=3 glib=2.51.0` -y

This problem has been solved.

pip uninstall opencv-python
pip install opencv-python-headless

More details can be found in here.

I faced this ImportError too and Thanks to @AmirMasoud's suggested solution. After running ! conda install glib=2.51.0 -y in my virtual environment, now cv2 is imported successfully.

Related