Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BUG] Cannot import name '_registerMatType' from 'cv2.cv2' #1494

Closed
2 of 6 tasks
Saransh-cpp opened this issue Dec 29, 2021 · 12 comments
Closed
2 of 6 tasks

[BUG] Cannot import name '_registerMatType' from 'cv2.cv2' #1494

Saransh-cpp opened this issue Dec 29, 2021 · 12 comments
Labels
bug Bug fixes

Comments

@Saransh-cpp
Copy link

System information

  • OS Platform and Distribution (e.g., Linux Ubuntu 16.04): Google Colab
  • FiftyOne installed from (pip or source): pip
  • FiftyOne version (run fiftyone --version): 0.14.2
  • Python version: 3.7.12

Commands to reproduce

Run the following in a Colab notebook -

%pip install fiftyone
import fiftyone as fo

Describe the problem

Running the commands above in a Colab notebook (quickstart.ipynb) gives an import error. My initial guess is that this is a version conflict issue but I couldn't explore this a lot as I am not familiar with fiftyone (a new user).

Code to reproduce issue

import fiftyone as fo

Other info / logs

---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-9-8c082ccbb59f> in <module>()
----> 1 import fiftyone as fo

9 frames
/usr/local/lib/python3.7/dist-packages/fiftyone/__init__.py in <module>()
     23 __version__ = _foc.VERSION
     24 
---> 25 from fiftyone.__public__ import *
     26 from fiftyone.core.uid import log_import_if_allowed as _log_import
     27 from fiftyone.migrations import migrate_database_if_necessary as _migrate

