https://stackoverflow.com/questions/26059134/adding-modules-from-opencv-contrib-to-opencv

https://stackoverflow.com/questions/26059134/adding-modules-from-opencv-contrib-to-opencv


Adding modules from opencv_contrib to OpenCV

I'm trying to add the xfeatures2d module from opencv_contrib to an existing OpenCV/Python project.

I've downloaded the latest version of the module from the repo, and built OpenCV again with the following additional params:

OPENCV_EXTRA_MODULES_PATH=/path/to/opencv_contrib-master/modules
BUILD_opencv_xfeatures2d=ON

Excerpt from build log:

-- Installing: /usr/local/lib/python2.7/site-packages/cv2.so
-- Installing: /usr/local/lib/python3.4/site-packages/cv2.so
-- Installing: /usr/local/lib/libopencv_xfeatures2d.3.0.0.dylib

It appears the new module is installed correctly. I'm able to import cv2 in both Python versions. However neither recognise the new features the module is supposed to add.

>>> cv2.SURF()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'SURF'
>>> cv2.xfeatures2d.SURF()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
AttributeError: 'module' object has no attribute 'xfeatures2d'
  • Could you consider editing the original Question since the answer does not reflect the question in the title. –  Elliot Woods  Mar 2 '17 at 6:08

4 Answers

up vote 15 down vote accepted

I encountered this same issue. I'm using python 2.7.6 and OpenCv 3.0 with the additional non-free modules. I do have xfeatures2d present in available modules and can import it, however it was as though xfeatures2d didn't contain SIFT or SURF. No matter how I called them it was the same Error:

"AttributeError: 'module' object has no attribute 'SIFT'

I tried the different name spaces suggested, and only recently noticed this detail and GOT IT WORKING!

$ python

>>>import cv2

>>>help(cv2.xfeatures2d)

You'll notice that it replies that it is now referred to as...

FUNCTIONS

SIFT_create(...)

and

SURF_create(...)

So very simply - the namespace is NOT "cv2.SIFT()" or "cv2.xfeatures2d.SIFT" but rather

cv2.xfeatures2d.SIFT_create()

Please give it a shot!

  • This does not really answer the question. If you have a different question, you can ask it by clickingAsk Question. You can also add a bounty to draw more attention to this question once you have enough reputation. –  RiggsFolly  Jan 26 '15 at 18:38
  • 2
    Thanks @RiggsFolly, but this could actually, potentially, answer the question. –  Robbert  Jan 27 '15 at 8:03 
  • 1
    @Sovtek - thanks for submitting your findings! I'll have a look at this solution later this week, when I continue work on the OpenCV project. If it does solve the issue for me, I'll mark your answer as Accepted. –  Robbert  Jan 27 '15 at 8:05
  • 1
    gave you a vote for the help(cv2.xfeatures2d). Worked for me. –  Yonatan Simson  May 8 '17 at 11:53

Another possibility (and the easiest one I found!) is to install the 2.4.9 release which already include SIFT and SURF algorithm. You just have to do then

import cv2
sift = cv2.SIFT()
(...)
  • 1
    Thanks for chiming in Jprog. I did end up using 2.4 for now (2.4.10 actually). I won't mark this as an accepted answer though, as it does not solve the issue in OpenCV 3. –  Robbert  Jan 13 '15 at 9:16

Install it from pip

Python 2.x

pip install opencv-contrib-python

Python 3.x

pip3 install opencv-contrib-python

Use sudo if a permsision error occurred.

  • I cannot find this package. Also 'pip3 list | grep opencv' does not contain it. Any ideas what am I missing? –  mapto  Feb 20 at 17:54
  • Actually, found it, not sure why it failed the first time. –  mapto  Feb 20 at 18:00

you are missing the new, additional namespace:


>>> help(cv2.xfeatures2d)
Help on module cv2.xfeatures2d in cv2:

NAME
    cv2.xfeatures2d

FILE
    (built-in)

