简体   繁体   English

如何同时使用两个 OpenCV 版本 python 运行代码?

[英]How to run the code using two OpenCV versions python at the same time?

Two versions are installed on my computer, OpenCV 3.3.1 is working with ROS library ( kinetic ) and OpenCV 4.2 has some advantage compared to 3.3.1.我的电脑上安装了两个版本,OpenCV 3.3.1 与 ROS 库(动力学)一起使用,OpenCV 4.2 与 3.3.1 相比具有一些优势。 So, I'd like to use them both.所以,我想同时使用它们。

simple code:简单的代码:

import cv2
print (cv2.__version__)

import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if  ros_path2 and ros_path3 in sys.path:
    sys.path.remove(ros_path2)
    sys.path.remove(ros_path3)
import cv2
print (cv2.__version__)

The output is: output 是:

3.3.1

3.3.1

if I comment out two first lines如果我注释掉前两行

# import cv2
# print (cv2.__version__)

import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if  ros_path2 and ros_path3 in sys.path:
    sys.path.remove(ros_path2)
    sys.path.remove(ros_path3)
import cv2
print (cv2.__version__)

The output becames: output 变为:

4.2.0

My goal from this action is to use some advantage OpenCV 4.2 for example: from cv2.xfeatures2d import matchGMS我的目标是利用一些优势 OpenCV 4.2 例如: from cv2.xfeatures2d import matchGMS

More explanations:更多解释:

if I added last line as:如果我将最后一行添加为:

import cv2
print (cv2.__version__)

import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if  ros_path2 and ros_path3 in sys.path:
    sys.path.remove(ros_path2)
    sys.path.remove(ros_path3)
import cv2
from cv2.xfeatures2d import matchGMS
print (cv2.__version__)

The output is: output 是:

3.3.1
Traceback (most recent call last):
  File "/home/redhwan/learn2.py", line 11, in <module>
    from cv2.xfeatures2d import matchGMS
ImportError: cannot import name matchGMS

if I comment out two first lines again:如果我再次注释掉前两行:

# import cv2
# print (cv2.__version__)

import sys
ros_path2 = '/usr/local/lib/python2.7/site-packages'
ros_path3 = '/usr/lib/python2.7/dist-packages'
if  ros_path2 and ros_path3 in sys.path:
    sys.path.remove(ros_path2)
    sys.path.remove(ros_path3)
import cv2
from cv2.xfeatures2d import matchGMS
print (cv2.__version__)

It is working fine, the output is:它工作正常,output 是:

4.2.0

Sorry, for long explanation抱歉,解释太长了

Any help,thank you in advance.任何帮助,提前谢谢你。

I don't think you can use them both because It was not designed it to be that way.我不认为你可以同时使用它们,因为它不是这样设计的。 I am sorry If I couldn't answer as you might have expected.很抱歉,如果我无法如您预期的那样回答。 I hope you work your way around this to avoid this kind of problem.我希望你能解决这个问题,以避免这种问题。 :) :)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM