aliaksandr960 / segment-anything-eo

Earth observation tools for Meta AI Segment Anything

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

ModuleNotFoundError: No module named 'cv2'

safalabolo opened this issue · comments

Thank You for sharing your work.

After executing the lines of code in my Jupiter notebook
I get the following error message.

import cv2
import matplotlib.pyplot as plt
from sameo import SamEO
ModuleNotFoundError Traceback (most recent call last)
/tmp/ipykernel_6910/2586265285.py in
----> 1 import cv2
2 import matplotlib.pyplot as plt
3 from sameo import SamEO

ModuleNotFoundError: No module named 'cv2'**

Any help is appreciated
Thanks in advance for your support

@safalabolo You should try installing opencv by running this command in your active python environment

pip install opencv-python

This would install opencv to your environment which can be imported to script by

import cv2

Refer to this link https://pypi.org/project/opencv-python/

Hi @ishwar97 and thank You for your response.

After running in python notebook
import cv2
import matplotlib.pyplot as plt
from sameo import SamEO

I have the following error message

ImportError Traceback (most recent call last)
~/anaconda3/lib/python3.8/site-packages/osgeo/init.py in swig_import_helper()
17 try:
---> 18 return importlib.import_module(mname)
19 except ImportError as e:

~/anaconda3/lib/python3.8/importlib/init.py in import_module(name, package)
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)
128

~/anaconda3/lib/python3.8/importlib/_bootstrap.py in _gcd_import(name, package, level)

~/anaconda3/lib/python3.8/importlib/_bootstrap.py in find_and_load(name, import)

~/anaconda3/lib/python3.8/importlib/_bootstrap.py in find_and_load_unlocked(name, import)

~/anaconda3/lib/python3.8/importlib/_bootstrap.py in _load_unlocked(spec)

~/anaconda3/lib/python3.8/importlib/_bootstrap.py in module_from_spec(spec)

~/anaconda3/lib/python3.8/importlib/_bootstrap_external.py in create_module(self, spec)

~/anaconda3/lib/python3.8/importlib/_bootstrap.py in _call_with_frames_removed(f, *args, **kwds)

ImportError: libgdal.so.27: cannot open shared object file: No such file or directory

During handling of the above exception, another exception occurred:

ModuleNotFoundError Traceback (most recent call last)
in
1 import cv2
2 import matplotlib.pyplot as plt
----> 3 from sameo import SamEO
4

~/segment-anything-eo/sameo.py in
3 import sliding_window
4 import polygonization
----> 5 from tms2geotiff.tms2geotiff import draw_tile
6 from segment_anything import sam_model_registry, SamAutomaticMaskGenerator
7

~/segment-anything-eo/tms2geotiff/tms2geotiff.py in
12 from tqdm import tqdm
13 from PIL import Image
---> 14 from osgeo import gdal
15
16 import requests

~/anaconda3/lib/python3.8/site-packages/osgeo/init.py in
30 raise ImportError(traceback_string + '\n' + msg)
31 return importlib.import_module('_gdal')
---> 32 _gdal = swig_import_helper()
33 del swig_import_helper
34 elif version_info >= (2, 6, 0):

~/anaconda3/lib/python3.8/site-packages/osgeo/init.py in swig_import_helper()
29 traceback_string = ''.join(traceback.format_exception(*sys.exc_info()))
30 raise ImportError(traceback_string + '\n' + msg)
---> 31 return importlib.import_module('_gdal')
32 _gdal = swig_import_helper()
33 del swig_import_helper

~/anaconda3/lib/python3.8/importlib/init.py in import_module(name, package)
125 break
126 level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)
128
129

ModuleNotFoundError: No module named '_gdal'

@safalabolo Try running

pip install osgeo

This will install osgeo in your active py environment

After that try

from osgeo import gdal

If this runs without throwing an error, then try running your script. That should work.

Hi @ishwar97 thank You.
unfortunately, after

