I installed OpenCV version 4.5.2.52 to try and use super-resolution on pictures that I want to read but are too blurry.
To do so, I am following the code from this site: https://programmer.group/opencv-advanced-super-resolution-based-on-opencv.html
There are different codes on this page that I can copy, I wanted to try this one :
import cv2
import matplotlib.pyplot as plt
# Read picture
img = cv2.imread("AI-Courses-By-OpenCV-Github.png")
img = img[5:60,700:755]
sr = cv2.dnn_superres.DnnSuperResImpl_create()
path = "ESPCN_x3.pb"
sr.readModel(path)
sr.setModel("espcn",3)
result = sr.upsample(img)
# Resize image
resized = cv2.resize(img,dsize=None,fx=3,fy=3)
plt.figure(figsize=(6,2))
plt.subplot(1,3,1)
# original image
plt.imshow(img[:,:,::-1])
plt.subplot(1,3,2)
# SR up sampling image
plt.imshow(result[:,:,::-1])
plt.subplot(1,3,3)
## Sampling images on OpenCV
plt.imshow(resized[:,:,::-1])
plt.show()
When I ran it I got the error "AttributeError: module 'cv2' has no attribute 'dnn_superres'". So I checked these pages "https://blog.csdn.net/qq_48455792/article/details/120258336" (translated from chinese) and "https://github.com/opencv/opencv-python/issues/441" where it was written that for versions of openCV that are above 4.5.x, the libraries have been moved to 'legacy'.
I understood that I just had to change sr = cv2.dnn_superres.DnnSuperResImpl_create()
into sr = cv2.legacy.dnn_superres.DnnSuperResImpl_create()
for it to work.
But with this change I got the error
AttributeError: module 'cv2' has no attribute 'legacy'
.
I checked the answer to the error on stackoverflow on these pages :
I'm getting module 'cv2.cv2' has no attribute 'legacy' ERROR. How can I fix it?
AttributeError: module 'cv2.cv2' has no attribute 'createLBPHFaceRecognizer'
On both these pages, it was recommended either to install the contribution openCV or to install an older version than the one I have (4.4.0.46), but none of these worked in my case, so I don't know what to do now.
Could you help me ?
Thank you in advance !
CodePudding user response:
I don't have this problem anymore so I will share what I did to make it work.
As Christoph Rackwitz mentionned opencv-contrib-python
alone works just fine with the version 4.5.5.62 (latest version) to use the function cv2.dnn_superres
.
I personnally had to uninstall all previous installations dealing with opencv :
pip uninstall opencv-python
pip uninstall opencv-contrib-python
pip uninstall opencv-contrib-python-headless
Then I deleted all files that had to do with opencv too : after the uninstallation a "cv2" file and "opencv_contrib_python-4.5.2.52.dist-info" remained and caused errors.
Once all these files were deleted I reinstalled opencv-contrib with the right version using :
pip3 install opencv-contrib-python==4.5.5.62
It appeared in the Python\Lib\site-packages directory and now I can use cv2 just fine.
Thank you for your help, I hope it will help others.
CodePudding user response:
Do not install both opencv-python
and opencv-contrib-python
at the same time. Install only one of both.
I can access cv2.dnn_superres
in opencv-contrib-python
version 4.5.5 (currently the latest).