如何修复“错误:模块‘cv2’”没有属性“legacy”;在 python 3.7.9 和 Windows 11 上”?
我安装了 OpenCV 版本 4.5.2.52 来尝试对我想阅读但太模糊的图片使用超分辨率。
为此,我遵循此站点中的代码: https ://programmer.group/opencv-advanced-super-resolution-based-on-opencv.html
此页面上有不同的代码我可以复制,我想尝试一下这个:
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()
当我运行它时,我收到错误“属性错误:模块“cv2”没有属性“dnn_superres””。 所以我检查了这些页面“https://blog.csdn.net/qq_48455792/article/details/120258336”(翻译自中文)和“https://github.com/opencv/opencv-python/issues/441”其中据报道,对于 4.5.x 以上的 openCV 版本,库已移至“旧版”。
我知道我只需将 sr = cv2.dnn_superres.DnnSuperResImpl_create() 更改为 sr = cv2.legacy.dnn_superres.DnnSuperResImpl_create() 即可使其正常工作。
但随着这个改变我得到了错误 AttributeError:模块“cv2”没有属性“legacy”
。
我在这些页面上检查了 stackoverflow 上错误的答案:
我收到模块“cv2.cv2”没有属性“旧版”错误。我该如何修复它
? cv2' has no attribute 'createLBPHFaceRecognizer'
在这两个页面上,建议安装贡献 openCV 或安装比我拥有的版本(4.4.0.46)旧版本,但这些都不适用于我的情况,所以我不知道现在该怎么办。
你能帮我吗?
先感谢您 !
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 errorAttributeError: 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 !
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我不再遇到这个问题,所以我将分享我为使其发挥作用所做的事情。
正如 Christoph Rackwitz 提到的,
opencv-contrib-python
单独与版本 4.5.5.62(最新版本)一起使用函数cv2.dnn_superres
就可以很好地工作。我个人必须卸载以前所有与 opencv 相关的安装:
然后我也删除了所有与 opencv 有关的文件:卸载后,出现一个“cv2”文件和“< em>opencv_contrib_python-4.5.2.52.dist-info”仍然存在并导致错误。
删除所有这些文件后,我使用正确的版本重新安装了 opencv-contrib:
它出现在 Python\Lib\site-packages 目录中,现在我可以很好地使用 cv2 。
感谢您的帮助,我希望它能帮助其他人。
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 functioncv2.dnn_superres
.I personnally had to uninstall all previous installations dealing with opencv :
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 :
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.
请勿同时安装
opencv-python
和opencv-contrib-python
。 仅安装其中一个。我可以在
opencv-contrib-python
版本 4.5.5(当前最新)中访问cv2.dnn_superres
。Do not install both
opencv-python
andopencv-contrib-python
at the same time. Install only one of both.I can access
cv2.dnn_superres
inopencv-contrib-python
version 4.5.5 (currently the latest).