(base) safalabolo@safalabolo-HPE-340it:~$ pip install osgeo
Collecting osgeo
Using cached osgeo-0.0.1.tar.gz (1.2 kB)
Building wheels for collected packages: osgeo
Building wheel for osgeo (setup.py) ... error
ERROR: Command errored out with exit status 1:
command: /home/safalabolo/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-sz4d_xma/osgeo/setup.py'"'"'; file='"'"'/tmp/pip-install-sz4d_xma/osgeo/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' bdist_wheel -d /tmp/pip-wheel-q4i255ie
cwd: /tmp/pip-install-sz4d_xma/osgeo/
Complete output (27 lines):
running bdist_wheel
running build
installing to build/bdist.linux-x86_64/wheel
running install
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-sz4d_xma/osgeo/setup.py", line 32, in
setup(
File "/home/safalabolo/anaconda3/lib/python3.8/site-packages/setuptools/init.py", line 153, in setup
return distutils.core.setup(**attrs)
File "/home/safalabolo/anaconda3/lib/python3.8/distutils/core.py", line 148, in setup
dist.run_commands()
File "/home/safalabolo/anaconda3/lib/python3.8/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/home/safalabolo/anaconda3/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/home/safalabolo/anaconda3/lib/python3.8/site-packages/wheel/bdist_wheel.py", line 326, in run
self.run_command('install')
File "/home/safalabolo/anaconda3/lib/python3.8/distutils/cmd.py", line 313, in run_command
self.distribution.run_command(command)
File "/home/safalabolo/anaconda3/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/tmp/pip-install-sz4d_xma/osgeo/setup.py", line 27, in run
raise Exception(error_msg)
Exception: In order to be able to run from osgeo import gdal,
You were probably trying to install gdal by running pip install osgeo.
Instead, you should either pip install gdal or replace osgeo with gdal in your requirements.

ERROR: Failed building wheel for osgeo
Running setup.py clean for osgeo
Failed to build osgeo
Installing collected packages: osgeo
Running setup.py install for osgeo ... error
ERROR: Command errored out with exit status 1:
command: /home/safalabolo/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-sz4d_xma/osgeo/setup.py'"'"'; file='"'"'/tmp/pip-install-sz4d_xma/osgeo/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-t89o551_/install-record.txt --single-version-externally-managed --compile --install-headers /home/safalabolo/anaconda3/include/python3.8/osgeo
cwd: /tmp/pip-install-sz4d_xma/osgeo/
Complete output (18 lines):
running install
Traceback (most recent call last):
File "", line 1, in
File "/tmp/pip-install-sz4d_xma/osgeo/setup.py", line 32, in
setup(
File "/home/safalabolo/anaconda3/lib/python3.8/site-packages/setuptools/init.py", line 153, in setup
return distutils.core.setup(**attrs)
File "/home/safalabolo/anaconda3/lib/python3.8/distutils/core.py", line 148, in setup
dist.run_commands()
File "/home/safalabolo/anaconda3/lib/python3.8/distutils/dist.py", line 966, in run_commands
self.run_command(cmd)
File "/home/safalabolo/anaconda3/lib/python3.8/distutils/dist.py", line 985, in run_command
cmd_obj.run()
File "/tmp/pip-install-sz4d_xma/osgeo/setup.py", line 27, in run
raise Exception(error_msg)
Exception: In order to be able to run from osgeo import gdal,
You were probably trying to install gdal by running pip install osgeo.
Instead, you should either pip install gdal or replace osgeo with gdal in your requirements.
----------------------------------------
ERROR: Command errored out with exit status 1: /home/safalabolo/anaconda3/bin/python -u -c 'import sys, setuptools, tokenize; sys.argv[0] = '"'"'/tmp/pip-install-sz4d_xma/osgeo/setup.py'"'"'; file='"'"'/tmp/pip-install-sz4d_xma/osgeo/setup.py'"'"';f=getattr(tokenize, '"'"'open'"'"', open)(file);code=f.read().replace('"'"'\r\n'"'"', '"'"'\n'"'"');f.close();exec(compile(code, file, '"'"'exec'"'"'))' install --record /tmp/pip-record-t89o551_/install-record.txt --single-version-externally-managed --compile --install-headers /home/safalabolo/anaconda3/include/python3.8/osgeo Check the logs for full command output.
(base) safalabolo@safalabolo-HPE-340it:~$

Exception: In order to be able to run from osgeo import gdal,
You were probably trying to install gdal by running pip install osgeo.
Instead, you should either pip install gdal or replace osgeo with gdal in your requirements.

Can you try replacing gdal with osgeo?

Oh... Unfortunately, installing GDAL is always a pain... And installation process depends on particular platform, there is no universal solution... You could try docker with preinstalled GDAL, but in this case you will need to install torch properly to work with GPU. If you will not use functions for downloading form XYZ (TMS) services, you could just comment all lines with tms2geotiff and in this case there be only rasterio module in use (no GDAL) and you will be able to process tiff files.