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?
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
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.
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
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With