Search code examples
pythonopencvneural-networkopencv-contrib

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?


Solution

  • 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.