FUNCTIONS
    SIFT(...)
        SIFT([, nfeatures[, nOctaveLayers[, contrastThreshold[, edgeThreshold[,
sigma]]]]]) -> <xfeatures2d_SIFT object>

    SURF(...)
        SURF([hessianThreshold[, nOctaves[, nOctaveLayers[, extended[, upright]]
]]]) -> <xfeatures2d_SURF object>

    StarDetector(...)
        StarDetector([, _maxSize[, _responseThreshold[, _lineThresholdProjected[
, _lineThresholdBinarized[, _suppressNonmaxSize]]]]]) -> <xfeatures2d_StarDetect
or object>

DATA
    FREAK_NB_ORIENPAIRS = 45
    FREAK_NB_PAIRS = 512
    FREAK_NB_SCALES = 64


>>> surf = cv2.xfeatures2d.SURF(300)
  • Thanks @berak (you seem to answer all OpenCV questions I come across). I have triedxfeatures2d but it gives the same error - no such attribute - on both Python 2 and 3. It also doesn't explain why PyCharm is autocompleting correctly. –  Robbert  Sep 26 '14 at 12:23
  • can it be, you got 2 pythons in your box ? –  berak  Sep 26 '14 at 12:25
  • Correct, I have both Python 2.7 and 3.4, just to test which version works, since OpenCV 3 is still in alpha. Both are able to load OpenCV correctly (though I have to change EXPORTPATH accordingly) and call regular cv2 properties. –  Robbert  Sep 26 '14 at 12:28
  • PyCharm was probably picking up autocomplete from an older OpenCV installation, considering the new namespace in OpenCV 3. When I reload the modules in PyCharm, autocomplete no longer works. –  Robbert  Sep 26 '14 at 12:45
  • 1
    I've been dealing with this issue myself. It looks like xfeatures2d is flat-out broken in the alpha/beta releases. It's building properly (at least according to the build log), but the python API is not built at all. –  Fake Name  Nov 29 '14 at 0:26


http://blog.youkuaiyun.com/firemicrocosm/article/details/48413763

http://blog.youkuaiyun.com/firemicrocosm/article/details/49307869

python3.0 以上版本配合opencv3.0以上的版本

SIFT及SURF等都被集成到contrib中,所以与之前的版本要区分开

[python]  view plain  copy
  1. import cv2  
  2. import numpy as np  
  3.   
  4. img = cv2.imread('lena.jpg')  
  5.   
  6. gray = cv2.cvtColor(img,cv2.COLOR_RGB2GRAY)  
  7.   
  8. siftDetector = cv2.xfeatures2d.SIFT_create(100)  
  9. kp,res = siftDetector.detectAndCompute(gray,None)  
  10. print(len(kp))  
  11. print(res)  
  12.   
  13. img = cv2.drawKeypoints(img,outImage=img,keypoints=kp, flags=cv2.DRAW_MATCHES_FLAGS_DRAW_RICH_KEYPOINTS)  
  14.   
  15. cv2.imshow("sift",img)  
  16. cv2.waitKey(0)  
  17. cv2.destroyAllWindows()  