/usr/local/lib/python3.7/dist-packages/fiftyone/__public__.py in <module>()
      7 """
      8 import fiftyone.core.config as foc
----> 9 import fiftyone.core.odm as foo
     10 
     11 config = foc.load_config()

/usr/local/lib/python3.7/dist-packages/fiftyone/core/odm/__init__.py in <module>()
     55     validate_fields_match,
     56 )
---> 57 from .sample import (
     58     DatasetSampleDocument,
     59     NoDatasetSampleDocument,

/usr/local/lib/python3.7/dist-packages/fiftyone/core/odm/sample.py in <module>()
     63 import fiftyone.core.fields as fof
     64 import fiftyone.core.frame_utils as fofu
---> 65 import fiftyone.core.metadata as fom
     66 import fiftyone.core.media as fomm
     67 import fiftyone.core.utils as fou

/usr/local/lib/python3.7/dist-packages/fiftyone/core/metadata.py in <module>()
     15 
     16 import eta.core.utils as etau
---> 17 import eta.core.video as etav
     18 
     19 from fiftyone.core.odm.document import DynamicEmbeddedDocument

/usr/local/lib/python3.7/dist-packages/eta/core/video.py in <module>()
     35 import threading
     36 
---> 37 import cv2
     38 import dateutil.parser
     39 import numpy as np

/usr/local/lib/python3.7/dist-packages/google/colab/_import_hooks/_cv2.py in load_module(self, name)
     79 
     80     module_info = imp.find_module(name, self.path)
---> 81     cv_module = imp.load_module(name, *module_info)
     82 
     83     if not previously_loaded:

/usr/lib/python3.7/imp.py in load_module(name, file, filename, details)
    242             return load_dynamic(name, filename, file)
    243     elif type_ == PKG_DIRECTORY:
--> 244         return load_package(name, filename)
    245     elif type_ == C_BUILTIN:
    246         return init_builtin(name)

/usr/lib/python3.7/imp.py in load_package(name, path)
    214         return _exec(spec, sys.modules[name])
    215     else:
--> 216         return _load(spec)
    217 
    218 

/usr/local/lib/python3.7/dist-packages/cv2/__init__.py in <module>()
      7 
      8 from .cv2 import *
----> 9 from .cv2 import _registerMatType
     10 from . import mat_wrapper
     11 from . import gapi

ImportError: cannot import name '_registerMatType' from 'cv2.cv2' (/usr/local/lib/python3.7/dist-packages/cv2/cv2.cpython-37m-x86_64-linux-gnu.so)

What areas of FiftyOne does this bug affect?

  • App: FiftyOne application issue
  • Core: Core fiftyone Python library issue
  • Server: Fiftyone server issue

Willingness to contribute

The FiftyOne Community encourages bug fix contributions. Would you or another
member of your organization be willing to contribute a fix for this bug to the
FiftyOne codebase?

  • Yes. I can contribute a fix for this bug independently.
  • Yes. I would be willing to contribute a fix for this bug with guidance
    from the FiftyOne community.
  • No. I cannot contribute a bug fix at this time.
@Saransh-cpp Saransh-cpp added the bug Bug fixes label Dec 29, 2021
@valentindbdg
Copy link

valentindbdg commented Dec 29, 2021

Had similar issue and it started a few hours ago. It seems like the problem comes from a version of opencv.

Now the version of opencv that is downloaded is 4.5.5:

Collecting fiftyone
Downloading fiftyone-0.14.2-py3-none-any.whl (1.2 MB)
|████████████████████████████████| 1.2 MB 5.0 MB/s
Requirement already satisfied: argcomplete in /usr/local/lib/python3.7/dist-packages (from fiftyone) (1.12.3)
Requirement already satisfied: future in /usr/local/lib/python3.7/dist-packages (from fiftyone) (0.16.0)
Collecting mongoengine==0.20.0
Downloading mongoengine-0.20.0-py3-none-any.whl (106 kB)
|████████████████████████████████| 106 kB 58.3 MB/s
Collecting motor<3,>=2.3
Downloading motor-2.5.1-py3-none-any.whl (55 kB)
|████████████████████████████████| 55 kB 2.9 MB/s
Collecting kaleido
Downloading kaleido-0.2.1-py2.py3-none-manylinux1_x86_64.whl (79.9 MB)
|████████████████████████████████| 79.9 MB 135 kB/s
Collecting fiftyone-db<0.4,>=0.3
Downloading fiftyone_db-0.3.0-py3-none-manylinux1_x86_64.whl (29.2 MB)
|████████████████████████████████| 29.2 MB 1.2 MB/s
Collecting pprintpp
Downloading pprintpp-0.4.0-py2.py3-none-any.whl (16 kB)
Requirement already satisfied: Jinja2 in /usr/local/lib/python3.7/dist-packages (from fiftyone) (2.11.3)
Requirement already satisfied: retrying in /usr/local/lib/python3.7/dist-packages (from fiftyone) (1.3.3)
Requirement already satisfied: pytz in /usr/local/lib/python3.7/dist-packages (from fiftyone) (2018.9)
Collecting boto3
Downloading boto3-1.20.26-py3-none-any.whl (131 kB)
|████████████████████████████████| 131 kB 46.0 MB/s
Collecting aiofiles
Downloading aiofiles-0.8.0-py3-none-any.whl (13 kB)
Collecting eventlet
Downloading eventlet-0.33.0-py2.py3-none-any.whl (226 kB)
|████████████████████████████████| 226 kB 58.0 MB/s
Requirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from fiftyone) (3.13)
Collecting plotly<5,>=4.14
Downloading plotly-4.14.3-py2.py3-none-any.whl (13.2 MB)
|████████████████████████████████| 13.2 MB 47.7 MB/s
Requirement already satisfied: tornado<7,>=5.1.1 in /usr/local/lib/python3.7/dist-packages (from fiftyone) (5.1.1)
Collecting universal-analytics-python3<2,>=1.0.1
Downloading universal_analytics_python3-1.1.1-py3-none-any.whl (10 kB)
Requirement already satisfied: setuptools in /usr/local/lib/python3.7/dist-packages (from fiftyone) (57.4.0)
Requirement already satisfied: pymongo<4,>=3.11 in /usr/local/lib/python3.7/dist-packages (from fiftyone) (3.12.1)
Requirement already satisfied: scikit-image in /usr/local/lib/python3.7/dist-packages (from fiftyone) (0.18.3)
Collecting Deprecated
Downloading Deprecated-1.2.13-py2.py3-none-any.whl (9.6 kB)
Collecting opencv-python-headless
Downloading opencv_python_headless-4.5.5.62-cp36-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (47.7 MB)
|████████████████████████████████| 47.7 MB 61 kB/s

But before it was 4.5.4:

Collecting fiftyone

Downloading fiftyone-0.14.2-py3-none-any.whl (1.2 MB)
|████████████████████████████████| 1.2 MB 4.2 MB/s
Requirement already satisfied: scikit-image in /usr/local/lib/python3.7/dist-packages (from fiftyone) (0.18.3)
Collecting boto3
Downloading boto3-1.20.26-py3-none-any.whl (131 kB)
|████████████████████████████████| 131 kB 48.1 MB/s
Requirement already satisfied: tabulate in /usr/local/lib/python3.7/dist-packages (from fiftyone) (0.8.9)
Collecting universal-analytics-python3<2,>=1.0.1
Downloading universal_analytics_python3-1.1.1-py3-none-any.whl (10 kB)
Collecting motor<3,>=2.3
Downloading motor-2.5.1-py3-none-any.whl (55 kB)
|████████████████████████████████| 55 kB 3.5 MB/s
Collecting fiftyone-brain<0.8,>=0.7.2
Downloading fiftyone_brain-0.7.2-cp37-cp37m-manylinux1_x86_64.whl (639 kB)
|████████████████████████████████| 639 kB 44.4 MB/s
Collecting eventlet
Downloading eventlet-0.33.0-py2.py3-none-any.whl (226 kB)
|████████████████████████████████| 226 kB 46.8 MB/s
Collecting kaleido
Downloading kaleido-0.2.1-py2.py3-none-manylinux1_x86_64.whl (79.9 MB)
|████████████████████████████████| 79.9 MB 127 kB/s
Requirement already satisfied: Jinja2 in /usr/local/lib/python3.7/dist-packages (from fiftyone) (2.11.3)
Requirement already satisfied: argcomplete in /usr/local/lib/python3.7/dist-packages (from fiftyone) (1.12.3)
Collecting plotly<5,>=4.14
Downloading plotly-4.14.3-py2.py3-none-any.whl (13.2 MB)
|████████████████████████████████| 13.2 MB 21.6 MB/s
Collecting aiofiles
Downloading aiofiles-0.8.0-py3-none-any.whl (13 kB)
Requirement already satisfied: numpy in /usr/local/lib/python3.7/dist-packages (from fiftyone) (1.19.5)
Collecting xmltodict
Downloading xmltodict-0.12.0-py2.py3-none-any.whl (9.2 kB)
Collecting voxel51-eta<0.7,>=0.6.1
Downloading voxel51_eta-0.6.1-py2.py3-none-any.whl (562 kB)
|████████████████████████████████| 562 kB 42.7 MB/s
Requirement already satisfied: PyYAML in /usr/local/lib/python3.7/dist-packages (from fiftyone) (3.13)
Collecting opencv-python-headless
Downloading opencv_python_headless-4.5.4.60-cp37-cp37m-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (47.6 MB)
|████████████████████████████████| 47.6 MB 1.5 MB/s

When trying to import fiftyone, the following error appears:


ImportError Traceback (most recent call last)
in ()
----> 1 import fiftyone as fo
2
3 #Add ground_truth annotations:
4
5

9 frames
/usr/local/lib/python3.7/dist-packages/cv2/init.py in ()
7
8 from .cv2 import *
----> 9 from .cv2 import _registerMatType
10 from . import mat_wrapper
11 from . import gapi

ImportError: cannot import name '_registerMatType' from 'cv2.cv2' (/usr/local/lib/python3.7/dist-packages/cv2/cv2.cpython-37m-x86_64-linux-gnu.so)

Because '_registerMatType' is a part of OpenCV 4.5.4.

Any solution available for this issue?

@valentindbdg
Copy link

@Saransh-cpp
Try this in that order in your google colab, but after doing "factory reset runtime" to make sure no previous version is installed by default:

!pip uninstall opencv_python_headless

!pip install opencv-python-headless==4.5.4.60

!pip install fiftyone

import fiftyone as fo

It worked for me.

@Saransh-cpp
Copy link
Author

Thanks, @valentindbdg! This works! My initial guess was right 😄. I guess there is no way to make it work without the extra uninstall and install commands?

@brimoor brimoor changed the title [BUG] Importing fiftyone on Google Colab gives an error [BUG] Cannot import name '_registerMatType' from 'cv2.cv2' Dec 29, 2021
@brimoor
Copy link
Contributor

brimoor commented Dec 29, 2021

Per @valentindbdg's helpful suggestion, it seems that the issue is that multiple versions of OpenCV are being installed: opencv/opencv-python#591

FiftyOne's requirements simply request any version of opencv-python-headless:

"opencv-python-headless",

I just tried this out myself and found that Colab's default environment has opencv-python and opencv-contrib-python installed, and if I first uninstall those then the FiftyOne install works (no need to restart runtime):

!pip uninstall -y opencv-python opencv-contrib-python
!pip install fiftyone
# works!
import fiftyone as fo

@brimoor
Copy link
Contributor

brimoor commented Dec 29, 2021

There's a detailed discussion of this issue at aleju/imgaug#473.

I see that they ended up modifying their setup.py to attempt to allow any of the opencv-*-python-* flavors:
https://github.com/aleju/imgaug/blob/master/setup.py

I haven't studied the full discussion in the issue in detail, but at a quick glance it seems that is still not a complete solution, however.

@valentindbdg
Copy link

Thank you @brimoor!

@Saransh-cpp
Copy link
Author

Thanks, @brimoor, and @valentindbdg! I finally found the root problem -

I tried installing fiftyone with the same opencv-python-headless and opencv-python versions (available in Google Colab) in my local system, and surprisingly it worked. This was obviously weird as the same code with the same versions was not working on Google Colab. Hence, I tried uninstalling opencv-contrib-python (the package that was not being used anywhere) using -

!pip uninstall --yes opencv-contrib-python

and voila, everything worked. The import statement worked after installing fiftyone using -

!pip install fiftyone

So the root problem is the presence of opencv-contrib-python in the Colab environment. Should we mention this somewhere in the documentation? Or should we raise an error if opencv-contrib-python is installed while importing cv2 inside fiftyone?

@Saransh-cpp
Copy link
Author

There's a detailed discussion of this issue at aleju/imgaug#473.

I see that they ended up modifying their setup.py to attempt to allow any of the opencv-*-python-* flavors: https://github.com/aleju/imgaug/blob/master/setup.py

I haven't studied the full discussion in the issue in detail, but at a quick glance it seems that is still not a complete solution, however.

Also yes, I tried this but modifying the setup.py file did not work :(

@brimoor
Copy link
Contributor

brimoor commented Dec 30, 2021

@Saransh-cpp thanks for the extra investigation. The issue is not opencv-contrib-python per se, but rather having multiple opencv-*-python-* packages with incompatible versions.

For example, on my local machine I have these installed and everything works:

opencv-contrib-python==4.5.5.62
opencv-python==4.5.4.60
opencv-python-headless==4.5.4.60

But in Colab, after pip install fiftyone, one currently gets:

opencv-contrib-python==4.1.2.30
opencv-python==4.1.2.30
opencv-python-headless==4.5.5.62

which doesn't work.

The consensus online seems to be that having any pair of opencv-*-python-* packages installed in the same environment is always a bad idea. Maybe it will work for particular versions of each, but other times it may not.

It's worth noting that FiftyOne has been running in Colab fine for a long time now. This issue popped up due to either a new OpenCV release or a change in Colab's default OpenCV versions.

@benjaminpkane
Copy link
Contributor

benjaminpkane commented Dec 30, 2021

The simplest workaround is to install the last working version of opencv-python-headless when installing fiftyone. No uninstall commands required:

!pip install opencv-python-headless==4.5.4.60 fiftyone

Update: as of fiftyone==0.14.3, another option is to install FiftyOne like this:

!pip install fiftyone --no-binary fiftyone,voxel51-eta

which should avoid installing any duplicate OpenCV versions (at the cost of not leveraging our prebuilt wheel).

If you're reading this AFTER a failed FiftyOne import in Colab, the fastest solution is to:

  • Restart your runtime (Runtime -> Restart runtime)
  • Install the last working version of opencv-python-headless:
!pip install opencv-python-headless==4.5.4.60

@Saransh-cpp
Copy link
Author

Saransh-cpp commented Dec 31, 2021

@brimoor, yes that makes more sense. Thanks, @benjaminpkane! This works too!

@native-m
Copy link

native-m commented May 21, 2022

Another workaround is to upgrade dependencies upfront.

!pip install albumentations --upgrade
!pip install opencv-contrib-python --upgrade
!pip install opencv-python --upgrade
!pip install opencv-python-headless --upgrade
!pip install datascience --upgrade
!pip install flask --upgrade
!pip install fiftyone

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Bug fixes
Projects
None yet
Development

No branches or pull requests

5 participants