'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. 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:
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:
4.2.0
My goal from this action is to use some advantage OpenCV 4.2 for example: 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:
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:
4.2.0
Sorry, for long explanation
Any help,thank you in advance.
Solution 1:[1]
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. :)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | Piyush Bajaj |