<think>好的,我现在需要帮助用户解决在RHEL服务器上安装Docker时遇到的问题。根据用户提供的引用信息,错误主要是找不到仓库的元数据,可能是因为Docker的仓库配置不正确或者RHEL版本与仓库不兼容。首先,我得回顾一下用户提到的具体错误和解决方案。 用户提到在执行yum install dkms时遇到了404错误,无法下载repomd.xml文件。引用[2]中指出,访问https://download.docker.com/linux/rhel/9/时发现只有s390x和source目录,而没有x86_64的包,这说明Docker官方可能没有为RHEL 9的x86_64架构提供稳定的仓库。所以,用户遇到的问题可能与RHEL版本不兼容有关。 接下来,我需要确定常见的解决方案。通常,安装Docker在RHEL上需要添加正确的仓库配置,并且确保系统版本与仓库支持的版本匹配。如果官方仓库中没有对应的包,可能需要使用其他方法,比如使用CentOS的仓库或者Docker的edge/test版本。另外,用户可能需要安装必要的依赖项,比如yum-utils来管理仓库。 另一个可能性是GPG密钥的问题,用户提到需要补全RPM-GPG-KEY-rockyofficial,这可能意味着在添加仓库时没有正确导入GPG密钥,导致验证失败。所以,步骤中应该包括导入正确的GPG密钥。 另外,用户可能没有启用额外的仓库,比如RHEL的附加仓库或EPEL仓库,这些有时候是安装第三方软件所必需的。例如,EPEL(Extra Packages for Enterprise Linux)提供了许多额外的软件包,可能包含所需的依赖项。 还要考虑用户是否使用了正确的Docker版本。对于RHEL,可能需要使用Docker EE(Enterprise Edition)而不是CE(Community Edition),或者需要订阅才能访问某些仓库。不过,根据用户提供的引用[1],Docker EE的安装需要满足特定前提条件,比如注册系统、启用仓库等。用户可能需要按照官方文档的步骤来配置这些。 总结可能的解决步骤: 1. 检查系统版本是否与Docker仓库支持的版本匹配。如果使用RHEL 9,而官方仓库中没有x86_64的包,可能需要降级到RHEL 8或寻找替代仓库。 2. 添加正确的Docker仓库配置文件,确保URL正确,并且架构匹配。 3. 导入Docker的GPG密钥,以验证软件包签名。 4. 安装必要工具,如yum-utils,以便管理仓库。 5. 如果官方仓库不可用,尝试使用CentOS的仓库或EPEL。 6. 清理yum缓存并重新生成元数据,解决可能的缓存问题。 同时,用户可能需要启用RHEL的额外订阅,比如使用subscription-manager来启用附加仓库。或者考虑使用Podman替代Docker,因为Red Hat推荐使用Podman作为容器引擎,尤其是在RHEL 8及更高版本中。 需要验证每一步是否正确,比如检查仓库配置文件的路径是否正确,是否存在拼写错误,或者是否使用了错误的发行版名称。例如,某些情况下,可能需要将rhel替换为centos,但需要注意兼容性问题。 此外,用户提到的错误信息中提到了IP地址,这可能是一个网络问题,但根据引用内容,更可能的问题还是仓库配置不正确,导致yum无法找到正确的资源。需要确保仓库的baseurl或镜像列表是正确的,并且可访问。 最后,如果所有方法都失败,用户可能需要联系Docker支持或Red Hat支持,以确认RHEL 9是否被支持,或者是否有其他解决方案。同时,提供替代方案,比如使用其他容器工具,可能也是一个有效的建议。</think>在RHEL服务器上安装Docker时遇到仓库元数据下载失败的问题,通常与仓库配置、系统版本或网络访问有关。以下是逐步解决方案: ### 1. 验证系统版本与仓库兼容性 Docker官方仓库可能未支持RHEL 9的x86_64架构。通过以下命令检查系统版本: ```bash cat /etc/redhat-release # 输出示例:Red Hat Enterprise Linux release 9.0 (Plow) ``` 若显示为RHEL 9,需暂时切换至RHEL 8仓库配置(若系统允许降级)或修改仓库URL中的版本号[^2]。 ### 2. 修正Docker仓库配置文件 编辑或创建文件`/etc/yum.repos.d/docker-ce.repo`,将URL中的`rhel/9`改为`rhel/8`: ```ini [docker-ce-stable] name=Docker CE Stable - $basearch baseurl=https://download.docker.com/linux/rhel/8/$basearch/stable enabled=1 gpgcheck=1 gpgkey=https://download.docker.com/linux/rhel/gpg ``` ### 3. 导入GPG密钥 确保密钥正确导入: ```bash sudo rpm --import https://download.docker.com/linux/rhel/gpg ``` ### 4. 安装必要工具 安装`yum-utils`以支持仓库管理: ```bash sudo yum install -y yum-utils ``` ### 5. 清理并重建缓存 ```bash sudo yum clean all sudo rm -rf /var/cache/yum sudo yum makecache ``` ### 6. 替代方案:使用EPEL仓库 若Docker仓库仍不可用,尝试通过EPEL安装: ```bash sudo yum install -y epel-release sudo yum install -y docker ``` ### 7. 终极解决方案:改用Podman RHEL 8/9默认推荐使用Podman(兼容Docker CLI): ```bash sudo yum install -y podman systemctl start podman ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值