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

我只是一个虾纸丫 提交于 2020-12-10 06:58:32

问题


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?


回答1:


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.




回答2:


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



回答3:


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



来源:https://stackoverflow.com/questions/61159469/importerror-cannot-import-name-dnn-superres-for-python-example-of-super-resol

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!