Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: cannot import name 'dnn_superres' for python example of super resolution with opencv

I am trying to run an example for upscaling images from the following website: https://towardsdatascience.com/deep-learning-based-super-resolution-with-opencv-4fd736678066

This is the code I am using:

import cv2
from cv2 import dnn_superres

# Create an SR object
sr = dnn_superres.DnnSuperResImpl_create()

# Read image
image = cv2.imread('butterfly.png')

# Read the desired model
path = "EDSR_x3.pb"
sr.readModel(path)

# Set the desired model and scale to get correct pre- and post-processing
sr.setModel("edsr", 3)

# Upscale the image
result = sr.upsample(image)

# Save the image
cv2.imwrite("./upscaled.png", result)

I have downloaded the already trained model from the website, called "EDSR_x3.pb" and when I run the code I get the following error:

Traceback (most recent call last):
  File "upscale.py", line 2, in <module>
    from cv2 import dnn_superres
ImportError: cannot import name 'dnn_superres'

I now it seems like there is no such method or class, but I have already installed opencv and the contrib modules. Why do I get this error?

like image 773
Al Jenssen Avatar asked Apr 11 '20 15:04

Al Jenssen


5 Answers

The key is in the documentation for opencv-python. dnn_superres is an extra module and requires you to install opencv-contrib-python

pip install opencv-contrib-python

like image 150
swalters Avatar answered Nov 19 '22 11:11

swalters


Run the below commands into your terminal

pip install --upgrade opencv-contrib-python
pip install --upgrade opencv-python

Then, Restart your Jupyter notebok kernel or Reactivate your virtual environment so that the updated package can be used properly.

like image 24
Amar Kumar Avatar answered Oct 04 '22 03:10

Amar Kumar


your opencv version should be opencv4.2.0+, the same question: https://github.com/opencv/opencv_contrib/issues/2544

solution:

pip install --upgrade opencv-python
pip install --upgrade opencv-contrib-python
like image 7
zhicheng duan Avatar answered Nov 19 '22 10:11

zhicheng duan


I had the same problem with Python 3.6.9 and opencv 4.2.0, but after the upgrade to 4.3.0, the problem disappeared. If you have no problem upgrading the version, try 4.3.0.

like image 4
Paseul Avatar answered Nov 19 '22 09:11

Paseul


As of this date, to add something to the currently accepted answer :

If you are doing it in your local system :

If you have

opencv-python

you need to uninstall it first, you can check if you have above package or not by the command :

pip show opencv-python

if you have it, uninstall it first by

pip uninstall opencv-python

and install

opencv-contrib-python

by

pip install opencv-contrib-python

by default it will download the latest version but still check the version by the show command above for opencv-contrib-python this time, just to check the version is above opencv4.2.0+, to force download a speicific version go through this : pip install package_name==version_number

If you are doing it in Google Colab :

opencv-python is already installed in colab but the version is 4.1.2, you can check it by :

import cv2
cv2.__version__

you don't need to uninstall it, you just need to install opencv-contrib-python but if you install by :

!pip install opencv-contrib-python

It won't install it, it would default to same preinstalled opencv

so run this :

!pip install opencv-contrib-python==

which will output all the available versions for colab, just select any version 4.3.x or above and run again, like:

!pip install opencv-contrib-python==4.3.0.36

like image 4
Arnav Das Avatar answered Nov 19 '22 11:11

Arnav Das